You are on page 1of 52

Chapter 3: Processes –

Inter Process
Communication (IPC)
Chapter 3: Processes - IPC

● Process Concept
● Interrupts and Processes
● Process Data
● Process Scheduling
● Operations on Processes
● Interprocess Communication
● Examples of IPC Systems
● Communication in Client-Server Systems

In order to explain the principles behind Processes and the manner in which they are
dealt with by the OS - we restrict ourselves in this presentation to:
• Using a single CPU system. The issue of running processes in a multiprocessor
systems will be dealt with later in the course.
• Dealing with  single-threaded processes. The issue of multi-threaded processes
will be dealt with later in the course. 

© Dr Yigal Hoffner 4 May 2020 2


Inter Process
Communication
(IPC)

© Dr Yigal Hoffner 4 May 2020 3


Inter-process Communication (IPC)
● Processes within a system or residing on different systems may need to cooperate with
each other for reasons such as:
● Sharing of Information
● Performance improvements such as computation speedup
● Better structuring of an application by separation of concerns (Modularity)
● Convenience & flexibility
● Cooperating processes need to be able to do two things:
● Exchange data between them
● Synchronise the activities of one process with anther

● Inter-process communication (IPC) refers specifically to the mechanisms an


operating system provides to allow the processes to exchange data and
synchronise the activities of two or more processes

© Dr Yigal Hoffner 4 May 2020 4


Models of communication (transfer method)
●  There are different views on what IPC includes
● The classic view:
● Shared memory
● Message passing
● POSIX IPC view:
● Shared memory
● Message passing
● Semaphores
● Other views (which we adopt):
● Shared memory: allows processes to exchange data and synchronize through shared
memory
● Message passing: allow processes to send data streams to other processes
● Procedure call: invocation of a procedure (code + data) elsewhere
● Semaphores: allow processes to synchronize their progress

© Dr Yigal Hoffner 4 May 2020 5


Models of communication (transfer method)
a) Message Passing: the medium
hands the information over in an (a) Message
Client Service
orderly manner passing

b) Sharing Memory : the medium is Client Service


used as a repository – it is up to (b) Shared
the parties to manage the transfer memory
Shared
memory
c) Procedure Calling:
communication results in an
invocation of a procedure Service (c) Procedure
elsewhere (upcall) & a reply call
Client Procedu
re

d) Semaphores: allow processes to


synchronize their progress
Semaphore sem (d) Semaphore
P
=0; P
0 1
Wait(se
m)

Signal(s
em)

© Dr Yigal Hoffner 4 May 2020 6


Dimensions of IPC
● Inter process communication mechanisms can differ along several dimensions:
● Communication medium capacity
4 Unbounded-exchange: places no practical limit on the size of the exchange
4 Bounded-exchange: assumes that there is a fixed exchange size
● Private or shared communication medium - Single or multiple producers/consumers
4 Single producer & consumer
4 Multiple proaducers & consumers (multi-process or multi-threaded environment)
● Uni-directional or bi-directional transfer: the medium can be used in both directions
● Direct or indirect transfer: explicitly naming the recipient or messages are sent to and received
from mailboxes
● Synchronous or asynchronous: Blocking or non-blocking send/receive operations
● Busy-Waiting or Suspension-waiting: does waiting process spin or suspend itself
● Local or remote IPC: IPC between parties on same or different CPU’s (or cores)

Communication
Client Service
Medium

© Dr Yigal Hoffner 4 May 2020 7


Client Service

Shared
memory

Shared Memory
IPC

© Dr Yigal Hoffner 4 May 2020 8


Local Shared Memory
● An area of memory shared among the processes that
wish to communicate (transfer data or synchronise)
Shared memory
● The communication is under the control of the users
processes (& hence the programmer) - not under the
control of the operating system
● It is therefore very flexible but also very prone to
programmer errors
● Major issues is to provide mechanism that will allow
the user processes to synchronize their actions when
they access shared memory
● Synchronisation through shared memory without the
OS – must use spinning or busy waiting
● Any form of synchronisation that does not use
spinning must be done by the OS

© Dr Yigal Hoffner 4 May 2020 9


Shared Memory – on same & different CPU’s
● Shared Memory on 1 CPU can generalized to IPC where the processes reside on
different CPU’s or cores and communicate through shared memory

Memory sharing in a single CPU Memory sharing in a multicore


environment environment
CPU A
Client Service CPU CPU CPU
cach
cache cache
e
Bus
Shared
memory
Shared memory

© Dr Yigal Hoffner 4 May 2020 10


Examples of IPC Systems - POSIX

● POSIX Shared Memory


● Process first creates shared memory segment – using System Call:
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
● Also used to open an existing segment to share it
● Set the size of the object
● ftruncate(shm fd, 4096);
● Now the process could write to the shared memory
● sprintf(shared memory, "Writing to shared
memory");
This is done without the involvement of the OS!

© Dr Yigal Hoffner 4 May 2020 11


Producer process illustrating POSIX shared-memory API
int main() { // PRODUCER side
/* the size (in bytes) of shared memory object */
const int SIZE 4096;

const char *name = "OS"; /* name of the shared memory object */


shared-memory object is to be created if it
does not yet exist (O CREAT)
/* strings written to shared memory */ It is open for reading and writing (O RDRW)
const char *message_0 = "Hello"; The last parameter establishes the directory
const char *message_1 = "World!"; permissions of the shared-memory object.

int shm_fd; /* shared memory file descriptor */

void *ptr; /* pointer to shared memory object */ A successful call to shm open()
returns an integer file descriptor
for the shared-memory object.
/* create the shared memory object */
shm_fd = shm_open(name, O_CREAT | O_RDRW, 0666);

/* configure the size of the shared memory object */


ftruncate(shm_fd, SIZE);

/* memory map the shared memory object */ mmap() function establishes a memory-mapped file
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd,
containing the0);
shared-memory object.
It also returns a pointer to the memory-mapped file
/* write to the shared memory object */ that is used for accessing the shared-memory object.
sprintf(ptr,"%s",message_0);

ptr += strlen(message_0);
sprintf(ptr,"%s",message_1);
© Dr Yigal Hoffner 4 May 2020 12
Consumer process illustrating POSIX shared-memory API
int main() { // CONSUMER side
/* the size (in bytes) of shared memory object */
const int SIZE 4096;

/* name of the shared memory object */


const char *name = "OS";
shared-memory object is open for reading
/* shared memory file descriptor */ (O_RDONLY)
The last parameter establishes the directory
int shm_fd; permissions of the shared-memory object.

/* pointer to shared memory object */


void *ptr; A successful call to shm open()
returns an integer file descriptor
for the shared-memory object.
/* open the shared memory object */
shm_fd = shm_open(name, O_RDONLY, 0666);

/* memory map the shared memory object */ mmap() establishes a a


ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm fd, 0); connection with the shared-
memory object created in
/* read from the shared memory object */ the Producer
printf("%s",(char *)ptr);

/* remove the shared memory object */


shm_unlink(name);
return 0;
}

© Dr Yigal Hoffner 4 May 2020 13


Shared memory: advantages & disadvantages

● Advantages:
● No need to involve OS – no system calls, except during setup
● Transfer can be uni or bi-directional
● Can be used for data transfer or synchronization
● Can pass any type of data

● Disadvantages:
● Lack of in-built synchronisation: no synchronisation is offered by the
mechanism
● If synchronisation is needed – it can be provided by using the shared memory,
but it will be based on spinning (busy-waiting)
● So anything based on suspension will require a proper mechanism, such as
semaphores or locks, for example

© Dr Yigal Hoffner 4 May 2020 14


Message Passing
IPC

© Dr Yigal Hoffner 4 May 2020 15


Message Passing – on same & different CPU’s
● Local Message Passing on 1 CPU
can generalized to IPC where the processes reside on different CPU’s – resulting
in:
● Remote Message Passing

(1) Local Message passing (2) Remote Message passing


(single CPU) (distributed CPU’s)

CPU A CPU A CPU B

PA1 PA2 PA1 PB1


Send Receive
Send Receive Kernel Kernel
Kernel Communication Communication

network

© Dr Yigal Hoffner 4 May 2020 16


Message Passing

● Mechanism for processes to communicate and to synchronize their


actions

● Message system – processes communicate with each other without


resorting to shared variables

● IPC facility provides two operations:


● send(message)
● receive(message)

● The message size is either fixed or variable

© Dr Yigal Hoffner 4 May 2020 17


Message Passing (Cont.)

● If processes P and Q wish to communicate, they need to:


● Establish a communication link between them
● Exchange messages via send/receive
● Implementation issues:
● 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?

© Dr Yigal Hoffner 4 May 2020 18


Message Passing (Cont.)

● Implementation of communication link


● Physical:
4 Shared memory
4 Hardware bus
4 Network
● Logical:
4 Direct or indirect
4 Synchronous or asynchronous
4 Automatic or explicit buffering

© Dr Yigal Hoffner 4 May 2020 19


Direct versus Indirect
Communication

© Dr Yigal Hoffner 4 May 2020 20


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
● The link may be unidirectional, but is usually bi-directional

© Dr Yigal Hoffner 4 May 2020 21


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 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
● 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

© Dr Yigal Hoffner 4 May 2020 22


Indirect Communication
● 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.

© Dr Yigal Hoffner 4 May 2020 23


IPC Blocking/Non-blocking

