《操作系统概念(第七版_英文版)ch3.ppt》由会员分享,可在线阅读,更多相关《操作系统概念(第七版_英文版)ch3.ppt(45页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Chapter 3:Processes3.2Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Chapter 3:ProcessesnProcess ConceptnProcess SchedulingnOperations on ProcessesnCooperating ProcessesnInterprocess CommunicationnCommunication in Client-Server Systems3.3Silberschatz,Galvin and Gag
2、ne 2005Operating System Concepts-7th Edition,Feb 7,2006Process ConceptnAn operating system executes a variety of programs:lBatch system jobslTime-shared systems user programs or tasksnTextbook uses the terms job and process almost interchangeablynProcess a program in execution;process execution must
3、 progress in sequential fashionnA process includes:lprogram counter lstackldata sectionlheap3.5Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Process StatenAs a process executes,it changes statelnew:The process is being createdlrunning:Instructions are being execut
4、edlwaiting:The process is waiting for some event to occurlready:The process is waiting to be assigned to a processorlterminated:The process has finished execution3.7Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Process Control Block(PCB)Information associated with
5、 each processnProcess statenProgram counternCPU registersnCPU scheduling informationnMemory-management informationnAccounting informationnI/O status informationpointer3.9Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006CPU Switch From Process to Process3.10Silberscha
6、tz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Process Scheduling QueuesnJob queue set of all processes in the systemnReady queue set of all processes residing in main memory,ready and waiting to executenDevice queues set of processes waiting for an I/O devicenProcesses migr
7、ate among the various queues3.12Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Representation of Process Scheduling3.13Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006SchedulersnLong-term scheduler (or job scheduler)selects which p
8、rocesses should be brought into the ready queuenShort-term scheduler (or CPU scheduler)selects which process should be executed next and allocates CPU3.14Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Schedulers(Cont.)nShort-term scheduler is invoked very frequentl
9、y(milliseconds)(must be fast)nLong-term scheduler is invoked very infrequently(seconds,minutes)(may be slow)nThe long-term scheduler controls the degree of multiprogrammingnProcesses can be described as either:lI/O-bound process spends more time doing I/O than computations,many short CPU burstslCPU-
10、bound process spends more time doing computations;few very long CPU bursts3.15Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Addition of Medium Term Scheduling3.16Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Context SwitchnThe C
11、ontext is presented in PCBnWhen CPU switches to another process,the system must save the state of the old process(state save)and load the saved state for the new process(state restore)nContext-switch time is overhead;the system does no useful work while switchingnTime dependent on hardware support3.
12、17Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Process CreationnParent process create children processes,which,in turn create other processes,forming a tree of processesnResource sharinglParent and children share all resourceslChildren share subset of parents res
13、ourceslParent and child share no resourcesnExecutionlParent and children execute concurrentlylParent waits until children terminate3.18Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006A tree of processes on a typical Solaris3.19Silberschatz,Galvin and Gagne 2005Opera
14、ting System Concepts-7th Edition,Feb 7,2006Process Creation(Cont.)nAddress spacelChild duplicate of parentlChild has a program loaded into itnUNIX exampleslfork system call creates new processlexec system call used after a fork to replace the process memory space with a new programReturn child proce
15、sss PIDReturn 03.21Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006C Program Forking Separate Processint main()pid_t pid;/*fork another process*/pid=fork();if(pid 0)/*error occurred*/fprintf(stderr,Fork Failed);exit(-1);else if(pid=0)/*child process*/execlp(/bin/ls,
16、ls,NULL);else /*parent process*/*parent will wait for the child to complete*/wait(NULL);printf(Child Complete);exit(0);3.22Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Process TerminationnProcess executes last statement and asks the operating system to delete it(
17、exit)lOutput data from child to parent(via wait)lProcess resources are deallocated by operating systemnParent may terminate execution of children processes(abort)lChild has exceeded allocated resourceslTask assigned to child is no longer requiredlIf parent is exiting4Some operating system do not all
18、ow child to continue if its parent terminatesAll children terminated-cascading termination3.23Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Cooperating ProcessesnIndependent process cannot affect or be affected by the execution of another processnCooperating proce
19、ss can affect or be affected by the execution of another processnAdvantages of process cooperationlInformation sharing lComputation speed-uplModularitylConvenience3.24Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Interprocess Communications Communications Models:C
20、ommunications Models:a)message passing b)Shared memory a)message passing b)Shared memory3.25Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Producer-Consumer ProblemnParadigm for cooperating processes,producer process produces information that is consumed by a consu
21、mer processlunbounded-buffer places no practical limit on the size of the bufferlbounded-buffer assumes that there is a fixed buffer sizeConsumerProducerBuffer3.26Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Bounded-Buffer Shared-Memory SolutionBounded-Buffer Sha
22、red-Memory SolutionnShared data#define BUFFER_SIZE 10typedef struct.item;item bufferBUFFER_SIZE;int in=0;int out=0;nSolution is correct,but can only use BUFFER_SIZE-1 elementsin outBuffer is EmptyBuffer is Fullinout3.27Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,200
23、6Bounded-Buffer Insert()Methodwhile(true)/*Produce an item*/while(in+1)%BUFFER_SIZE)=out);/*do nothing-no free buffers*/bufferin=item;in=(in+1)%BUFFER_SIZE;Insert()inout3.28Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Bounded Buffer Remove()Methodwhile(true)while
24、(in=out);/do nothing-nothing to consume /remove an item from the buffer item=bufferout;out=(out+1)%BUFFER SIZE;return item;Remove()inout3.29Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Massage PassingnMechanism for processes to communicate and to synchronize thei
25、r actionsnMessage system processes communicate with each other without resorting to shared variablesnIPC facility provides two operations:lsend(message)message size fixed or variable lreceive(message)nIf P and Q wish to communicate,they need to:lestablish a communication link between themlexchange m
26、essages via send/receivenImplementation of communication linklphysical(e.g.,shared memory,hardware bus)llogical(e.g.,logical properties)3.30Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Implementation QuestionsnHow are links established?nCan a link be associated w
27、ith more than two processes?nHow many links can there be between every pair of communicating processes?nWhat is the capacity of a link?nIs the size of a message that the link can accommodate fixed or variable?nIs a link unidirectional or bi-directional?3.31Silberschatz,Galvin and Gagne 2005Operating
28、 System Concepts-7th Edition,Feb 7,2006Direct CommunicationnProcesses must name each other explicitly:lsend(P,message)send a message to process Plreceive(Q,message)receive a message from process QnProperties of communication linklLinks are established automaticallylA link is associated with exactly
29、one pair of communicating processeslBetween each pair there exists exactly one linklThe link may be unidirectional,but is usually bi-directionalP2P1P33.32Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Indirect CommunicationnPrimitives are defined as:send(M,message)
30、send a message to mailbox Mreceive(M,message)receive a message from mailbox MnMessages are directed and received from mailboxes(also referred to as ports)lEach mailbox has a unique idlProcesses can communicate only if they share a mailboxnOperationslcreate a new mailboxlsend and receive messages thr
31、ough mailboxldestroy a mailbox3.33Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006Indirect CommunicationnProperties of communication linklLink established only if processes share a common mailboxlA link may be associated with many processeslEach pair of processes ma
32、y share several communication linkslLink may be unidirectional or bi-directionalnMailbox sharinglP1,P2,and P3 share mailbox M1lP1,sends;P2 and P3 receivelWho gets the message?nSolutionslAllow a link to be associated with at most two processeslAllow only one process at a time to execute a receive ope
33、rationlAllow the system to select arbitrarily the receiver.Sender is notified who the receiver was.P2P1P3M1M23.35Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006SynchronizationnMessage passing may be either blocking or non-blockingnBlocking is considered synchronous
34、lBlocking send has the sender block until the message is receivedlBlocking receive has the receiver block until a message is availablenNon-blocking is considered asynchronouslNon-blocking send has the sender send the message and continuelNon-blocking receive has the receiver receive a valid message
35、or null3.36Silberschatz,Galvin and Gagne 2005Operating System Concepts-7th Edition,Feb 7,2006BufferingnQueue of messages attached to the link;implemented in one of three ways1.Zero capacity 0 messagesSender must wait for receiver(rendezvous)2.Bounded capacity finite length of n messagesSender must wait if link full3.Unbounded capacity infinite length Sender never waitsEnd of Chapter 3