Вы находитесь на странице: 1из 31

Operating Systems

Inter-Process
Communication (IPC)
Processes
Independent – if it cannot affect or be affected
by the other processes executing in the system.
Any process that does not share data withany other
process.
Cooperating - if it can affect or be affected by
the other processes executing in the system.
Process that shares data with other processes.
Why co-operating processes?
1. Information sharing
2. Computational speedup
3. Modularity
4. Convenience
Inter-process communication
1. Shared memory – co-operating processes share a
region of memory.
2. Message passing – communication takes place by
means of messages exchanged between the
cooperating processes.
Shared Data between the two Processes
#define buff_max 25
#define mod %

struct item{

// diffrent member of the produced data


// or consumed data
---------
}

// An array is needed for holding the items.


// This is the shared place which will be
// access by both process
// item shared_buff [ buff_max ];

// Two variables which will keep track of


// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
int free_index = 0;
int full_index = 0;
Producer Process Code

item nextProduced;

while(1){

// check if there is no space


// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index);

shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}
Consumer Process Code

item nextConsumed;

while(1){

// check if there is an available


// item for consumption.
// if not keep on waiting for
// get them produced.
while((free_index == full_index);

nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}
Message passing
Operations
Send(message)
Receive(message)
Methods to implement link b/w two processes
Direct or indirect communication
Synchronous or asynchronous communication
Buffering
Basic Message-passing Primitives

A. Frank - P. Weisberg
Message format
Consists of header and body
of message.
In Unix: no ID, only message
type.
Control info:
what to do if run out of buffer
space.
sequence numbers.
priority.
Queuing discipline:
usually FIFO but can also
include priorities.

A. Frank - P. Weisberg
Messages and Pipes Compared

A. Frank - P. Weisberg
Direct communication
Each process must explicitly name the recipient or
sender of communication.
send(P, message) – send a message to process P
receive(Q, message) – receive a message from process
Q
Communication link has the following properties
Link is automatically established. Process must only
know the identity of other process.
Link is associated with exactly two processes
B/w each pair of processes, exists exactly one link
Indirect communication
Messages are sent and received through mailboxes or ports.
Send(A, message) – send a message to mailbox A
Receive (A, message) – receive a message from mailbox A
Link properties
Link is established between a pair of processes only if both
members of the pair have a shared mailbox
Link may be associated with more than two processes
Between each pair of communicating processes, there may be
a number of different links, with each link corresponding to
one mailbox.
Synchronization
Message passing may be blocking (synchronous) or
non blocking (non-synchronous)
Blocking send – the sending process is blocked until the
message is received by the receiving process or mailbox.
Non blocking send – the sending process sends the
message and resumes operation
Blocking receive – the receiver blocks until a message is
available.
Non blocking receive – the receiver retrieves either a
valid message or NULL.
Buffering
Messages exchanged by communicating process reside
in temporary queue.
These queues are implemented in three ways:
Zero capacity
Bounded capacity
Unbounded capacity
Producer Code
void Producer(void){

int item;
Message m;

while(1){

receive(Consumer, &m);
item = produce();
build_message(&m , item ) ;
send(Consumer, &m);
}
}
Consumer Code
void Consumer(void){

int item;
Message m;

while(1){

receive(Producer, &m);
item = extracted_item();
send(Producer, &m);
consume_item(item);
}
}
Difference
Shared memory Message passing
1. Processes exchange 1. Direct exchange of
information by reading or messages.
writing into the shared
2. Used for exchanging small
region.
amounts of data.
2. Used for exchanging large
amount of data 3. Slower than shared
3. Faster than message memory because it is
passing (system calls implemented using system
required only to establish calls , which involves kernel
shared region and rest all intervention.
access are treated as
normal memory access)
Communications in Client-Server
Systems
There are various mechanisms:
1. Pipes
2. Sockets (Internet)
3. Remote Procedure Calls (RPCs)
4. Remote Method Invocation (RMI,
Java)
Pipes
Acts as a conduit allowing two processes to
communicate.
Some 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
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.
Ordinary Pipes
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.
Sockets
A socket is defined as an endpoint for
communication.

Concatenation of IP address and port.

The socket 161.25.19.8:1625 refers to port


1625 on host 161.25.19.8.

Communication consists between a pair of


sockets.
Socket Communication

A. Frank - P. Weisberg
Remote Procedure Calls (RPCs)

RPC abstracts a Local Procedure Call (LPC)


between processes on a networked system.
Stubs – client-side proxy for the actual
procedure existing on the server.
The client-side stub locates the server and
marshals the parameters.
The server-side stub/skeleton receives this
message, unpacks the marshaled parameters,
and performs the procedure on the server.
Vice versa happens on the opposite direction.
Remote
Procedure
Call
Mechanism

A. Frank - P. Weisberg
Execution of RPC

A. Frank - P. Weisberg
References
Silberschatz, Abraham, et al. Operating system
concepts. Edition-8. Reading: Addison-Wesley
Remote Method Invocation
Remote Method Invocation (RMI) is a Java mechanism
similar to RPCs.
RMI allows a Java program on one machine to invoke a
method on a remote object.

Вам также может понравиться