SOCSAMS e-learning INTERPROCESS COMMUNICATION AND SYNCHRONIZATION Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Concurrency - Basic Concept In a multi-programming environment, there will be concurrent processes which are of two types: • Operating system processes (those that execute system code) • User processes (those that execute user’s code) Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Concurrency - Basic Concept A simple batch operating system can be viewed as 3 processes : • a reader process • an executor process Input Buffer Process • a printer process Output Buffer Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication Basic Concepts of IPC & Synchronization: In order processes to cooperate, must concurrently communicate and executing synchronize. Interprocess communication is based on the use of shared variables (variables that can be referenced by more than one process) or message passing. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication Basic Concepts of IPC & Synchronization: Synchronization is often necessary when processes communicate. Processes are executed with unpredictable speed. Yet to communicate one process must perform some action such as setting the value of a variable or sending a message that the other detects. This only works if the events perform an action or detect an action are constrained to happen in that order. Thus one can view synchronization as a set of constraints on the ordering of events. The programmer employs a synchronization mechanism to delay execution of a process in order to satisfy such constraints. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication IPC is a capability supported by operating system that allows one process to communicate with another process. The processes can be running on the same computer or on different computers connected through a network. IPC enables one application to control another application, and for several applications to share the same data without interfering with one another. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication • Critical Resource • Critical Section • Mutual Exclusion Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication • Critical Resource It is a resource shared with constraints on its use(e.g., memory, files, printers, etc) • Critical Section • Mutual Exclusion Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication • Critical Resource • Critical Section It is code that accesses a critical resource. • Mutual Exclusion Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication • Critical Resource • Critical Section • Mutual Exclusion At most one process may be executing a critical section with respect to a particular critical resource simultaneously. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Inter-Process Communication IPC can be possible in two different ways: • Shared-Memory System • Message-Passing System Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Shared-Memory System Shared-memory system require communication processes to share some variables. The processes are expected to exchange information through the use of these shared variables. Responsibility for providing communication rests with the application programmer. The OS only needs to provide shared memory. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Message-Passing System MPS allow communication processes to exchange messages – responsibility for providing communication rest with OS. The function of a MPS is to allow processes to communicate with each other without the need to resort to shared variables. An IPC facility basically provides two operations: • send(message) • receive(message) Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Message-Passing System MPS allow communication processes to exchange messages – responsibility for providing communication rest with OS. The function of a MPS is to allow processes to communicate with each other without the need to resort to shared variables. An IPC facility basically provides two operations: • send(message) • receive(message) In order to send and to receive messages, a communication link must exist between the two involved processes. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Message-Passing System Methods for logically implementing a communication link and the send/receive operations are classified into: • Naming • Buffering Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Message-Passing System Methods for logically implementing a communication link and the send/receive operations are classified into: • Naming • Buffering Consisting of direct and indirect communication. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Message-Passing System Methods for logically implementing a communication link and the send/receive operations are classified into: • Naming • Buffering Consisting of capacity message properties. and Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Direct Communication Each process that wants to communicate must explicitly name the recipient or sender of the communication. In this scheme the send and receive primitives are: • send(P, message) – send a message to process P • receive(Q, message) – receive a message from process Q Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Direct Communication Each process that wants to communicate must explicitly name the recipient or sender of the communication. In this scheme the send and receive primitives are: • send(P, message) – send a message to process P • receive(Q, message) – receive a message from process Q Here a link is established automatically between every pair of processes that want to communicate. Exactly one link exists between each pair of processes. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Direct Communication Symmetry in addressing • send(P, message) – send a message to process P • receive(Q, message) – receive a message from process Q This scheme shows the symmetry in addressing; that is both the sender and the receiver processes must name the other to communicate. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Direct Communication Asymmetry in addressing • send(P, message) – send a message to process P • receive(id, message) – receive a message from any process; the variable id is set to the name of the process with which communication has taken place. Only the sender names the recipient; the recipient is not required to name the sender. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Indirect Communication With indirect communication, the message are sent to and receive from a mailbox. It is an object into which messages may be placed and from which messages may be removed by processes. Each mailbox owns a unique identification. A process may communicate with some other process by a number of different mailboxes. • send(A, message) – send a message to mailbox A • receive(A, message) – receive a message from mailbox A Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Indirect Communication Mailbox owned by process: Mailboxes may be owned by either by a process or by the system. If the mailbox is owned by a process, then the owner who can only receive from this mailbox and the user who can only send message to the mailbox are to be distinguished. When a process that owns a mailbox terminates, its mailbox disappears. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Indirect Communication Mailbox owned by the OS: It has an existence of its own, i.e., it is independent and not attached to any particular process. The OS provides a mechanism that allows a process to: • create a new mailbox • send and receive message through the mailbox • destroy a mailbox Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Buffering Whether the communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue. This queue can be implemented in three ways: • Zero capacity • Bounded capacity • Unbounded capacity Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Buffering - Capacity Zero capacity The queue has maximum length 0; thus, the link cannot have any messages waiting in it. In this case, the sender must block until the recipient receives the message. The zero-capacity link is referred to as a message-passing system with no buffering. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Buffering - Capacity Bounded capacity The queue has finite length n; thus, at most n messages can reside in it. If a new message is sent, and the queue is not full, it is placed in the queue either by copying the message or by keeping a pointer to the message and the sender can continue execution without waiting. If the link is full, the sender must block until space is available in the queue. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Buffering - Capacity Unbounded capacity The queue has potentially infinite length; thus, any number of messages can wait in it. The sender never blocks. Bounded and Unbounded capacity link is referred to as message-passing system with automatic buffering. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Interprocess Synchronization Race Condition Process A 4 5 6 . . abc Prog.c Prog.n next file to be printed Out = 4 In = 7 7 Process B . . next free slot in the directory Value depends on which of the processes wins the race to update the variable. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Interprocess Synchronization Serialization Make an operating system not to perform several tasks in parallel. Two strategies to serializing processes in a multitasking environment: • The Scheduler can be disabled • A Protocol can be introduced Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Interprocess Synchronization The Scheduler can be disabled • Scheduler can be disabled for a short period of time, to prevent control being given to another process during a critical action like modifying shared data. • Inefficient on multiprocessor machines, since all other processors have to be halted every time one wishes to execute a critical section. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Interprocess Synchronization A Protocol can be introduced • A protocol can be introduced which all programs sharing data must obey. The protocol ensures that processes have to queue up to gain access to shared data. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical Section Do { entry section critical section exit section remainder section } while(1); General structure of a typical process Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical Section Do { entry section critical section exit section Section of code that request permission to enter its critical section. remainder section } while(1); General structure of a typical process Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical Section Do { entry section critical section exit section It is a part of code in which it is necessary to have exclusive access to shared data. remainder section } while(1); General structure of a typical process Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical Section Do { entry section critical section Code for tasks just after exiting from the critical section. exit section remainder section } while(1); General structure of a typical process Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical Section Do { entry section critical section The remaining code. exit section remainder section } while(1); General structure of a typical process Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Mutexes : Mutual Exclusion var P1busy, P2busy : boolean; {parent process} P1busy:=false; P2busy:=false; initiate P1, P2; end; {mutex} Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Mutexes : Mutual Exclusion var P1busy, P2busy : boolean; process P1 begin while true do begin P1busy := true; while P2busy do {keep testing}; critical-section; P1busy:=false; other_P1busy_processing; end{while} End; {P1} {parent process} P1busy:=false; P2busy:=false; initiate P1, P2; end; {mutex} Dept. of Computer Applications, MES College Marampally process P2 SOCSAMS e-learning begin while true do begin P2busy := true; while P1busy do {keep testing}; critical-section; P2busy:=false; other_P2busy_processing; end{while} End; {P2} Mutexes : Mutual Exclusion var P1busy, P2busy : boolean; process P1 begin while true do begin P1busy := true; while P2busy do {keep testing}; critical-section; P1busy:=false; other_P1busy_processing; end{while} End; {P1} {parent process} P1busy:=false; P2busy:=false; initiate P1, P2; end; {mutex} Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Critical-Section Problem boolean flag[2]; int turn; do { flag[i] = true; turn = j; while (flag[j] && turn == j); Critical-section flag[i] = false; remainder-section } while(1); Initially flag[0]=flag[1]=false Turn = 0 or 1. Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Bakery Algorithm boolean choosing[n]; int number[n]; do { choosing[i] = true; number[i] = max(number[0], number[1], …, number[n-1]) + 1; choosing[i] = flase; for (j=0; j<n; j++){ while (choosing[j]); while((number[j]!=0) && ((number[j],j) < (number[i],j))); } critical-section number[i] = 0; remainder-section } while(1); Dept. of Computer Applications, MES College Marampally SOCSAMS e-learning Thank You Murugan R Dept. Computer Applications MES College Marampally Dept. of Computer Applications, MES College Marampally