OS Lecture3 - Inter Process Communication
OS Lecture3 - Inter Process Communication
Interprocess Communication
• 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.
Examples: IRCTC Ticket reservation
Note: They can share the variables (or) memory (or)
code (or) Resources such as CPU, Printer, Scanner.
Basics of inter process communication (IPC) (or)
Advantages of process cooperation (or) Why IPC
• There are several reasons for providing an
environment that allows process cooperation :
• Information sharing: Since several users may be
interested in the same piece of information (for
example, a shared file), you must provide a situation
for allowing concurrent access to that information.
• Computation speed up: If you want a particular work
to run fast, you must break it into sub-tasks where each
of them will get executed in parallel with the other
tasks. Note that such a speed-up can be attained only
when the computer has compound or various
processing elements like CPUs or I/O channels.
• Modularity: You may want to build the system
in a modular way by dividing the system
functions into split processes or threads.
• Convenience: Even a single user may work on
many tasks at a time. For example, a user may
be editing, formatting, printing, and compiling
in parallel.
Dangers of process cooperation
– Data corruption, deadlocks, increased complexity
– Requires processes to synchronize their processing
• Inter process communication (IPC) is a mechanism
which allows processes to communicate with each
other and synchronize their actions.
• OS Provides facilities for IPC.
• There are two primary models of inter process
communication (or)IPC Mechanisms:
1. Shared memory
2. Message passing
• In the shared memory model, a region of memory
that is shared by cooperating processes is
established.
• Processes can then exchange information by reading
and writing data to the shared region.
• In the message passing model, communication
takes place by means of messages exchanged
between the cooperative processes.
• The two communications models are contrasted
in the figure below:
Producer Consumer Problem
• A producer process produces information that is
consumed by a consumer process.
• For example, a compiler may produce assembly
code, which is consumed by an assembler. The
assembler, in turn, may produce object modules,
which are consumed by the loader.
• One solution to the producer consumer problem
uses shared memory.
Process Synchronization
Introduction:
• When two or more process cooperates with each
other, their order of execution must be
preserved otherwise there can be conflicts in
their execution and inappropriate outputs can be
produced.
• A cooperative process is the one which can
affect the execution of other process or can be
affected by the execution of other process. Such
processes need to be synchronized so that their
order of execution can be guaranteed.
What is Process Synchronization?
• The procedure involved in preserving the
appropriate order of execution of cooperative
processes is known as Process
Synchronization.
(or)
• Process Synchronization is the task of
coordinating the execution of processes in a
way that no two processes can have access to
the same shared data and resources.
• There are various synchronization mechanisms
that are used to synchronize the processes.
Example: int shared=5 (uni processor)
Process P1 Process P2
int x= shared; int y=shared;
x++; y--;
sleep(1); sleep(1);
shared=x; shared=y;
P1() P2()
{ {
c=b-1; d=2*b;
b=2*c; b=d-1;
}
}
Down(s) Down(s)
CS CS
Up(S) Up(S)
Counting Semaphore vs. Binary Semaphore
• Here, are some major differences between counting and
binary semaphore: