100% found this document useful (1 vote)
185 views28 pages

Interprocess Communication

The document discusses interprocess communication (IPC), which enables processes to communicate and synchronize their actions. It distinguishes between independent and cooperating processes, highlighting the importance of IPC mechanisms such as shared memory and message passing. The document also covers the producer-consumer problem, communication models, synchronization, and buffering methods in IPC.

Uploaded by

Elakkia U
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
185 views28 pages

Interprocess Communication

The document discusses interprocess communication (IPC), which enables processes to communicate and synchronize their actions. It distinguishes between independent and cooperating processes, highlighting the importance of IPC mechanisms such as shared memory and message passing. The document also covers the producer-consumer problem, communication models, synchronization, and buffering methods in IPC.

Uploaded by

Elakkia U
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Interprocess

Communication
Module 2
Interprocess Communication
• The way in which processes communicate with each other
• Now, processes executing concurrently in the operating system may be
either independent processes or cooperating processes.
• Independent processes – they cannot affect or be affected by the
other processes executing in the system
• Cooperating processes – they can affect or be affected by the other
processes executing in the system
• Any processes that shares data with other processes is a cooperating
processes
Several Reasons for providing an environment that allow process cooperation:

Information sharing - Several processes may need to access the


same piece of information(files)
Computation speedup - A task can often be run faster if it is broken
into subtasks and distributed among different processes.(note:if
computer has multicore)
Modularity – divide the system functions into separate process
Convenience – even an individual user may work on many tasks at
the same time
Interprocess Communication
• Cooperating processes need interprocess communication (IPC)
mechanism that will allow them to exchange data and information
• Two models:
 Shared memory
 Message passing

• Shared memory – a region of memory that is shared by cooperating


process is established
• Process can then exchange information by reading and writing data to
the shared region
Contd..
• Message Passing – communications takes place by means of
messages exchanged
• Message passing is useful for exchanging smaller amounts of data,
because no conflicts and also can easily implemented
Communications Models
(a) Message passing. (b) shared memory.

Ms.U.Elakkiya AP/IT 6
Shared memory
• IPC using shared memory requires to establish a region of shared
memory
• Shared-memory resides in the address space of the process creating
the memory segment
• If other process wants to communicate the shared-memory segment
must attach their address space
• OS to prevent one process from accessing another process memory
• Process also responsible for ensure that they not to write on same
location
Producer-Consumer Problem
• To illustrate the concept of cooperating process, let us
consider the producer – consumer problem
• Producer – to produce information
• Consumer – to consume
• Example : web server provides the web pages (HTML
files and images) which is consumed by client(web
browser) requesting resource
• One solution for producer – consumer problem uses shared
memory
• It allows the producer and consumer process to run
concurrently we need available a buffer of items that filled
by producer and empty by consumer

Ms.U.Elakkiya AP/IT 8
Contd..
• This buffer will reside in a region of memory that
is shared by producer and consumer process
• Producer produce one item while consumer
consume another item
• Both will be synchronized
• Consumer does not try to consume an item that has
not produced
• Two types of buffer has used.
• Bounded buffer
• Unbounded buffer

Ms.U.Elakkiya AP/IT 9
Contd..
 unbounded-buffer places no practical limit on the
size of the buffer
In this, the consumer may have to wait for new
items but the producer can always produce new
items
bounded-buffer assumes that there is a fixed
buffer size
In this the consumer must wait if the buffer is
empty and the producer must wait if the buffer is
full

Ms.U.Elakkiya AP/IT 10
C Program-shared memory
int shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT); //Create shared memory

char *shared_memory = (char *)shmat(shmid, NULL, 0); // Attach shared memory


Message passing
• Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the
same address space
• It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers
connected by a network.
Message Passing
• A message passing facility provides at least two operations:
• Send (message)
• Receive(message)
• The message sent by a processes can be fixed size or variable size
Contd..
• If processes P and Q wish to communicate, they must send messages
to and receive messages from each other.
• a communication link must exist between them
• The link can be implemented in a variety of ways.
There are several methods for logically implementing
a link and the send() / receive() operations like:
Direct or indirect communication
Synchronous or asynchronous communication
Automatic or explicit buffering
Contd..
• There are several issues related with features like:
• Naming
• Synchronization
• Buffering
Naming
• Processes that want to communicate must have a way to refer to each
other. They can use either direct or indirect communication
• Direct communication: each processes that wants to communicate
must explicitly name the recipient or sender of the communication
• Send ( P, message )  send a message to process P
• Receive (Q, message)  receive a message from process Q
Contd..
• A communication link has the following properties:
• Link is established automatically between every pair of processes that want
to communicate. The processes need to know only each other’s identity to
communicate
• A link is associated with exactly two processes
• Between each pair there exists exactly one link
• This scheme exhibits symmetry in addressing ; that is both the
sender and receiver must name the other to communicate.
Contd..
• Another variant of direct communication – here , only the sender
names the recipient , the recipient is not required to name the sender
• send(P, message) – send a message to process P;
• receive(id,message)- receive a message from any process; variable
id is set to the process with which communication has taken place
• This scheme Asymmetry in addressing ; here the sender names the
recipient and recipient is not required to name the sender.
Contd..
 The disadvantage in both these schemes( symmetric and asymmetric)
is the limited modularity of the resulting process definitions.
Changing the identifier of a process may necessitate examining all
other process definition.
Indirect Communication
• Messages are directed and received from mailboxes (also referred to
as ports)
• A mailbox can be viewed as an object into which messages can be
placed by processes and from which messages can be removed
• Each mailbox has unique identification
• Two processes can communicate only if the processes have shared
mailbox
• Send(A, message) - send a message to mailbox A
• Receive(A,message) – receive a message from mailbox A
Contd..
• A communication link has the following properties
• 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, with each links
corresponding to one mailbox.
Contd..
Now suppose processes P1,P2 and P3 all share mailbox A

Process P1 sends a message to A, while both P2 and P3 execute a


receive() from A. which process will receive a message sent by P1?
Contd..
• Process P1 sends a message to A, while both P2 and P3 execute a
receive() from A. which process will receive a message sent by P1?
The answer depends on which of the following methods we choose:
• Allow a link to be associated with two processes at most
• Allow at most one process at a time to execute a receive() operation
• Allow the system to select which process will receive the message. That is
system may define an algorithm for selecting which process will receive
message
• A mailbox can be owned by a process or by the operating system
ii) Synchronization
• Communication between processes take place through calls to send()
and receive() primitives.
• There are different design options for implementing each primitive
• Message passing may be either blocking or nonblocking - also
known as synchronous and asynchronous
Contd..
• Blocking Send – the sending process is blocked until the message is
received by the receiving process or by the mailbox
• Nonblocking send – the sending process sends a message and
resumes operation
• Blocking receive – the receiver blocks until a message is available
• Nonblocking receive – the receiver retrieves either a valid message or
null
iii) Buffering
• Whether communication is direct or indirect, messages exchanged between by
communicating processes reside in a temporary queue.
Basically such queues can be implemented in three ways:
• Zero capacity: the queue has a maximum length of zero; thus the link cannot
have any messages within in it. In this case, the sender must block until the
recipient receives the message.
• Bounded capacity: the queue has finite length n; thus at most n messages can
reside in it. If the queue is not full when a new message is sent, the message is
placed in the queue, and the sender can continue execution without waiting. The
links capacity is finite however. If the link is full, the sender must block until
space is available in the queue
Contd..
• Unbounded capacity: the queue length is potentially infinite; thus
any number of messages can wait in it. The sender never blocks.

You might also like