Inter-process communication!
Processes need a way to interact with each other!
Examples!
- I/O redirection under UNIX shell!
- GUI apps communicate with window system to draw on
screen and receive mouse/keyboard events!
- Interaction between desktop apps. e.g. mail program
requesting web browser to follow a link included in an
email!
The OS must provide primitives to facilitate this!
There are several different ways to do this, including:!
- Pipes!
- Message passing!
Inter-process communication!
Pipes and message passing!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 1!
2010 University of Adelaide
!
Pipes!
Process 2
!Operating Systems
!
Appear as special files in the filesystem!
Processes can access these just like any other type of file,
by opening, reading from, and writing to them!
$ mkfifo test_pipe!
$ ls -l!
total 0!
prw-r--r-- 1 peter
$ nl test_pipe!
Can be created using the UNIX shell:!
- grep printf *.c | nl!
And also using the pipe system call!
- int fds[2]!
- pipe(fds)
And by creating special files!
- mkfifo test_pipe!
Pipes are uni-directional (one way)!
Two types of pipes: named and unnamed!
2010 University of Adelaide
!
!!
!Pipes_MP/Slide 2!
Named pipes!
A pipe allows one process to send a stream of data to another process!
Process 1
!Operating Systems
!
peter
3 Aug 14:40 test_pipe|!
Then in another window!
$ ls -l ~ > test_pipe!
Data written to the pipe by ls is read by nl!
!!
!Pipes_MP/Slide 3!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 4!
Unnamed pipes!
Pipes within the kernel!
Created by a process using the pipe system call!
Never referenced from the filesystem!
Can only be shared between processes using fork!
int fds[2];!
pipe(fds);!
if (0 == fork()) { /* child */!
close(fds[1]);!
char buf[1024];!
int r = read(fds[0],buf,1024);!
/* ... */!
}!
else { /* parent */!
close(fds[0]);!
write(fds[1],"Hello",5);!
/* ... */!
}!
2010 University of Adelaide
!
!Operating Systems
!
Pipe writer
fd 0
Process 1
... Process 2
fd 6
!!
!Pipes_MP/Slide 5!
2010 University of Adelaide
!
fd 5
!Operating Systems
!
!!
!Pipes_MP/Slide 6!
Message passing!
When read is called!
- If the pipe contains data, return data immediately!
- If the pipe is closed for writing, return 0 immediately!
- If the pipe is empty, block process until some more data
becomes available!
When write is called!
- If the pipe has enough space for all the data, add it to
the pipe and return immediately!
- If there is not enough space, write as much as possible,
but block process until all data can be written!
File descriptors can optionally be put into a non-blocking
mode, where read and write always return immediately!
!Operating Systems
!
fd 0
...
Pipes: blocking!
2010 University of Adelaide
!
Pipe reader
!!
!Pipes_MP/Slide 7!
Processes can send messages to each other!
Process 1
Process 2
Each message is typically represented as an array of bytes,
with the sender and receiver agreeing on a common format!
Primitives!
- send(dest,message)!
- receive(source,message)!
Not generally supported by UNIX!
But common in microkernels, e.g. MINIX!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 8!
Message passing!
Message passing - Blocking!
Like pipes, message passing is unidirectional!
Bi-directional communication can be implemented by having
the receiver send back a reply message!
Some implementations provide a send_receive primitive
which combines a send to a particular destination, followed
by a reply!
Message passing can be implemented at different layers:!
- Kernel (MINIX)!
- Library (MPI, PVM) - runs in user mode!
- Programming language (Erlang) - runs in user mode!
Microkernels use message passing extensively!
- In MINIX, all system calls are implemented as send/
receive combinations!
Both send and receive can be blocking or non-blocking!
For send!
- The process blocks until the recipient executes a
receive call!
For receive!
- Process blocks until a message is available!
Blocking tends avoid the need for (potentially large)
intermediate buffers!
- But can make programming less flexible!
- Sometimes you dont want to wait for the receive to get
the message before continuing!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 9!
Implementing message passing!
2010 University of Adelaide
!
!Operating Systems
!
Implementing message passing!
For non-blocking sends, the kernel needs to keep the
message data in memory until the destination calls
receive!
Each process has a mailbox which stores the messages
sent to it that it has not yet obtained via receive!
The mailboxes are stored in kernel memory!
-%".%/'*%*#"+
012/3#4'5#"'
6"#$%&&'(
012/3#4'5#"'
6"#$%&&',
!"#$'&+&8%*'$1//
%"&"'("'&+&8%*'$1//
7""1+'$#.812.2.9'
#:89#2.9'
*%&&19%';181
7""1+'2.'<=2$='8#'
&8#"%'2.$#*2.9'
*%&&19%';181
!"#$%&&'()&'*%*#"+
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 10!
!!
!Pipes_MP/Slide 11!
2010 University of Adelaide
!
!"#$%&&',)&'*%*#"+
!Operating Systems
!
!!
!Pipes_MP/Slide 12!
Message passing - Distribution!
Synchronisation issues!
Message passing can be used for distributed operating
systems!
- Instead of transferring a message directly in memory, it
is sent over a network!
- Processes on different machines can interact with each
other!
In theory, a microkernel based on message passing could
provide network transparency!
- Processes interact without concern for if the interaction
is local or remote!
- The OS can hide details of distribution from processes!
This is an interesting research area but has not seen use in
mainstream OSs to date!
When processes or threads are interacting, we must be
careful to avoid certain types of problems, e.g:!
- Deadlock - each process is waiting for another before it
can continue, therefore neither does anything!
- Race conditions - where the order in which messages
arrive determines program behaviour, leading to
incorrect results in certain situations!
There are tools we can use to avoid these!
- Semaphores!
- Mutexes!
- Monitors!
The problem is mostly present with shared memory (i.e.
threads), but can also affect message passing!
Well cover these issues in a later lecture!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 13!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 14!
Summary!
Pipes and message passing are two ways for processes to
communicate, without sharing memory!
Pipes are a common feature of UNIX, and provide
unidirectional streams between processes!
Message passing is used by micro-kernels and some userlevel libraries to allow processes to exchange one-way
messages with each other!
Synchronisation issues become important when dealing with
complex interactions between processes!
2010 University of Adelaide
!
!Operating Systems
!
!!
!Pipes_MP/Slide 15!