06B Systems Programming-IPC-Pipes
06B Systems Programming-IPC-Pipes
Inter-Process
Communication
Inter-Process Communication
• Inter-Process Communication(IPC) is the generic term describing how
two processes may exchange information with each other.
• In general, the two processes may be running on the same machine
or on different machines
• This communication may be an exchange of data for which two or
more processes are cooperatively processing the data or
synchronization information to help two independent, but related,
processes schedule work so that they do not destructively overlap.
Inter-Process Communication Methods
• Local → Processes running on the same machine
• Pipe
• Signal
• MultiProcessing (MP) in multi-core/multi-processor architecture
• Distributed → Processes running on different machines
• Message Passing Interface (MPI)
Inter-process Communication
using Pipe
Pipes
• Pipes are an inter-process communication mechanism that allow two
or more processes to send information to each other.
• commonly used from within shells to connect the standard output of one
utility to the standard input of another.
• For example, here’s a simple shell command that determines how many users
there are on the system:
$ who | wc -l
• The who utility generates one line of output per user. This output is then
“piped” into the wc utility, which, when invoked with the “-l” option, outputs
the total number of lines in its input.
Pipes
who pipe wc
A simple pipe
PIPEs are more powerful constructs
• Child process exit code send to parent process → limit of only 1 int
• What if child process wants to send larger data of different data types
to parent process?
• Also what if parent process wants to send some information to
different child processes?
• if information is static it can be initialized in parent before fork() to share a
content of a variable with the respective child.
• But what if parent generates information along the way after fork()?
Pipes
• It’s important to realize that both the writer process and the reader
process of a pipeline execute concurrently;
• a pipe automatically buffers the output of the writer and suspends the writer
if the pipe gets too full.
• Similarly, if a pipe empties, the reader is suspended until some more output
becomes available.
• All versions of UNIX support unnamed pipes, which are the kind of
pipes that shells use.
• System V also supports a more powerful kind of pipe called a named
pipe.
Unnamed Pipes: pipe() System Call
• An unnamed pipe is a unidirectional communications link that
automatically buffers its input ( the maximum size of the input varies
with different versions of UNIX, but is approximately 5K ) and may be
created using the pipe() system call.
• Each end of a pipe has an associated file descriptor:
• The “write” end of the pipe may be written to using write()
• The “read” end may be read from using read()
• When a process has finished with a pipe’s file descriptor. it should
close it using close()
• Note read(), write and close() are unbuffered I/O System Calls that we
have studied earlier
Unnamed Pipes: pipe() System Call
int pipe( int fd[2] )
• pipe() creates an unnamed pipe and returns two file descriptors:
• The descriptor associated with the “read” end of the pipe is stored in fd[0],
• The descriptor associated with the “write” end of the pipe is stored in fd[1].
PIPEs are generally Unidirectional
• Assume that the following code was executed:
int fd[2];
pipe(fd);
data structure as shown below will be created
• Code uses dup2 system call: duplicate a file descriptor and we can use
the file descriptors interchangeably to refer to the file
int dup2(int oldfd, int newfd);
Unnamed Pipes: pipe() System Call
$ who ---> execute “who” by itself.
ubuntu18 :0 2019-02-13 10:11 (:0)