操作系统学习笔记——进程

本文是本人操作系统课程的笔记,因为是所谓的“双语授课”,所以笔记也有些中英夹杂。

3.1进程

3.1.1概念

A process is a program in execution.

3.1.2进程包括

text section     :        the program code

data section    :        global variables

pc                       :        he value of program counter

registers           :        the contents of the processor’sregisters

stack                  :        process stack, contains temporary data.

such as function parameters,  return address, local variables

heap                  :      a process may have.

memory that is dynamically allocated during process runtime.

A process includes: (UNIX)

text section – contains the program code

data section – contains global variables

PCB (Process Control Block)

current activities

program counter

general registers

stack – contains temporary data

Figure 1 process in memory

3.1.3进程的特性

3.1.3.1进程与程序的比较

进程与程序是截然不同的两个概念;

一个程序可以对应多个进程;

一个进程也可以由多个程序段共同完成一项任务;(接力)

进程具有五个特征,而程序则不具备;

一个程序不能两次属于同一个进程

3.1.3.2进程的五个基本特征

动态性,并发性,独立性,异步性,结构特征。

动态性

是进程的最基本的特征,表现在进程由创建而产生,由调度而执行,因得不到资源而暂停执行,由撤销而消亡

进程具有一定的生命期;而程序只是一组指令的集合,并存放在某种介质上,并无运动的含义,因此程序是个静态实体

并发性

多个进程实体同存于内存中,能在一段时间内同时执行;

引入进程的目的正是为了使其程序能和其它进程的程序并发执行,而程序是不能并发的;

独立性

进程实体是一个能独立运行的基本单位,同时也是系统中独立获得资源和独立调度的基本单位;

凡未建立进程的程序,都不能作为一个独立的单位参加运行;

异步性

指进程按各自独立的、不可预知的速度向前推进;

或者说,进程按异步方式运行;

OS中必须采取措施保证各程序之间能协调运行;

结构特征

从结构上看,进程实体由程序段、数据段以及进程控制块组成;

这三部分也称为进程影像;

3.1.4进程的状态

进程执行时,它的状态会改变。

3.1.4.1进程的五状态

New                   : The process is being created

Running           : Instructions are being executed

Waiting             : The process is waiting for some event to occur

(such as an I/Ocompletion or reception of a signal)

Ready                : The process is waiting to be assigned to a processor

Terminated      : The process has finished execution

3.1.4.2进程状态的转换及转换条件

Only one progress can be running on any processor at any instant

Figure 2进程5状态图

Figure 3进程7状态图

3.1.4.3几个词语的区分

suspend vs.block

suspend (挂起)

active->static

activate(激活)

static ->active

activity vs. wakeup

blocked(waiting,sleeping)(阻塞、等待、睡眠)

running->waiting

wakeup (唤醒)

waiting ->ready

concurrent vs. parallel

concurrent      并发 时间段

parallel             并行 时刻

 3.1.5 PCB

3.1.5.1 PCB (process control block) 概念

系统为了管理进程设置的一个专门的数据结构,用它来记录进程的外部特征,描述进程的运动变化过程.

PCB是进程存在的唯一标志;

操作系统通过PCB而感知进程的存在;

3.1.5.4 PCB的组成

Process number

Pointer to text section

Pointer to data section

 Process state

 Program counter

 CPU registers

 CPU scheduling information

 Memory-management information

 Accounting information

 I/O status information

 Pointer to next PCB

3.2进程调度

3.2.1 Process Scheduling Queues

queue             :  set of all processes in the system

Ready queue        :  setof all processes residing in main memory, ready and waiting to execute

Device queues       :  set of processes waiting for an I/O device

Processes migrate among thevarious queues

 

Readyqueue and various I/O device queues.

 3.2.2 Schedulers

Short-term scheduler (or
CPU scheduler
)

– selects which process should be executed next andallocates CPU

Short-termscheduler is invoked frequently (milliseconds) T (must be fast)

Long-term scheduler (or
job scheduler
)

– selects which processes should be brought into theready queue

Long-termscheduler is invoked infrequently (seconds, minutes) T (may be slow)

Thelong-term scheduler controls the degree of multiprogramming

Processes can be described as either:

I/O-bound process – spends more time doing I/O thancomputations, many short CPU bursts

CPU-bound process – spends more time doing computations; fewvery long CPU bursts

Long-term scheduler strives for good process mix

Medium Term Scheduler

– can be added if degree of multipleprogramming needs to
decrease

Remove process from memory, store on disk,bring back in from disk to continue execution:swapping

Selects which process should be swapped in orswapped out.

Figure 4 addition of medium-term scheduling to the queuing diagram

3.2.3  Contex switch

When CPU switches to anotherprocess, the system must

save the state of the old process

and

