43
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally INTERPROCESS INTERPROCESS COMMUNICATION COMMUNICATION AND AND SYNCHRONIZATION SYNCHRONIZATION

INTERPROCESS COMMUNICATION AND SYNCHRONIZATION

  • Upload
    vea

  • View
    59

  • Download
    0

Embed Size (px)

DESCRIPTION

INTERPROCESS COMMUNICATION AND SYNCHRONIZATION. 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). - PowerPoint PPT Presentation

Citation preview

Page 1: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

INTERPROCESS INTERPROCESS COMMUNICATION COMMUNICATION

ANDAND

SYNCHRONIZATIONSYNCHRONIZATION

Page 2: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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)

Concurrency - Basic Concept

Page 3: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

A simple batch operating system can be viewed as 3 processes :

• a reader process

• an executor process

• a printer process

Concurrency - Basic Concept

Input Input BufferBuffer

Output Output BufferBuffer

ProcessProcess

Page 4: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Basic Concepts of IPC & Synchronization:

In order to cooperate, concurrently executing processes must communicate and synchronize. Interprocess communication is based on the use of shared variables (variables that can be referenced by more than one process) or message passing.

Inter-Process Communication

Page 5: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Inter-Process Communication

Page 6: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Inter-Process Communication

Page 7: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

• Critical Resource

• Critical Section

• Mutual Exclusion

Inter-Process Communication

Page 8: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

• Critical Resource

It is a resource shared with constraints on its use(e.g., memory, files, printers, etc)

• Critical Section

• Mutual Exclusion

Inter-Process Communication

Page 9: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

• Critical Resource

• Critical Section

It is code that accesses a critical resource.

• Mutual Exclusion

Inter-Process Communication

Page 10: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

• Critical Resource

• Critical Section

• Mutual Exclusion

At most one process may be executing a critical section with respect to a particular critical resource simultaneously.

Inter-Process Communication

Page 11: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

IPC can be possible in two different ways:

• Shared-Memory System

• Message-Passing System

Inter-Process Communication

Page 12: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Shared-Memory System

Page 13: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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)

Message-Passing System

Page 14: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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)

Message-Passing System

In order to send and to receive messages, a communication link must exist between the two involved processes.

Page 15: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Methods for logically implementing a communication link and the send/receive operations are classified into:

• Naming

• Buffering

Message-Passing System

Page 16: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Methods for logically implementing a communication link and the send/receive operations are classified into:

• Naming

• Buffering

Message-Passing System

Consisting of direct and Consisting of direct and indirect communication.indirect communication.

Page 17: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Methods for logically implementing a communication link and the send/receive operations are classified into:

• Naming

• Buffering

Message-Passing System

Consisting of capacity and Consisting of capacity and message properties.message properties.

Page 18: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Direct Communication

Page 19: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Direct Communication

Here a link is established automatically Here a link is established automatically between every pair of processes that want to between every pair of processes that want to communicate. Exactly one link exists between communicate. Exactly one link exists between each pair of processes.each pair of processes.

Page 20: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Direct Communication

Page 21: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Direct Communication

Page 22: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Indirect Communication

Page 23: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Indirect Communication

Page 24: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Indirect Communication

Page 25: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Buffering

Page 26: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Buffering - Capacity

Page 27: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Buffering - Capacity

Page 28: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Buffering - Capacity

Page 29: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Race Condition

Value depends on which of the processes wins the race to update the variable.

Interprocess Synchronization

abcabcProg.cProg.cProg.nProg.n

..

..

..

..44556677

Process AProcess A

Process BProcess B

Out = 4Out = 4

In = 7In = 7

next filenext fileto be printedto be printed

next free slotnext free slotin the directoryin the directory

Page 30: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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

Interprocess Synchronization

Page 31: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Interprocess Synchronization

Page 32: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

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.

Interprocess Synchronization

Page 33: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Do {

entry section

critical section

exit section

remainder section

} while(1);

General structure of a typical process

Critical Section

Page 34: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Do {

entry section

critical section

exit section

remainder section

} while(1);

General structure of a typical process

Critical Section

Section of code that request Section of code that request permission to enter its critical permission to enter its critical section.section.

Page 35: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Do {

entry section

critical section

exit section

remainder section

} while(1);

General structure of a typical process

Critical Section

It is a part of code in which it It is a part of code in which it is necessary to have exclusive is necessary to have exclusive access to shared data.access to shared data.

Page 36: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Do {

entry section

critical section

exit section

remainder section

} while(1);

General structure of a typical process

Critical Section

Code for tasks just after Code for tasks just after exiting from the critical exiting from the critical section.section.

Page 37: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Do {

entry section

critical section

exit section

remainder section

} while(1);

General structure of a typical process

Critical Section

The remaining code.The remaining code.

Page 38: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Mutexes : Mutual Exclusion

{parent process} P1busy:=false; P2busy:=false; initiate P1, P2;end; {mutex}

var P1busy, P2busy : boolean;

Page 39: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Mutexes : Mutual Exclusion

process P1begin 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}

var P1busy, P2busy : boolean;

Page 40: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Mutexes : Mutual Exclusion

process P1begin while true do begin P1busy := true; while P2busy do {keep testing}; critical-section; P1busy:=false; other_P1busy_processing; end{while}End; {P1}

process P2begin while true do begin P2busy := true; while P1busy do {keep testing}; critical-section; P2busy:=false; other_P2busy_processing; end{while}End; {P2}

{parent process} P1busy:=false; P2busy:=false; initiate P1, P2;end; {mutex}

var P1busy, P2busy : boolean;

Page 41: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Critical-Section Problem

boolean flag[2];boolean flag[2];int turn;int turn;do {do { flag[i] = true;flag[i] = true; turn = j;turn = j; while (flag[j] && turn == j);while (flag[j] && turn == j); Critical-sectionCritical-section flag[i] = false;flag[i] = false; remainder-sectionremainder-section } while(1);} while(1);

Initially flag[0]=flag[1]=falseTurn = 0 or 1.

Page 42: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Bakery Algorithmboolean choosing[n];boolean choosing[n];int number[n];int number[n];

do {do { choosing[i] = true;choosing[i] = true; number[i] = max(number[0], number[1], …, number[n-1]) + 1;number[i] = max(number[0], number[1], …, number[n-1]) + 1; choosing[i] = flase;choosing[i] = flase; for (j=0; j<n; j++){for (j=0; j<n; j++){ while (choosing[j]);while (choosing[j]); while((number[j]!=0) && ((number[j],j) < (number[i],j)));while((number[j]!=0) && ((number[j],j) < (number[i],j))); } } critical-sectioncritical-section number[i] = 0;number[i] = 0; remainder-sectionremainder-section } while(1);} while(1);

Page 43: INTERPROCESS COMMUNICATION  AND  SYNCHRONIZATION

SOCSAMS e-learning

Dept. of Computer Applications, MES College Marampally

Thank YouMurugan R

Dept. Computer ApplicationsMES College Marampally