© Dr Yigal Hoffner 4 May 2020 24


Synchronization: Synchronous or asynchronous operations

● Synchronous or asynchronous: Blocking or non-blocking send/receive operations


● Send
● Blocking send: The sending process is blocked until the message is received by
the receiving process or by the mailbox
● Non-blocking send: The sending process sends the message and resumes
operation
● Receive
● Blocking receive: The receiver blocks until a message is available.
● Non-blocking receive: The receiver retrieves either a valid message or a null
● Different combinations of send() and receive() are possible. When both send() and
receive() are blocking, we have a rendezvous between the sender and the receiver

© Dr Yigal Hoffner 4 May 2020 25


Producer-consumer Synchronization with Message passing

● Producer-consumer becomes trivial

message next_produced;
while (true) {

/* produce an item in next produced */


send(next_produced);
}

message next_consumed;
while (true) {
receive(next_consumed);

/* consume the item in next consumed */


}

© Dr Yigal Hoffner 4 May 2020 26


IPC Buffering
options

© Dr Yigal Hoffner 4 May 2020 27


Message passing Buffering
● Queue of messages attached to the link.
● implemented in one of three ways
1. Zero capacity – no messages are queued on a link.
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full Consumer
consumer producer
Producer
3. Unbounded capacity – infinite length
out
Sender never waits in

BUFFER_SIZE-1 y z
0 1
2
buffer

Flag

Buffer
Producer Consumer

© Dr Yigal Hoffner 4 May 2020 28


Examples of IPC Systems - Mach
● Mach communication is message based
● Even system calls are messages
● Each task gets two mailboxes at creation- Kernel and Notify
● Only three system calls needed for message transfer
msg_send(), msg_receive(), msg_rpc()
● Mailboxes needed for commuication, created via
port_allocate()
● Send and receive are flexible, for example four options if mailbox full:
4 Wait indefinitely
4 Wait at most n milliseconds
4 Return immediately
4 Temporarily cache a message

© Dr Yigal Hoffner 4 May 2020 29


Pipe IPC

© Dr Yigal Hoffner 4 May 2020 30


Pipes
● Acts as a conduit allowing two processes to communicate
● Issues:
● Is communication unidirectional or bidirectional?
● In the case of two-way communication, is it half or full-duplex?
● Must there exist a relationship (i.e., parent-child) between the
communicating processes?
● Can the pipes be used over a network?
● Ordinary pipes – cannot be accessed from outside the process that created it.
Typically, a parent process creates a pipe and uses it to communicate with a
child process that it created
● Named pipes – can be accessed without a parent-child relationship.

© Dr Yigal Hoffner 4 May 2020 31


Ordinary Pipes

● Ordinary Pipes allow communication in standard producer-consumer style


● Producer writes to one end (the write-end of the pipe)
● Consumer reads from the other end (the read-end of the pipe)
● Ordinary pipes are therefore unidirectional
● Require parent-child relationship between communicating processes

● Windows calls these anonymous pipes


● See Unix and Windows code samples in textbook

© Dr Yigal Hoffner 4 May 2020 32


Named Pipes

● 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 systems

© Dr Yigal Hoffner 4 May 2020 33


IPC on
different CPU’s

© Dr Yigal Hoffner 4 May 2020 34


Communications in Client-Server Systems

● Sockets
● Remote Procedure Calls (example: Remote Method Invocation in Java)

© Dr Yigal Hoffner 4 May 2020 35


Sockets
IPC

© Dr Yigal Hoffner 4 May 2020 36


Sockets

● A socket is defined as an endpoint for communication

● Concatenation of IP address and port – a number included at start of


message packet to differentiate network services on a host

● The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8

● Communication consists between a pair of sockets

● All ports below 1024 are well known, used for standard services

● Special IP address 127.0.0.1 (loopback) to refer to system on which


process is running

© Dr Yigal Hoffner 4 May 2020 37


Socket Communication

© Dr Yigal Hoffner 4 May 2020 38


Sockets
● The steps involved in establishing a socket on the client side are as follows:
● Create a socket with the socket() system call
● Connect the socket to the address of the server using the connect() system call
● Send and receive data. There are a number of ways to do this, but the simplest is to use
the read() and write() system calls.

● The steps involved in establishing a socket on the server side are as follows:
● Create a socket with the socket() system call
● Bind the socket to an address using the bind() system call. For a server socket on the
Internet, an address consists of a port number on the host machine.
● Listen for connections with the listen() system call
● Accept a connection with the accept() system call. This call typically blocks until a client
connects with the server.
● Receive and Send data

© Dr Yigal Hoffner 4 May 2020 39


