IPC - Pipe Socket
IPC - Pipe Socket
1
Pipes
• Pipes are a way to allow processes to
communicate with each other
– Pipes implement one form of IPC (Inter process
Communication)
– This allows synchronization of process execution
• There are two kinds of pipes
– named pipes
– unnamed pipes
• Pipes are uni-directional
– They can only transfer data in one direction
– If you want two processes to have a two-way conversation,
you must use two pipes
2
Pipes Implement a FIFO
• A FIFO (First In, First Out) buffer is like a
queue or a line at a movie theater
• Elements are added at one end of the queue and
exit the other end in the same order
• There is no way for any individual element to
move ahead of another
3
• Traditional implementation of pipes uses
the file system for storage
– This allows processes to communicate even
though they don’t know what processes are at
the other end of the pipe
• Unnamed pipes can only be used between
processes that are children of the process
that initiated the pipe
• Named pipes solve this problem - any
process can communicate with another
using named pipes
4
Unnamed Pipes
5
Named Pipes
6
Named Pipe Creation
7
Using Named Pipes
• First, create your pipes
$ mknod pipe1
$ mknod pipe2
$ mknod pipe3
• Then, attach a data source to your pipes
$ ls -l >> pipe1 &
$ cat myfile >> pipe2 &
$ who >> pipe3 &
• Then, read from the pipes with your reader process
$ cat < pipe1 | lpr
$ spell < pipe2
$ sort < pipe3
Finally, delete your pipes
$ rm pipe[1-3]
8
UNIX Pipes
• The pipe interface is intended to look like a
file interface
– Analog of open is to create the pipe
– File read/write system calls are used to
send/receive information on the pipe
• What is going on here?
– Kernel creates a buffer when pipe is created
– Processes can read/write into/out of their
address spaces from/to the buffer
– Processes just need a handle to the buffer
UNIX Pipes
Info to be
Info copy
shared
write(pipe[1], …); read(pipe[0]);
System Call Interface
for (;;)
if (argc==2)
write (fd, string, 6);
else
read (fd, buf, 6);
SOCKETS
Why do we need sockets?
Provides an abstraction for
interprocess communication
Socket model
• Sockets
– a socket is an IPC mechanism for transmitting
data from one process to another both within a
single machine and between machines
– a socket is a bi-directional IPC channel
– Domains for sockets - a domain is the space from
which an address is drawn for a socket
• UNIX domain(AF_UNIX)
– uses the UNIX file system name space
– used for local IPC
• Internet domain(AF_INET)
– uses Internet address plus a port number
– used for local and remote IPC
A server accepting a call
Functions
–Define an “end- point” for
communication
–Initiate and accept a connection
–Send and receive data
–Terminate a connection
gracefully
Examples
19
Addresses, Ports and Sockets
• Like apartments and mailboxes
– You are the application
– Your apartment building address is the address
– Your mailbox is the port
– The post-office is the network
– The socket is the key that gives you access to the
right mailbox
Sockets and ports
agreed port
socket any port socket
message
client server
other ports
21
Sockets used for streams
bind(s, ServerAddress);
listen(s,5);
connect(s, ServerAddress)
sNew = accept(s, ClientAddress);
Socket() accept()
blocks until connection from client
connect() Connection establishment
write()
read()
process request
write()
read()
close()
read()
close()
Server process in unix system
A client process