Unit 2 Lecture 21 - IPC
Unit 2 Lecture 21 - IPC
UE22CS242B
IPC - Shared Memory & Message Passing
Suresh Jamadagni
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all the PPTs of this course
Suresh Jamadagni
Department of Computer Science
OPERATING SYSTEMS
Interprocess Communication
• Processes within a system may be independent or cooperating
• Cooperating process can affect or be affected by other processes, including
sharing data
• Reasons for cooperating processes:
• Information sharing
• Computation speedup
• Modularity
• Convenience
• Cooperating processes need interprocess communication (IPC)
• Two models of IPC
• Shared memory
• Message passing
OPERATING SYSTEMS
Cooperating Processes
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
• Shared buffer is implemented as a circular array with 2 logical pointers: in and out
• Buffer is empty when in == out; buffer is full when ((in + 1) % BUFFER_SIZE) == out
• Variable in points to the next free position in the buffer; out points to the first full position in
the buffer
• Solution is correct, but can only use BUFFER_SIZE-1 elements
OPERATING SYSTEMS
Bounded-Buffer – Producer
item next_produced;
while (true) {
/* produce an item in next_produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
OPERATING SYSTEMS
Bounded-Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
• The communication is under the control of the users processes not the
operating system.
• Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.
OPERATING SYSTEMS
Shared Memory (Cont.)
• Shared memory allows two or more processes to share a given region of memory.
• Shared memory is the fastest form of IPC, because the data does not need to be copied between the
client and the server.
• The only trick in using shared memory is synchronizing access to a given region among multiple
processes.
• If the server is placing data into a shared memory region, the client shouldn't try to access the data until
the server is done.
• Often, semaphores are used to synchronize shared memory access. (record locking can also be used.)
(A semaphore is a value in an operating system (OS) that controls resource access. )
OPERATING SYSTEMS
Shared Memory (Cont.)
OPERATING SYSTEMS
Interprocess Communication – Message Passing
• send(message)
• receive(message)
• Implementation issues:
• How many links can there be between every pair of communicating processes?
• Is the size of a message that the link can accommodate fixed or variable?
• Messages are directed and received from mailboxes (also referred to as ports)
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
• Properties of communication link
• 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
• Link may be unidirectional or bi-directional
OPERATING SYSTEMS
Indirect Communication (Cont.)
• Operations
• create a new mailbox (port)
• send and receive messages through mailbox
• destroy a mailbox
• Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
OPERATING SYSTEMS
Indirect Communication (Cont.)
• Mailbox sharing
• P1, P2, and P3 share mailbox A
• P1 sends; P2 and P3 receive
• Who gets the message?
• Solutions
• Allow a link to be associated with at most two processes
• Allow only one process at a time to execute a receive operation
• Allow the system to select arbitrarily the receiver. Sender is notified who the
receiver was.
OPERATING SYSTEMS
Message Passing - Synchronization
message next_produced;
while (true) {
/* produce an item in next_produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
• Queue of messages attached to the link (direct or indirect); messages reside in a temporary queue.
Suresh Jamadagni
Department of Computer Science Engineering
[email protected]