load the saved statefor the new process via acontext switch.

Switching the CPU to anotherprocess requires performing

a statesave of current process

and

a state restore of a differentprocess.

Contextof a process represented in the PCB

Context-switchtime is overhead; the system doesno useful work while switching

The more complex the OS and the PCB  the longer the context switch

Time dependent on hardwaresupport

With multiple setsof registers, a context switch simply includes changing the pointerto the current register set.

With one set of registers, …

3进程操作3.1进程创建

3.3 进程操作

3.3.1进程创建

Parent process create
children processes
, which, in turn create other processes, forming atreeof processes

Generally, process identified and managed via aprocess identifier(pid)

Resource sharing options

Parent and children share all resources

Children share subset of parent’s resources

Parent and child share no resources

Address space options

Child duplicate of parent

Child has a program loaded into it

Execution options

Parent andchildren execute concurrently

Parentwaits until children terminate

3.3.1.2 Unix examples

fork() system call creates new process

内核为子进程做一个父进程的上下文的拷贝;

子进程与父进程共享子进程创建之前父进程所有的资源

父进程和子进程在不同的地址空间上运行;

Resource sharing

父子进程共享子进程创建之前的资源(继承);

Addressspace

Childduplicate of parent

Child has aprogram loaded into it(exec())

Execution

Parent and children execute concurrently

Parent waits until children terminate

UNIX examples

fork() system call creates new process

exec() system call used after a fork() to replace the process’ memory space with a new program

3.2.1.3Fork()系统调用

fork()的返回值

如果正确执行

对父进程,返回非0的正整数(子进程的进程号)

对子进程,返回0

如果出现错误

返回 -1

fork()的功能

内核为子进程做一个父进程上下文的拷贝;

复制父进程的PCB作为子进程的PCB

在新的地址空间中复制父进程的一个拷贝(有不同的实现)

父进程和子进程在不同的地址空间上运行;

Fork的要点

父子进程具有独立的内存空间

父子进程资源的共享与分离

父进程中在fork之前创建的变量—先继承,后分离

子进程继承了父进程的私有变量,作为自己的私有变量;

一般的私有变量

文件描述符(文件描述符变量分离,但对文件的操作与文件表项有关,因此他们并不完全独立)

fork之后各自创建的变量—完全分离

一般的私有变量

文件描述符(文件描述符变量分离,对文件的操作也完全独立)

Figure 5系统调用创建一个新的系统上下文

 3.3.2进程终止process termination  

3.3.2.1 exit 进程退出

Process executes last statement and asks the operating system to deleteit (exit)

Output data from child to parent(viawait)

Process’ resources aredeallocated by operating system

3.3.2.2 abort 进程中止

Parent may
terminate executionof children processes (abort)

Child has exceeded allocated resources

Task assigned to child is no longerrequired

If parentis exiting

Someoperating system do not allow child to continue if its parentterminates,if a process terminates, then allits children must also be terminated.

Allchildren terminated - cascading termination级联退出

UNIX – init

3.3.2.3 wait 系统调用

The parent process may wait fortermination of a child process by using thewait()systemcall.

The call returns status information andthe pid of the terminated process

      pid = wait(&status);

If no parentwaiting (did not invokewait())

process is a zombie

If parent terminatedwithout invoking wait

process is an orphan

3.4  IPC-InterprocessCommunication  

Mechanism for processes to communicateandtosynchronizetheir actions

 3.4.1 .1Cooperating Processes

Independentprocesscannotaffect or be affectedby the execution of another process

Cooperating processcanaffect or be affectedby the execution of another process

Advantages of process cooperation(team-work)

Informationsharing (e.g. producer-consumer problem (a shared memory),a sharedfile, etc. )

Computationspeed-up (beaking a task into subtasks, and the several subtasks executing inparallel)

Modularity(constracting a system in a modular fashing, dividing the system functions intoseperate processes or threads )

Convenience (one maywork on many tasks at the same time, eg. editing, printing, and compiling inparallel.)

3.4.1.2 fundament methods

Cooperative processes require an
interprocesscommunication(IPC) mechanism that will allow them to
exchange data and information.

Two fundament methods:

Shared memory

Message passing

(pipeline)

 

Figure 6 message passing(left) shared memory(right)

ory

3.4.2  Shared memory

e.g. Producer-Consumer Problem(Bounded-Buffer)

Paradigm for cooperating processes, producer process producesinformation that is consumed by aconsumer process

unbounded-bufferplaces nopractical limit on the size of the buffer

bounded-buffer assumesthat there is a fixed buffer size

 

Shared data

#defineBUFFER_SIZE 10

typedefstruct {

       . . .

}item;

itembuffer[BUFFER_SIZE];

intin = 0;

intout = 0;

