0% found this document useful (0 votes)
141 views15 pages

Pipes: Pipes Represent A Channel For Interprocess Communication

Pipes allow for interprocess communication by providing a channel for transferring data between the write end of one process and the read end of another process. Pipes can be unnamed, created with the pipe system call and used between related processes like parent and child, or named pipes which have a directory entry and can be used between unrelated processes. Standard input/output of processes can be redirected to the pipe ends using functions like dup and dup2 to communicate solely through the pipe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views15 pages

Pipes: Pipes Represent A Channel For Interprocess Communication

Pipes allow for interprocess communication by providing a channel for transferring data between the write end of one process and the read end of another process. Pipes can be unnamed, created with the pipe system call and used between related processes like parent and child, or named pipes which have a directory entry and can be used between unrelated processes. Standard input/output of processes can be redirected to the pipe ends using functions like dup and dup2 to communicate solely through the pipe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Pipes

Pipes represent a channel for Interprocess Communication


Pipe
* Pipe
•A simple, unnamed pipe provides a one-way flow of data.
– Can be thought as a special file that can store a limited amount of data
in a first-in-first-out manner, exactly akin to a queue.
•Other variations:
• Stream pipes
• FIFOs
•An unnamed pipe is created by calling pipe(), which returns an
array of 2 file descriptors (int).
– The file descriptors are for reading and writing, respectively
pipe System Call (unnamed)
Creates a half-duplex pipe.
• Include(s): < unistd.h>
• Syntax: int pipe (int pipefd[2]);
• Return: Success: 0; Failure: -1; Sets errno: Yes
– What does it mean to return errno?
• Arguments: None
• If successful, the pipe system call will return two integer file
descriptors, pipefd[0] and pipefd[1].
– pipefd[1] is the write end to the pipe.
– pipefd[0] is the read end from the pipe.
• Example (pipedemo.c)
• Parent/child processes communicating via unnamed pipe.
Features of Pipes
Features of Pipes
• On many systems, pipes are limited to 10 logical blocks,
each block has 512 bytes.
• As a general rule, one process will write to the pipe (as if it
were a file), while another process will read from the pipe.
• Data is written to one end of the pipe and read from the
other end.
• A pipe exists until both file descriptors are closed in all
processes
Piping Between Two Processes
• The pipe is represented in an array of 2 file descriptors (int)
Writing process Reading process

read fd0
write fd1

pipe
flow of data
Initial State of Pipe
Writing process Reading process

read fd0 read fd0


write fd1 write fd1

pipe

Chaos!?! Some questions…


Initial State of Pipe: Questions
• How come each end works both ways???
• How is a a pipe shared between two processes?
– When is it declared?
– How must the processes be related?
• How can the pipe be harnessed to accomplish one-way
communication?
• How can full-duplex be achieved?
• If multiple processes share one end of a pipe, how can we
be sure transmissions aren’t interleaved?
Full Duplex Communication via Two Pipes
Two separate pipes, say p0 and p1
Process A Process B

write p01 write p11


read p10 read p01

p0

p1
write System Call
Function:
• To write nbytes to the write end of a pipe.
– If a write process attempts to write to a full pipe, the default action is
for the system to block the process until the data is able to be
received.
• Include(s): <unistd.h>
• Syntax: ssize_t write (int fd, const void *buf, size_t nbytes);
– just the write system call
• Returns
– success: Number of bytes written; Failure; -1; Sets errno:Yes.
• Arguments
– int fd: file descriptor;
– const void *buf: buffer;
– size_t nbyte: number of bytes in buffer
read System Call
Function:
• To read nbytes from the read end of a pipe.
– if read is attempted on an empty pipe, the process will block until data
is available.
• Includes: <unistd.h> <sys/types.h> <sys/uio.h>
• Syntax: ssize_t read(int fd, const void *buf, size_t nbytes);
• Return
– success: Number of bytes read;
– Failure; -1; Sets errno:Yes.
– EOF (0): write end of pipe closed
• Arguments
– int fildes: file descriptor;
– const void *buf: buffer;
– size_t nbyte: number of bytes
Unnamed Pipes
• Unnamed pipes can only be used
between related process, such as
parent/child, or child/child process.
• Unnamed pipes can exist only as long as
the processes using them.
• An unnamed pipe is constructed with the
pipe system call.
– See the popen command for an automated
method of using unnamed pipes
• Demo: popenDemo.cpp
Named Pipes
• When generated, named pipes have a directory
entry.
• With the directory entry are file access permissions
and capability for unrelated processes to use the
pipe file.
• Problem: Processes using the pipe know nothing
about each other
– Why is this a problem??
• Named pipes can be created at the shell level or
within a program.
• See mknod or mkfifo commands
Redirecting Standard I/O
A process that communicates solely with
another process doesn’t use its standard I/O.
• If process communicates with another
process only via pipes, redirect standard I/O
to the pipe ends
• Functions: dup, dup2
dup & dup2
#include <unistd.h>
int dup(int fildes);
• Returns a new file descriptor that is a copy of
filedes
• File descriptor returned is first available file
descriptor in file table.
• For example, to dup a read pipe end to stdin (0),
close stdin, then immediately dup the pipe’s read
end.
• Close unused file descriptors; a process should
have only one file descriptor open on a pipe end.
dup & dup2
#include <unistd.h>
int dup2(int fromFD,int toFD);
• Duplicate fromFD to toFD. If toFD is
open, it is closed first.
• For example, if a pipe’s ends are in array
pipefd, dup2(pipefd[1],1) redirects stdout to
the write end of the pipe.
• You still must close the unused pipe end, in
this case pipefd[1]

You might also like