本文是本人操作系统课程的笔记,因为是所谓的“双语授课”,所以笔记也有些中英夹杂。
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进程的特性
进程与程序是截然不同的两个概念;
一个程序可以对应多个进程;
一个进程也可以由多个程序段共同完成一项任务;(接力)
进程具有五个特征,而程序则不具备;
一个程序不能两次属于同一个进程
动态性,并发性,独立性,异步性,结构特征。
是进程的最基本的特征,表现在进程由创建而产生,由调度而执行,因得不到资源而暂停执行,由撤销而消亡;
进程具有一定的生命期;而程序只是一组指令的集合,并存放在某种介质上,并无运动的含义,因此程序是个静态实体
多个进程实体同存于内存中,能在一段时间内同时执行;
引入进程的目的正是为了使其程序能和其它进程的程序并发执行,而程序是不能并发的;
进程实体是一个能独立运行的基本单位,同时也是系统中独立获得资源和独立调度的基本单位;
凡未建立进程的程序,都不能作为一个独立的单位参加运行;
指进程按各自独立的、不可预知的速度向前推进;
或者说,进程按异步方式运行;
OS中必须采取措施保证各程序之间能协调运行;
从结构上看,进程实体由程序段、数据段以及进程控制块组成;
这三部分也称为进程影像;
进程执行时,它的状态会改变。
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
Only one progress can be running on any processor at any instant
Figure 2进程5状态图
Figure 3进程7状态图
suspend (挂起)
active->static
activate(激活)
static ->active
blocked(waiting,sleeping)(阻塞、等待、睡眠)
running->waiting
wakeup (唤醒)
waiting ->ready
concurrent 并发 时间段
parallel 并行 时刻
3.1.5 PCB
系统为了管理进程设置的一个专门的数据结构,用它来记录进程的外部特征,描述进程的运动变化过程.
PCB是进程存在的唯一标志;
操作系统通过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
|
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
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, …
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
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
如果正确执行
对父进程,返回非0的正整数(子进程的进程号)
对子进程,返回0
如果出现错误
返回 -1
内核为子进程做一个父进程上下文的拷贝;
复制父进程的PCB作为子进程的PCB
在新的地址空间中复制父进程的一个拷贝(有不同的实现)
父进程和子进程在不同的地址空间上运行;
父子进程具有独立的内存空间
父子进程资源的共享与分离
父进程中在fork之前创建的变量—先继承,后分离
子进程继承了父进程的私有变量,作为自己的私有变量;
一般的私有变量
文件描述符(文件描述符变量分离,但对文件的操作与文件表项有关,因此他们并不完全独立)
fork之后各自创建的变量—完全分离
一般的私有变量
文件描述符(文件描述符变量分离,对文件的操作也完全独立)
Figure 5系统调用创建一个新的系统上下文
3.3.2进程终止process termination
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
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
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.)
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)
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)
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
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
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
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.
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
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
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.
说明:
本文由giantpoplar发表于CSDN文章地址 http://blog.csdn.net/giantpoplar/article/details/47700683
转载请保留本说明
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/giantpoplar/article/details/47700683