0% found this document useful (0 votes)
79 views

CSC 2104 Operating System Fundamentals: Process Communication

Processes communicate for reasons such as information sharing, computation speedup, modularity, and convenience. The two main models for interprocess communication are shared memory and message passing. Shared memory allows processes to directly read and write to the same memory region, while message passing involves processes sending and receiving messages. Both models require synchronization to prevent race conditions when multiple processes access shared resources concurrently.

Uploaded by

Charmaine Teh
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
0% found this document useful (0 votes)
79 views

CSC 2104 Operating System Fundamentals: Process Communication

Processes communicate for reasons such as information sharing, computation speedup, modularity, and convenience. The two main models for interprocess communication are shared memory and message passing. Shared memory allows processes to directly read and write to the same memory region, while message passing involves processes sending and receiving messages. Both models require synchronization to prevent race conditions when multiple processes access shared resources concurrently.

Uploaded by

Charmaine Teh
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/ 31

CSC 2104

Operating System
Fundamentals
Process Communication
Introduction
• Processes within a computer system may be independent or cooperating.
• Independent process cannot affect or be affected by the execution of
another process.
• Because cooperating processes can affect or be affected by the execution
of other processes, some sort of mechanisms have to be implemented for
them to communicate with each other (interprocess communication).
What are the reasons for processes to communicate?
Reasons for Interprocess Communication (IPC)

Information • Several users may be interested in the same piece of information


Sharing • For example, accessing a shared file.

Computation • Speedup the processing of a task.


Speedup • Break into smaller subtasks, so they can be executed concurrently.

• Construct system in modular fashion.


Modularity • Dividing the system functions into separate processes.

• Allow individual user to work on many tasks at the same time.


Convenience • For example, editing and printing.
Computation Speedup

Single
Process
T1 + T2 + T3 + T4 P1

T1 P1 T3 P3
Multiple
Processes

T2 P2 T4 P4
How do processes communicate with each other?
Interprocess Communication (IPC)
• Two models:
a) Shared-Memory
b) Message Passing

Process A
Process A
Shared Memory

Process B Process B

OS (Kernel) OS (Kernel)

(a) (b)
Shared-Memory
• A region of memory that is shared by the cooperating processes is established
(via system call, so that the kernel is aware of it).
• This region usually resides in the address space of the process that is creating it
(e.g. if the region is created by process A then it will be inside process A).
• The processes can then exchange information by reading and writing data to the
shared region.
Process A

Shared Memory
Process B

OS (Kernel)
Main Memory
Shared-Memory
• The communication mechanism (or related issue) is often explained using the
producer-consumer problem.
• In this case, a producer (a process) produces information that will be retrieved by a
consumer (another process).

Producer
Process A

Buffer
(Shared Memory)

Consumer
Process B
PASS
Shared-Memory

Producer Process A

Put

Buffer
(Shared Memory)

Consume

Consumer Process B
FAIL…
Shared-Memory

Producer Process A

Put

Buffer
(Shared Memory)

Consume ??? Empty ???

Consumer Process B
Shared-Memory
• The producer and the consumer must be synchronized.
• In other words, all the cooperating processes must be synchronized when
communicating with each other.
• Consumer should not consume an item that has not been produced.
• A process should not read the data until it has been produced (or updated) by
another process (else it might be reading the outdated or wrong data).
• In cases where there is more than one producer, the producers must not
change the same piece of data at the same time.
• This might lead to race condition.
Race Condition in Shared-Memory
• When two processes attempt to perform certain operation at the same time:

Process B
A 3 Current: 5
Stop
sleeping_time.txt
assignment.docx
Time Slice 4
Expired
7
8 5 exam_questions.docx Next: 7
8
9
6 csc2104_w5.pptx
Process C 7 sleeping_time.txt
myCode.java 8 myCode.txt
assignment.docx
8 9 Printer Daemon
(Background Process)
10
Printer Spooler Directory
Simple Solution to Race Condition
• Lock Variable