Sockets in Java – Server side
● Three types of sockets
1. Connection-oriented:
Transmission Control Protocol
(TCP) Por
t
2. Connectionless: User Datagram
Protocol (UDP)
3.MulticastSocket class– data Wait for
client
can be sent to multiple recipients request

● A TCP/UDP socket is an endpoint


instance defined by an IP address reply
and a port

● UDP=User Datagram Protocol


● TCP=Transmission Control Protocol

© Dr Yigal Hoffner 4 May 2020 40


Sockets in Java – Client side

import java.net.*;
import java.io.*;
public class DateClient IP address
{ Por
public static void main(String[] args) { t
try {
/* make connection to server socket */ client
Socket sock = new Socket("127.0.0.1", 6013); request

InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));

/* read the date from the socket */


String line;
while ( (line = bin.readLine()) != null)
System.out.println(line);
/* close the socket connection*/
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
© Dr Yigal Hoffner 4 May 2020 41
Local & Remote Procedure Calls
IPC

© Dr Yigal Hoffner 4 May 2020 42


Remote Procedure Calls
● Remote procedure call (RPC) abstracts procedure calls between processes on networked
systems
● Again uses ports for service differentiation
● Stubs – client-side proxy for the actual procedure on the server
● The client-side stub locates the server and marshalls the parameters
● The server-side stub receives this message, unpacks the marshalled parameters, and
performs the procedure on the server
● Different types of Interface Definition Language (IDL):
● On Windows, stub code compile from specification written in Microsoft Interface
Definition Language (MIDL)
● There is also Web Services Definition Language (WSDL)

Local procedure call Remote procedure call

s s
t t
u u
b b

CPU
CPU CPU 43
© Dr Yigal Hoffner 4 May 2020
Remote Procedure Calls (Cont.)
● Data representation handled via External Data Representation (XDL)
format to account for different architectures
● Big-endian and little-endian
● Remote communication has more failure scenarios than local
● Messages can be delivered exactly once rather than at most once
● OS typically provides a rendezvous (or matchmaker) service to connect
client and server

Remote procedure call

s s
Java t t C++
u u
b b

CPU CPU
(64 bit) (32 bit)

© Dr Yigal Hoffner 4 May 2020 44


Execution of RPC
client Matchmaker server

© Dr Yigal Hoffner 4 May 2020 45


Examples of IPC Systems – Windows

● Message-passing centric via advanced local procedure call (LPC)


facility
● Only works between processes on the same system
● Uses ports (like mailboxes) to establish and maintain
communication channels
● Communication works as follows:
4 The client opens a handle to the subsystem’s connection port
object.
4 The client sends a connection request.
4 The server creates two private communication ports and
returns the handle to one of them to the client.
4 The client and server use the corresponding port handle to
send messages or callbacks and to listen for replies.

© Dr Yigal Hoffner 4 May 2020 46


Advanced Local Procedure Calls in Windows

● The message-passing facility in Windows is called the advanced local procedure


call (ALPC) facility
● It is used for communication between two processes on the same machine. It is
similar to the standard remote procedure call (RPC) mechanism that is widely used,
but it is optimized for and specific to Windows

© Dr Yigal Hoffner 4 May 2020 47


An example problem
– requiring inter-process interaction

© Dr Yigal Hoffner 4 May 2020 48


Producer-Consumer Problem
● The producer consumer problem is a synchronization problem that deals with a shared buffer - where
a producer produces items and enters them into the buffer. The consumer removes the items from the buffer and
consumes them
● A popular paradigm for cooperating processes
● The problem & solutions have several dimensions:
● Buffering
4 Unbounded-buffer places no practical limit on the size of the buffer
4 Bounded-buffer assumes that there is a fixed buffer size
– Single cell buffer
– Cyclical buffer
● Single or multiple producers/consumers
4 Single producer & consumer
4 Multiple producers & consumers (multi-process or multi-threaded environment)
● Blocking or non-blocking operations
● Busy waiting or Suspension waiting (when buffer is empty/full)

Producer buffer Consumer

© Dr Yigal Hoffner 4 May 2020 49


Producer-Consumer: Single Cell buffer

● Single producer and consumer


● Bounded buffer

Flag

Producer Buffer Consumer

© Dr Yigal Hoffner 4 May 2020 50


Producer-Consumer: cyclical-buffer
● Shared data
#define BUFFER_SIZE 10 consumer
typedef struct { producer
out
. . . in
} item;
item buffer[BUFFER_SIZE];
int in = 0; BUFFER_SIZE-1 y z
Producer int out = 0; 0 1
2
buffer
item next_produced;
while (true) {
while (((in + 1) % BUFFER_SIZE) == out); /* wait */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer

item next_consumed;
while (true) {
while (in == out); /* wait */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}

© Dr Yigal Hoffner 4 May 2020 51


End of Chapter 3

You might also like