Cooperating Processes

3,511 views

Published on

Published in: Business, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,511
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
59
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Cooperating Processes

  1. 1. Chapter 4: Processes <ul><li>Process Concept </li></ul><ul><li>Process Scheduling </li></ul><ul><li>Operations on Processes </li></ul><ul><li>Cooperating Processes </li></ul><ul><li>Interprocess Communication </li></ul><ul><li>Communication in Client-Server Systems </li></ul>
  2. 2. Process Concept <ul><li>An operating system executes a variety of programs: </li></ul><ul><ul><li>Batch system – jobs </li></ul></ul><ul><ul><li>Time-shared systems – user programs or tasks </li></ul></ul><ul><li>Textbook uses the terms job and process almost interchangeably. </li></ul><ul><li>Process </li></ul><ul><ul><li>a program in execution; </li></ul></ul><ul><ul><li>process execution must progress in sequential fashion. </li></ul></ul><ul><li>A process includes: </li></ul><ul><ul><li>program counter 를 포함한 문맥 일체 </li></ul></ul><ul><ul><li>stack </li></ul></ul><ul><ul><li>data section (initialized / uninitialized) </li></ul></ul><ul><ul><li>code section (text section) </li></ul></ul>
  3. 3. Process State <ul><li>As a process executes, it changes state </li></ul><ul><ul><li>new : The process is being created. </li></ul></ul><ul><ul><li>running : Instructions are being executed. </li></ul></ul><ul><ul><li>waiting : The process is waiting for some event to occur. </li></ul></ul><ul><ul><li>ready : The process is waiting to be assigned to a process. </li></ul></ul><ul><ul><li>terminated : The process has finished execution. </li></ul></ul>
  4. 4. Diagram of Process State
  5. 5. Process Control Block (PCB) <ul><li>Information associated with each process. </li></ul><ul><ul><li>Process state </li></ul></ul><ul><ul><li>Program counter </li></ul></ul><ul><ul><li>CPU registers </li></ul></ul><ul><ul><li>CPU scheduling information </li></ul></ul><ul><ul><li>Memory-management information </li></ul></ul><ul><ul><li>Accounting information </li></ul></ul><ul><ul><li>I/O status information </li></ul></ul>
  6. 6. Process Control Block (PCB)
  7. 7. CPU Switch From Process to Process
  8. 8. Process Scheduling Queues <ul><li>Job queue </li></ul><ul><ul><li>set of all processes in the system. </li></ul></ul><ul><li>Ready queue </li></ul><ul><ul><li>set of all processes residing in main memory, ready and waiting to execute. </li></ul></ul><ul><li>Device queues </li></ul><ul><ul><li>set of processes waiting for an I/O device. </li></ul></ul><ul><li>Process migration between the various queues. </li></ul>
  9. 9. Ready Queue And Various I/O Device Queues
  10. 10. Representation of Process Scheduling
  11. 11. Schedulers <ul><li>Long-term scheduler (or job scheduler) </li></ul><ul><ul><li>selects which processes should be brought into the ready queue . </li></ul></ul><ul><li>Short-term scheduler (or CPU scheduler) </li></ul><ul><ul><li>selects which process should be executed next and allocates CPU . </li></ul></ul>
  12. 12. Addition of Medium Term Scheduling
  13. 13. Schedulers (Cont.) <ul><li>Short-term scheduler is invoked very frequently (milliseconds)  (must be fast). </li></ul><ul><li>Long-term scheduler is invoked very infrequently (seconds, minutes)  (may be slow). </li></ul><ul><li>The long-term scheduler controls the degree of multiprogramming. </li></ul><ul><li>Processes can be described as either: </li></ul><ul><ul><li>I/O- bound process – spends more time doing I/O than computations, many short CPU bursts. </li></ul></ul><ul><ul><li>CPU - bound process – spends more time doing computations; few very long CPU bursts. </li></ul></ul>
  14. 14. Context Switch <ul><li>When CPU switches to another process, </li></ul><ul><ul><li>the system must save the state of the old process and </li></ul></ul><ul><ul><li>load the saved state for the new process. </li></ul></ul><ul><li>Context-switch time is overhead; </li></ul><ul><ul><li>the system does no useful work while switching. </li></ul></ul><ul><li>Time dependent on hardware support. </li></ul>
  15. 15. Process Creation <ul><li>Forming a tree of processes </li></ul><ul><ul><li>Parent process create children processes, which, in turn create other processes, </li></ul></ul><ul><li>Resource sharing </li></ul><ul><ul><li>Parent and children share all resources. </li></ul></ul><ul><ul><li>Children share subset of parent’s resources. </li></ul></ul><ul><ul><li>Parent and child share no resources. </li></ul></ul><ul><li>Execution </li></ul><ul><ul><li>Parent and children execute concurrently. </li></ul></ul><ul><ul><li>Parent waits until children terminate. </li></ul></ul>
  16. 16. Process Creation (Cont.) <ul><li>Address space </li></ul><ul><ul><li>Child duplicate of parent. </li></ul></ul><ul><ul><li>Child has a program loaded into it. </li></ul></ul><ul><li>UNIX examples </li></ul><ul><ul><li>fork system call creates new process </li></ul></ul><ul><ul><li>exec system call used after a fork to replace the process’ memory space with a new program. </li></ul></ul>
  17. 17. Processes Tree on a UNIX System
  18. 18. Process Termination <ul><li>Process executes last statement and asks the operating system to decide it ( exit ). </li></ul><ul><ul><li>Output data from child to parent (via wait ). </li></ul></ul><ul><ul><li>Process’ resources are deallocated by operating system. </li></ul></ul><ul><li>Parent may terminate execution of children processes ( abort ). </li></ul><ul><ul><li>Child has exceeded allocated resources. </li></ul></ul><ul><ul><li>Task assigned to child is no longer required. </li></ul></ul><ul><ul><li>Parent is exiting. </li></ul></ul><ul><ul><ul><li>Operating system does not allow child to continue if its parent terminates. </li></ul></ul></ul><ul><ul><ul><li>Cascading termination. </li></ul></ul></ul>
  19. 19. Cooperating Processes <ul><li>Independent process cannot affect or be affected by the execution of another process. </li></ul><ul><li>Cooperating process can affect or be affected by the execution of another process </li></ul><ul><li>Advantages of process cooperation </li></ul><ul><ul><li>Information sharing </li></ul></ul><ul><ul><li>Computation speed-up </li></ul></ul><ul><ul><li>Modularity </li></ul></ul><ul><ul><li>Convenience </li></ul></ul>
  20. 20. Producer-Consumer Problem <ul><li>Paradigm for cooperating processes </li></ul><ul><ul><li>producer process produces information that is consumed by a consumer process. </li></ul></ul><ul><ul><li>unbounded-buffer places no practical limit on the size of the buffer. </li></ul></ul><ul><ul><li>bounded-buffer assumes that there is a fixed buffer size. </li></ul></ul>
  21. 21. Bounded-Buffer – Shared-Memory Solution <ul><li>Shared data </li></ul><ul><ul><ul><ul><li>#define BUFFER_SIZE 10 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Typedef struct { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>. . . </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} item; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>item buffer[BUFFER_SIZE]; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int in = 0; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int out = 0; </li></ul></ul></ul></ul><ul><li>Solution is correct, but can only use BUFFER_SIZE-1 elements </li></ul>
  22. 22. Bounded-Buffer – Producer Process <ul><li>item nextProduced; </li></ul><ul><li>while (1) { </li></ul><ul><li>while (((in + 1) % BUFFER_SIZE) == out) </li></ul><ul><li>; /* do nothing */ </li></ul><ul><li>buffer[in] = nextProduced; </li></ul><ul><li>in = (in + 1) % BUFFER_SIZE; </li></ul><ul><li>} </li></ul>
  23. 23. Bounded-Buffer – Consumer Process <ul><li>item nextConsumed; </li></ul><ul><li>while (1) { </li></ul><ul><li>while (in == out) </li></ul><ul><li>; /* do nothing */ </li></ul><ul><li>nextConsumed = buffer[out]; </li></ul><ul><li>out = (out + 1) % BUFFER_SIZE; </li></ul><ul><li>} </li></ul>
  24. 24. Interprocess Communication (IPC) <ul><li>프로세스들이 서로 통신 할 수 있는 방법을 제공함과 동시에 서로 동기화 할 수 있는 메커니즘을 제공 </li></ul><ul><li>Messaging system – processes communicate with each other without resorting to shared variables. </li></ul><ul><li>IPC facility provides two operations: </li></ul><ul><ul><li>send ( message ) – message size fixed or variable </li></ul></ul><ul><ul><li>receive ( message ) </li></ul></ul><ul><li>If P and Q wish to communicate, they need to: </li></ul><ul><ul><li>establish a communication link between them </li></ul></ul><ul><ul><li>exchange messages via send/receive </li></ul></ul><ul><li>Implementation of communication link </li></ul><ul><ul><li>physical (e.g., shared memory, hardware bus) </li></ul></ul><ul><ul><li>logical (e.g., logical properties) </li></ul></ul>
  25. 25. Implementation Questions <ul><li>링크 설정 방법은 ? </li></ul><ul><li>링크 하나에 여러 프로세스가 동시에 붙는 것을 허용할 것인가 ? </li></ul><ul><li>통신하는 프로세스 간에 얼마나 많은 수의 링크를 허용할 것인가 ? </li></ul><ul><li>링크 하나의 최대 bandwidth 는 얼마인가 ? </li></ul><ul><li>링크를 통해 주고 받을 패킷 하나의 최대 크기를 얼마로 할 것인가 ? 가변 / 고정 크기 ? </li></ul><ul><li>링크 속성이 unidirectional 인가 bi-directional 인가 ? </li></ul>
  26. 26. Direct Communication <ul><li>Processes must name each other explicitly: </li></ul><ul><ul><li>send(P, message) – send a message to process P </li></ul></ul><ul><ul><li>receive(Q, message) – receive a message from process Q </li></ul></ul><ul><li>직접 통신 링크의 속성 </li></ul><ul><ul><li>링크는 자동으로 설정되어야 한다 . </li></ul></ul><ul><ul><li>한 링크의 양단에는 반드시 하나씩의 통신 프로세스만이 관련되어 있어야 한다 . </li></ul></ul><ul><ul><li>통신하는 두 프로세스 사이에는 반드시 하나의 링크만이 존재해야 한다 . </li></ul></ul><ul><ul><li>링크는 단방향일 수도 있지만 대개의 경우 양방향인 것이 바람직하다 . </li></ul></ul>
  27. 27. Indirect Communication <ul><li>Messages are directed and received from mailboxes (also referred to as ports ). </li></ul><ul><ul><li>Each mailbox has a unique id( 포트 번호 ). </li></ul></ul><ul><ul><li>Processes can communicate only if they share a mailbox. </li></ul></ul><ul><li>간접 통신 링크의 속성 </li></ul><ul><ul><li>프로세스들이 하나의 공통 메일박스를 공유할 때에만 링크가 설정되어야 한다 . </li></ul></ul><ul><ul><li>하나의 링크에는 여러 개의 프로세스들이 연관될 수 있다 . </li></ul></ul><ul><ul><li>통신하는 양단 프로세스들은 여러 개의 통신 링크를 동시에 공유할 수 있다 . </li></ul></ul><ul><ul><li>링크는 단방향 / 양방향 어느 것이라도 무방함 . </li></ul></ul>
  28. 28. Indirect Communication (cont’d) <ul><li>Operations </li></ul><ul><ul><li>create a new mailbox </li></ul></ul><ul><ul><li>send and receive messages through mailbox </li></ul></ul><ul><ul><li>destroy a mailbox </li></ul></ul><ul><li>Primitives are defined as: </li></ul><ul><li>send(A, message) – send a message to mailbox A </li></ul><ul><li>receive(A, message) – receive a message from mailbox A </li></ul>
  29. 29. Indirect Communication(cont’d) <ul><li>Mailbox sharing </li></ul><ul><ul><li>P 1 , P 2 , and P 3 share mailbox A. </li></ul></ul><ul><ul><li>P 1 , sends; P 2 and P 3 receive. </li></ul></ul><ul><ul><li>Who gets the message? </li></ul></ul><ul><li>해결책 </li></ul><ul><ul><li>한 링크에 많아야 두 개의 프로세스만이 연관될 수 있도록 제한한다 . </li></ul></ul><ul><ul><li>한 번에 한 프로세스만이 receive 동작을 실행할 수 있도록 제한한다 . </li></ul></ul><ul><ul><li>시스템으로 하여금 어떤 프로세스가 수신할 지를 마음대로 결정하도록 한다 . </li></ul></ul><ul><ul><ul><li>다만 누가 수신했는지 송신 프로세스에게 통보는 해준다 . </li></ul></ul></ul>
  30. 30. 동기 / 비동기 통신 <ul><li>Message passing may be either blocking or non-blocking . </li></ul><ul><li>Blocking is considered synchronous, </li></ul><ul><li>Non-blocking is considered asynchronous. </li></ul><ul><li>send and receive primitives may be either blocking or non-blocking. </li></ul>
  31. 31. 통신 버퍼링 모드의 종류 <ul><li>Queue of messages attached to the link; implemented in one of three ways. </li></ul><ul><ul><li>Zero capacity – 0 messages </li></ul></ul><ul><ul><ul><li>Sender must wait for receiver (rendezvous). </li></ul></ul></ul><ul><ul><li>Bounded capacity – finite length of n messages </li></ul></ul><ul><ul><ul><li>Sender must wait if link full. </li></ul></ul></ul><ul><ul><li>Unbounded capacity – infinite length </li></ul></ul><ul><ul><ul><li>Sender never waits. </li></ul></ul></ul>
  32. 32. Client-Server Communication <ul><li>Sockets </li></ul><ul><li>Remote Procedure Calls </li></ul><ul><li>Remote Method Invocation (Java) </li></ul>
  33. 33. Sockets <ul><li>A socket is defined as an endpoint for communication . </li></ul><ul><li>Concatenation of IP address and port </li></ul><ul><ul><li>The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 </li></ul></ul><ul><li>Communication consists of a pair of sockets: source and destination sockets . </li></ul>
  34. 34. Socket Communication
  35. 35. Remote Procedure Calls <ul><li>Remote procedure call (RPC) 은 네트워크 시스템에서 프로세스 간 함수 호출 메커니즘을 추상화해준다 . </li></ul><ul><li>Stubs </li></ul><ul><ul><li>client/server-side proxy for the actual procedure on the server. </li></ul></ul><ul><li>The client-side stub </li></ul><ul><ul><li>locates the server and marshalls the parameters. </li></ul></ul><ul><li>The server-side stub </li></ul><ul><ul><li>receives this message, </li></ul></ul><ul><ul><li>unpacks the marshalled parameters, and </li></ul></ul><ul><ul><li>performs the procedure on the server. </li></ul></ul>
  36. 36. Execution of RPC
  37. 37. Remote Method Invocation <ul><li>Remote Method Invocation (RMI) is a Java mechanism similar to RPCs. </li></ul><ul><li>RMI allows a Java program on one machine to invoke a method on a remote object. </li></ul>
  38. 38. Marshalling Parameters

×