Insert()Method

       while (true) {

/* Produce an item */

        while (((in = (in + 1) % BUFFER SIZEcount)  == out)

              ;   /* do nothing -- no free buffers */

          buffer[in] = item;

          in = (in + 1) % BUFFER SIZE;

     }

 

Remove()Method

while(true) {

          while (in == out)

                 ; // do nothing -- nothing toconsume

 

           // remove an item from the buffer

           item = buffer[out];

           out = (out + 1) % BUFFER SIZE;

       return item;

     }

 

Solutionis correct, but can only use BUFFER_SIZE-1 elements

3.4.3 Message-Passing System  

3.4.3.0 basics

Message system – processes communicate with each otherwithoutresorting toshared variables

IPC facilityprovides two operations:

send(message) – message size fixed or variable

receive(message)

If Pand Q wish to communicate, they need to:

establish a communicationlinkbetween them

exchangemessages via send/receive

Implementationof communication link

physical (e.g., shared memory, hardware bus, or network)

logical (e.g., logical properties)

3.4.3.2 Implementation Questions

How are links established?

Can a link be associated with more than two processes?

How many links can there be between every pair ofcommunicating processes?

What is the capacity of a link?

Is the size of a message that the link can accommodate fixedor variable?

Is a link unidirectional or bi-directional?

 

Two schemes

Direct Communication

如:信、纸条直接送到收信人手中

Indirect Communication

via Mailbox

For eitherschame, processes that want to communicate must have a way torefer to each other—naming

3.4.3.3 Naming--Direct Communication

Processes must name each other explicitly:

send (P, message) – send amessage toprocess P

receive(Q, message) – receivea message fromprocess Q

Properties of communication link

Links are established automatically

A link is associated with exactlyone pair of communicating processes

Between each pair there exists exactlyone link

The link may be unidirectional, but isusually
bi-directional

eg. system call -- kill(pid,SIGINT)

making a call

3.4.3.4 Naming--Indirect Communication

Basics

Messages are
directed and received from mailboxes(also referred to asports)

Each mailbox has a unique id

Processes can communicate only if they share a mailbox

Propertiesof communication link

Link established only if processes share a common mailbox

A link may be associated with many processes

Each pair of processes may share several communication links

Link may be unidirectionalor bi-directional

Operations

create a new mailbox

send and receive messages through mailbox

destroy a mailbox

Primitives are definedas:

send(A, message) – send a message tomailbox A

receive(A, message) – receive a message frommailbox A

eg. Email

Short Messaging Service (SMS)

(QQ, webchat ?)

Mailboxsharing

P1, P2, and P3 share mailbox A

P1, sends; P2 and P3 receive

Who gets the message?

Solutions

Allow a link to be associated with at most two processes

Allow only one process at a time to execute a receive operation

Allow the system to select arbitrarily the receiver.  Sender is notified who the receiver was.

 

Synchronization

Message passing may be eitherblockingornon-blocking

Blocking isconsidered synchronous

Blocking send has thesender
block until the message is received

Blocking receive has thereceiver
block until a message is available

Non-blocking isconsidered asynchronous

Non-blocking send has thesender send the message andcontinue

Non-blocking receive has thereceiver receive
a valid message or null

Buffering

Queue of messages attached to the link; implemented in one of threeways

1Zerocapacity – 0 messages

Sender must wait for receiver (rendezvous)

2.Boundedcapacity – finite length ofn messages

Sender must wait if link full

3.Unboundedcapacity – infinite length

Sender never waits

.6 Communication in Client-ServerSystem

3.6 Communication in Client-Server Systems

网络间通信

Sockets

Remote Procedure Calls

Remote Method Invocation (Java)

3.6.1 Sockets

A socket is defined as anendpoint for communication

Concatenation of IP addressand port

The socket 161.25.19.8:1625refers to port 1625 on host 161.25.19.8

Communication consistsbetween a pair of sockets

3.6.2 Remote procedure call

Remote procedure call (RPC)abstracts procedure calls between processes on networked systems.

 

Stubs – client-side proxy forthe actual procedure on the server.

 

The client-side stublocates the server and marshalls the parameters.

 

The server-side stubreceives this message, unpacks the marshalled parameters, and peforms theprocedure on the server.

3.6.3 RemoteMethod Invocation

Remote Method Invocation(RMI) is a Java mechanism similar to RPCs.

 

RMI allows a Java programon one machine to invoke a method on a remote object.

Marshalling Parameters

说明:

本文由giantpoplar发表于CSDN

文章地址 http://blog.csdn.net/giantpoplar/article/details/47700683

转载请保留本说明

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 07:53:15

操作系统学习笔记——进程的相关文章

操作系统学习笔记----进程/线程模型----Coursera课程笔记

操作系统学习笔记----进程/线程模型----Coursera课程笔记 进程/线程模型 0. 概述 0.1 进程模型 多道程序设计 进程的概念.进程控制块 进程状态及转换.进程队列 进程控制----进程创建.撤销.阻塞.唤醒.... 0.2 线程模型 为什么引入线程 线程的组成 线程机制的实现 用户级线程.核心级线程.混合方式 1. 进程的基本概念 1.1 多道程序设计 允许多个程序同时进入内存运行,目的是为了提高CPU系统效率 1.2 并发环境与并发程序 并发环境: 一段时间间隔内,单处理器上

操作系统学习笔记 进程

进程的定义 1. 一个正在执行中的程序: 2. 一个正在计算机上执行的程序实例: 3. 能够分配给处理器并由处理器执行的实体: 4. 一个具有以下特征的活动单元:一组指令序列的执行,一个当前状态和相关系统资源的集合: 由一组元素(包括两个基本元素:程序代码和代码相关的数据集)组成的实体,如果处理器开始执行该代码,这个执行实体就称为进程.在进程执行的任意一个时刻,都可以唯一的被表示为以下元素: 标识符:和进程相关的唯一标示,用以区分不同的进程: 状态:执行态,阻塞态,就绪态等: 优先级:相对于其他

计算机操作系统学习笔记_2_进程管理 --进程与线程(上)

h3.western { font-family: "Liberation Sans",sans-serif; }h3.cjk { font-family: "微软雅黑"; }h3.ctl { font-family: "AR PL UMing CN"; }h2.western { font-family: "Liberation Sans",sans-serif; font-size: 16pt; }h2.cjk { fon

计算机操作系统学习笔记_4_进程管理 --处理机调度

h3.western { font-family: "Liberation Sans",sans-serif; }h3.cjk { font-family: "微软雅黑"; }h3.ctl { font-family: "AR PL UMing CN"; }h1 { margin-bottom: 0.21cm; }h1.western { font-family: "Liberation Sans",sans-serif; f

Linux 操作系统学习笔记

一,unix 1.unix 特点 伸缩性强,开放性好, 2.基本原则 所有对象,硬件都是文件 配置数据以文本形式保存 短小的单目的程序构成 多个程序合作完成复杂任务 3.gnu 基本原则是共享,建立自由开放的unix系统 1984年 richard stallman 发起 基本体系是micro kernel 4.gpl Copyleft 原作者所有权 5.linux起源 Linustorvalds, 自由的类unix操作系统, 遵循gnu和gpl 6.linux 可以实现unix功能 遵循开源许

计算机操作系统学习笔记_1_操作系统概述

操作系统概述 一.操作系统的概念.特征.功能和提供的服务 1.操作系统的概念     操作系统是计算机系统中最重要.最基本的系统软件,操作系统位于硬件和用户程序之间.    对于用户来讲:它能向用户提供使用计算机的接口;    从资源管理角度来看:它能管理计算机软硬件资源,提高其利用率;    再者,利用虚拟机技术(如WMWare,VirtualBox,Java虚拟机等),扩展了计算机的功能和使用范围.     因此,操作系统的定义为:操作系统是控制和管理计算机软.硬件资源,以尽可能合理.高效的

Linux 程序设计学习笔记----进程管理与程序开发(上)

转载请注明出处,http://blog.csdn.net/suool/article/details/38406211,谢谢! Linux进程存储结构和进程结构 可执行文件结构 如下图: 可以看出,此ELF可执行文件存储时(没有调入内存)分为代码区.数据区和未出花数据区三部分. 代码区:存放cpu的执行的机器指令. 数据区:包含程序中的已经初始化的静态变量,以及已经初始化的全局变量. 未初始化数据区:存入的是未初始化的全局变量和未初始化的静态变量. 现在在上面的程序代码中增加一个int的静态变量

Linux 程序设计学习笔记----进程管理与程序开发(下)

转载请注明出处:http://blog.csdn.net/suool/article/details/38419983,谢谢! 进程管理及其控制 创建进程 fork()函数 函数说明具体参见:http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html 返回值:Upon successful completion, fork() shall return 0 to the child process and shall re

RTX51 Tiny实时操作系统学习笔记—初识RTX51 Tiny

 一,RTX51 Tiny简单介绍 RTX51 Tiny是一种实时操作系统(RTOS),能够用它来建立多个任务(函数)同一时候运行的应用(从宏观上看是同一时候运行的,但从微观上看,还是独立运行的).嵌入式应用系统常常有这样的需求.RTOS能够提供调度.维护.同步等功能. 实时操作系统能灵活的调度系统资源,像CPU和存储器,而且提供任务间的通信.RTX51 Tiny是一个功能强大的RTOS,且易于使用,它用于8051系列的微控制器.该RTOS最多支持16个任务,基于RTX51 Tiny构建的应用程