Process A 0
1
Wait
Process B Shared Variable
Message Passing
• Another mechanism for processes to communicate.
• Easier to implement (no synchronization issue).
• The message passing facility provides at least two operations (system calls):
• send (receiver, message)
• receive (source, message)
• Message can be in fixed or variable size.
Message Passing
• If message is in fixed size:
• Easier for system implementation (e.g. can only accept 5 characters).
• Harder for programmer (have to split data into chunks, each with 5 characters).
• If message is in variable size:
• Harder for system implementation (have to perform the splitting task).
• Easier for programmer (do not have to worry about the size limit).
Example
• A standard message (packet) consists of two parts:
• Header, which consists of message type, destination, source, message length,
and other information.
• Body, which is the message that one would like to transfer.

Fixed Length

Header Body

Packet
Good Morning
Example 1 Packet:5 Chars

• Fixed Size
• send(“Good ”); System Implementation
• send(“Morni”);
• send(“ng”); Transmit
Packet
• Variable Size
• send(“Good Morning” ) Transmit

“Morni”
“Good
“ng ” ” Transmit
System Implementation

Transmit
Message Passing Model
• When processes want to communicate, they need to:
• Establish a communication link between them.
• Exchange messages via the send or receive operation.
• Implementation Issues:
• Direct vs. Indirect
• Synchronous vs. Asynchronous
• Buffering
Direct and Indirect Communication
• Direct Communication (One-to-One)

To Q From P
P Q

• Indirect Communication (One-to-One)

From Mailbox A

P Mailbox A Q

To Mailbox A
Direct Communication
• Processes must name each other explicitly:
• send (process P, message)
• Send a message to process P.
• receive(process Q, message)
• Receive a message from process Q.
• Properties of communication link:
• Links are established automatically.
• Between each pair of processes there exists exactly one link (one-to-one).
• The link is usually bi-directional.
• But can be hard to know in advanced, the time to establish the link (e.g. a
printer process will not be able to know when to run the receive operation).
Indirect Communication
• Messages are sent to and received from mailboxes or ports.
• Each mailbox has a unique id.
• A data structure to temporary hold the messages (usually first-in first-out).
• 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.
• Link may be unidirectional or bi-directional.
• Each pair of processes may share several communication links.
Indirect Communication
• To communicate using indirect communication:
1) Create a new mailbox.
2) Send and receive messages through mailbox
3) Destroy the mailbox.
• Processes do not have to name each other explicitly:
• send(mailbox A, message)
• Send a message to mailbox A.
• receive(mailbox A, message)
• Receive a message from mailbox A.
msgsnd()
M Process A

msgrcv()
Process B

Mailbox
M

OS (Kernel)
Indirect Communication
• Many-to-One

P1 Mailbox A P2

msgsnd() msgrcv()

P3
msgrcv()
Direct and Indirect Communication
• Many-to-Many

P Mailbox A Q

R
Synchronous vs. Asynchronous
• Synchronous
• Sender will not be able to send another message until the message is
received by the receiving process or by the mailbox.
• Sender will have to wait for the process to complete before it can continue
with the remaining task.
• Asynchronous
• Sender can just send the message without waiting for the receiving process or
by the mailbox.
• Sender sends the message and continue with the remaining task (no waiting).
Synchronous

Can I send NO
another message?
Suspended
OK
Sender Receiver
1. send(“I’m hungry”) Sending…
Done
2. send(“Dinner?”) Ya, I received and
read your message.

Have to wait for confirmation from the receiver.


Asynchronous
Can I send another OK
message?

Sending…
Sender Receiver
1. send(“I’m hungry”)
2. send(“Dinner?”) Buffer
3. send(“Where?”)
Buffering
• Messages exchanged by processes reside in a temporary queue (buffer).
• Basically, the queue can be implemented in three ways:
• Zero Capacity (or Zero Message)
• The link cannot have any messages waiting in it. Synchronous
• The sender needs to block until the recipient receives the message. Communication
• Bounded Capacity (Finite Length of n Messages)
• If the queue is not full, sender places the message in the queue.
• Sender can then continue execution without waiting. Asynchronous
• If the queue is full, sender has to be blocked. Communication
• Unbounded Capacity (Infinite Length of Messages)
• Sender will never blocks.
Shared-Memory vs. Message Passing

Shared-Memory Message Passing

Faster in exchanging data. System calls are required Useful for exchanging smaller amount of data.
to establish the shared-memory regions. Once is Slower because typically it is implemented using
established, no assistance is needed from the system calls that require kernel intervention.
kernel.

Convenience for communication within the same Easier to implement for inter-computer
system. communication.

You might also like