0% found this document useful (0 votes)
14 views

IPC - Pipe Socket

Pipes and sockets allow for inter-process communication (IPC). Pipes implement a first-in, first-out buffer to transfer data between processes unidirectionally, while sockets provide bidirectional communication using addresses and ports. There are named and unnamed pipes, with named pipes allowing any process to communicate using a known name. Sockets define endpoints for communication and functions for initiating connections, sending/receiving data, and terminating connections gracefully across networks.

Uploaded by

vivek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

IPC - Pipe Socket

Pipes and sockets allow for inter-process communication (IPC). Pipes implement a first-in, first-out buffer to transfer data between processes unidirectionally, while sockets provide bidirectional communication using addresses and ports. There are named and unnamed pipes, with named pipes allowing any process to communicate using a known name. Sockets define endpoints for communication and functions for initiating connections, sending/receiving data, and terminating connections gracefully across networks.

Uploaded by

vivek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Pipes & Sockets

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

• Unnamed pipes are used as we saw in Unix


cat myfile | grep key | sort | lpr

• The parent process (the shell or shell script


that creates the pipes) also spawns the child
processes that access the pipe
– cat, grep, sort, and lpr in this case

5
Named Pipes

• Named pipes can be accessed by any


process that “knows the name”

• Named pipes appear in the user’s directory


list
$ls -l
pwr_r__r__ 1 krf 0 Mar 27 19:33 mypipe

6
Named Pipe Creation

• Named pipes are created using the mknod or


the mkfifo commands
$ mkfifo name
$ mknod name p
• Make sure you remove (rm) your pipes after
use!

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

Address Space for p1

Info to be
Info copy
shared
write(pipe[1], …); read(pipe[0]);
System Call Interface

pipe for p1 and p2

write function read function


UNIX Pipes (cont)
• File handles are copied on fork
int pipeID[2];
. . .
pipe(pipeID);
. . .
if(fork() == 0) { /* the child */
. . .
read(pipeID[0], childBuf, len);
<process the message>;
. . .
} else { /* the parent */
. . .
write(pipeID[1], msgToChild, len);
. . .
}
int pipe(int fd[2]) -- creates a pipe and returns two file descriptors,
fd[0], fd[1]. fd[0] is opened for reading, fd[1] for writing
Code fragment: Named Pipes
Char string[]=“Hello”;
main(int argc, char* argv)
int fd;
char buf[256];

fd = mknod(“fifo”,010777,0) /* Create Pipe


RW=all*/

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

File transfer apps (FTP), Web browsers


(HTTP), Email (SMTP/ POP3), etc…
3 Types of Socket

• Stream sockets interface to the TCP


(transport connect protocol).
• Datagram sockets interface to the UDP
(user datagram protocol).
• Raw sockets interface to the IP (Internet
protocol).

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

Internet address = 138.37.94.248 Internet address = 138.37.88.249

21
Sockets used for streams

Requesting a connection Listening and accepting a connec

s = socket(AF_INET, SOCK_STREAM,0) s = socket(AF_INET, SOCK_STREAM,0)

bind(s, ServerAddress);
listen(s,5);
connect(s, ServerAddress)
sNew = accept(s, ClientAddress);

write(s, "message", length) n = read(sNew, buffer, amount)


socket() TCP Server
bind() Well-known port

TCP Client listen()

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

You might also like