Operating Systems
CS2006
Chapter:3
Inter-Process Communication (IPC)
Part:2
1 CS-2006 Operating Systems
Overview
Zombie Vs Orphan
1- Re-aping a process
When a parent process (waits) and then removes a child
process's PCB (Process Control Block) after the child exits,
freeing its resources and fully terminating it.
“Check child process state by using sleep(100) call.”
2- Reparenting a process
Happens when the parent process of a child terminates before
the child does. The orphaned process is typically reparented to
the init process (PID 1 in Linux) to ensure it has proper
process management.
Process states: more detail
Orphan Process
What’s in today’s
lecture
1. Process Concept
2. Process Manager Responsibilities
3. Process Scheduling
4. Operations on Processes
5. Cooperating Processes
6. Inter-process
Communication
7 CS-2006 Operating Systems
Introductio
nA process has access to the memory which constitutes its own
address space.
When a child process is created, the only way to communicate
between a parent and a child process is:
The parent receives the exit status of the child
So far, we’ve discussed communication mechanisms only during
process creation/termination
Processes may need to communicate during their life time.
8 CS-2006 Operating Systems
Cooperating
Processes process cannot affect
Independent or be
affected by the
execution of another process.
Cooperating process can affect or
be affectedby the execution of another
process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
Dangers of process cooperation
9 DataOperating
CS-2006
corruption, deadlocks, increased complexity
Systems
Requires processes to synchronize their processing
Inter-Process Communication
(IPC)
Mechanism for to communicate and
processes synchronize to
their actions.
Two main
types
Shared Memory
Message Passing
1 CS-2006 Operating Systems
0
Communications
Models
(a) Message passing. (b) shared
memory.
Interprocess Communication – Shared
Memory
An area of memory shared among the processes
that wish to communicate
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.
Synchronization is discussed in great details in
Chapter 6.
Producer-Consumer
Problem
Paradigm for cooperating processes, producer process
produces information that is consumed by a consumer process
To allow producer and consumer processes to run concurrently,
we must have available a buffer of items that can be filled by
the producer and emptied by the consumer.
This buffer will reside in a region of memory that is shared
by the producer and consumer processes.
A producer can produce one item while the consumer
is consuming another item.
The producer and consumer must be synchronized, so that
the consumer does not try to consume an item that has not
yet been produced.
Producer-Consumer
Problem
Two types of buffers can be used.
unbounded-buffer places no practical limit on the
size of the buffer
bounded-buffer assumes that there is a fixed buffer
size
Let’s look more closely at how the bounded buffer
illustrates inter- process communication using shared
memory….
Bounded-Buffer – Shared-Memory
Solution
The following variables reside in a region of memory shared by the producer
and consumer processes:
Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Lets code this….
Bounded-Buffer –
Producer
item next_produced; while
(true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out) ;
/* buffer is full - do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Bounded Buffer –
Consumer
item next_consumed;
while (true) {
while (in
== out);
/* buffer
empty - do
nothing */
next_consum
ed =
buffer[out]
;
out = (out
+ 1) %
This scheme allows at most BUFFER SIZE − 1 items in the buffer at the same
BUFFER_SIZE
time. ;
/* consume
the item in
next
consumed */
}
Message Passing
(IPC)
Message system – processes communicatewith
each other without resorting to shared variables.
IPC facility provides two operations:
send(message) – message size fixed or variable
receive(message)
If P and Q wish to communicate, they need to:
establish a communication link between them
exchange messages via send/receive
Implementation of communication link
physical (i.e., shared memory, hardware bus)
logical (direct/ indirect, blocking/ non-blocking,
automatic/ explicit buffering)
18 CS-2006 Operating Systems
Implementation
Questions
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate fixed or
variable?
Is a link unidirectional or bi-directional?
19 CS-2006 Operating Systems
Message Passing
Systems
Exchange messages over a communication link
Methods for implementing the communication link
and primitives (send/receive):
1. Direct or Indirect communications (Naming)
2. Symmetric or Asymmetric communications (blocking
versus non-blocking)
3. Buffering
20 CS-2006 Operating Systems
Direct
Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically.
A link is associated with exactly one pair of communicating
processes.
Between each pair there exists exactly one link.
Processes need to know each other’s identity
The link may be unidirectional, but is usually bi-directional.
Disadvantage: a process must know the name or ID of the process(es)
it wishes to communicate with
21 CS-2006 Operating Systems
Indirect
Communication
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 have a shared 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.
22 CS-2006 Operating Systems
Indirect Communication
(Cont.)
Operations provided by the OS
create a new mailbox
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
23 CS-2006 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.
24 CS-2006 Operating Systems
Synchronizatio
nMessage passing can be blocking or non-blocking
Four types of synchronization
Blocking send:
sender blocked until message received by mailbox or process
Nonblocking send:
sender resumes operation immediately after sending
Blocking receive:
receiver blocks until a message is available
Nonblocking receive:
receiver returns immediately with either a valid or null
message.
2 CS-2006 Operating Systems
0
Produces
Consumer
Producer-consumer becomes trivial
The producer merely
message next_produced; invokes the
while (true) { send() call and waits
blocking
/* produce an item in next produced */ until the message is
send(next_produced); delivered to either
} the receiver or
the mailbox.
message next_consumed; Likewise, when the
while (true) { consumer invokes
receive(next_consum
receive(), it blocks
ed);
/* consume the item in next consumed */
until a message is
} available.
Bufferin
g exchanged by processes reside in temporary queue
Messages
Three ways to implement queues
1. Zero capacity
No messages may be queued within the link, requires sender
to block until receiver retrieves message.
2. Bounded capacity
Link has finite number of message buffers. If no buffers
are available then sender must block until one is
freed up.
3. Unbounded capacity
Link has unlimited buffer space, consequently send never needs
to block.
22 CS-2006 Operating Systems
Communication in
Client/Server Systems
Sockets
Remote procedure calls
Pipes
What types of Inter-Process Communication might each
of these use?
23 CS-2006 Operating Systems
1.
Sockets
Defined as a point of communication
Apair of process communicatingover a
network employee a pair of sockets
Socket = IP Address : Port Number
All ports below 1024 are considered well known
FTP: 21
Telnet: 23
Web server: 80
2 CS-2006 Operating Systems
4
Socket
sClient initiates a requests for connection, it is assigned
a port by host computer
Connection on one host must be unique
Sockets can be
Connection-oriented (TCP) socket
Connectionless (UDP) socket
Considered Low level mode of communication
Shares unstructured stream of bytes
25 CS-2006 Operating Systems
Socket
s
26 CS-2006 Operating Systems
2- RPC
32 CS-2006 Operating Systems
3- PIPES
Default File Descriptors
Sharing File Descriptors
Duplicate File Descriptors
3.
Pipes
IPC mechanism in early UNIX systems
Four issues must be considered
Unidirectional or bi-directional communication
If bidirectional then is it half duplex or full duplex
Must a relationship (such as parent-child) exist between
the communicating processes?
Can pipe communicate over a network?
33 CS-2006 Operating Systems
Pipes: Shared info in kernel’s
memory
Buffer in kernel’s
memory
fd[1] fd[0]
Pipe
write() read()
34 CS-2006 Operating Systems
Pipe
sTwo types of pipes
Ordinary pipes
Named pipes
•Unnamed pipe: parent process creates pipe, then uses it
to communicate with its child processes ,And they are
temporary; they need to be created every time and are
destroyed when the corresponding processes exit
• Named pipe (FIFO): any two processes (potentially
unrelated) can use
42 CS-2006 Operating Systems
Ordinary
pipes
Ordinary pipes allow
communication in standard producer-
consumer form
Producer writes to one end of the pipe (the write-
end)
Consumer reads from other end of the pipe (the
read- end of the pipe)
Required parent-child between
Ordinary pipescommunicating
are therefore unidirectional
relationship processes
In windows known as Anonymous pipes
43 CS-2006 Operating Systems
Ordinary
pipe
44 CS-2006 Operating Systems
Pipe Creation
(Unix)
#include <unistd.h>
int pipe(int filedes[2]);
Creates a pair of file descriptors pointing to a pipe
inode
Places them in the array pointed to by filedes
filedes[0] is for reading
filedes[1] is for writing.
On success, zero is returned.
On error, -1 is returned
35 CS-2006 Operating Systems
Pipe
Creation
int main()
{ int pfds[2];
if (pipe(pfds) == -1)
{
perror("pipe") pfds[0]
} ; exit(1); } pfds[1]
Process
Kernel
Pipe
flow of data
CS-2006 Operating Systems 36
Reading/Writing from/to a
Pipe
int read(int filedescriptor, char
*buffer, int bytetoread);
int write(int filedescriptor,char
*buffer,int bytetowrite);
37 CS-2006 Operating Systems
Examp
le
int main()
{ int pfds[2]={0,1};
char buf[30];
if (pipe(pfds) == -1)
{
perror("pipe");
exit(1);
}
printf("writing to file
write(pfds[1], "test", 5);
descriptor #%d\n",
pfds[1]);
write(pfds[1],
"test", 5);
printf("reading from
file descriptor #%d\
n",pfds[0]);
read(pfds[0], buf, 5);
50 CS-2006 Operating Systems
printf("read %s\n", buf);
Use of Pipe in a Single Process
A Channel between two processes
Parent Child
2. Fork()
pfds[0] pfds[0]
pfds[1] pfds[1]
1. Pipe()
Process
Kernel
Pipe
flow of data
52 CS-2006 Operating Systems
Use of Pipe Between two Related Processes
A Channel between two
•
processes
To allow one way communication each process
should close one end of the pipe.
Parent Child
2. Fork()
pfds[0
4. close(pfds[0])
pfds[0]
pfds[1] ] 3. close(pfds[1])
pfds[1
]
1. Pipe()
Process
Kernel
Pipe
flow of data
54 CS-2006 Operating Systems
An Example of pipes with
intfork
main()
{
int pfds[2];
char buf[30]; pipe(pfds);…………………………………………1 if
(fork()==0) ………………………………………2
{
close(pfds[0]);…………………………………3
printf(" CHILD: writing to the pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n"); exit(0);
}
else {
wait(NULL);
close(pfds[1]);…………………………………………4
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf( "PARENT: read \"%s\"\n", buf); }
41
} CS-2006 Operating Systems
2- Named pipes/ FIFO
Named pipes are more powerful than ordinary pipes
Communication is bidirectional
No parent-child relationship is necessary between
the communicating processes
Several processes can use the named pipe
for communication
Provided on both UNIX and Windows
Called FIFO in UNIX
45 CS-2006 Operating Systems
Introduction to FIFOs
●Pipes have no names, and their biggest disadvantage is that they can be
only used between processes that have a parent process in common (ignoring
descriptor passing)
● UNIX FIFO is similar to a pipe, as it is a one way (half duplex) flow of
data. But unlike pipes a FIFO has a path name associated with it allowing
unrelated processes to access a single pipe
● FIFOs/named pipes are used for communication between related or
unrelated processes executing on the same machine
● A FIFO is created by one process and can be opened by multiple processes
for reading or writing. When processes are reading or writing data via FIFO,
kernel passes all data internally without writing it to the file system. Thus a
FIFO file has no contents on the file system; the file system entry merely
serves as a reference point so that processes can access the pipe using a
name in the file system
mkfifo() Library Call
int mkfifo(const char*pathname, mode_t mode);
● Makes a FIFO special file with name pathname. The second
argument mode specifies the FIFO’s permissions.
● Once you have created a FIFO, any process can open it for
reading or writing, in the same way as an ordinary file.
Opening a FIFO for reading normally blocks until some other
process opens the same FIFO for writing, and vice versa
Communication Using FIFO
mkfifo command
Process1:
mkfifo mypipe
echo "Hello from Process 1" > mypipe
Process2:
cat < mypipe
"Hello from Process 1" will be get printed on output
screen.
Step 1: Create a FIFO File
mkfifo myfifo
This creates a special file named myfifo that will be used for inter-process
communication.
Process1: (Writer process)
char msg[] = "Hello from Writer!";
// Open FIFO in Write-Only mode
fd = open("myfifo", O_WRONLY);
write(fd, msg, strlen(msg) + 1);
Process2: (Reader process)
char buffer[100];
// Open FIFO in Read-Only mode
fd = open("myfifo", O_RDONLY);
read(fd, buffer, sizeof(buffer));
printf("Message received: %s\n", buffer);
Bidirectional Comm Using FIFOs
Reference
sOperating System Concepts (Silberschatz, 9th edition)
Chapter 3
46 CS-2006 Operating Systems