CS312 Lec 13
CS312 Lec 13
CS312 Lec 13
PROGRAMMING
LECTURE # 13
PIPES
Course Code: CS 312
Course Instructor: Dr. Sarah Iqbal
1
AGENDA FOR TODAY
Overview of Pipes
Examples
2
INTRODUCTION
Historyof Pipes: Pipes history goes back to 3rd edition of UNIX in 1973.
They have no name and can therefore be used only between related
processes. This was corrected in 1982 with the addition of FIFOs
Byte stream: When we say that a pipe is a byte stream, we mean that
there is no concept of message boundaries when using a pipe. Each
read operation may read an arbitrary number of bytes regardless of
the size of bytes written by the writer. Furthermore, the data passes
through the pipe sequentially, bytes are read from a pipe in exactly
the order they were written. It is not possible to randomly access the
data in a pipe using lseek()
Pipes are unidirectional: Data can travel only in one direction. One
end of the pipe is used for writing, and the other end is used for
reading
3
READING FROM A PIPE
When a process reads bytes from a pipe, those bytes are removed
from the pipe (Destructive read semantics) Attempts to read from a
pipe that is currently empty block until at least one byte has been
written to the pipe.
Ifthe write end of a pipe is closed, then a process reading from the
pipe will see end-of-file (i.e., read() returns 0) once it has read all
remaining data in the pipe.
Iftwo processes try to read from the same pipe, one process will get
some of the bytes from the pipe, and the other process will get the
other bytes. Unless the two processes use some method to coordinate
their access to the pipe, the data they read are likely to be
incomplete
4
WRITING TO A PIPE
5
SIZE OF PIPE
A pipe is simply a buffer maintained in kernel memory. This buffer has a
maximum capacity. Once a pipe is full, further writes to the pipe block
until the reader removes some data from the pipe.
Ifmultiple processes are writing to a single pipe, then it is guaranteed
that their data won’t be intermingled if they write no more than
PIPE_BUF bytes at a time.
This
is because writing PIPE_BUF number of bytes to a pipe is an atomic
operation. On Linux, value of PIPE_BUF is 4096 bytes on x86-32
When writing more bytes than PIPE_BUF to a pipe, the kernel may
transfer the data in multiple smaller pieces, appending further data as
the reader removes bytes from the pipe. The write() call blocks until all
of the data has been written to the pipe
When there is a single writer process, this doesn’t matter. But in case of 6
multiple writer processes, this may cause problems
FILE DESCRIPTOR TO FILE CONTENTS
…
… …
OPEN_MAX — 1
7
8
CREATING AND USING PIPES
A successful call to pipe() returns two open file descriptors in the array filedes:
one for the read end of the pipe ( filedes[0]) and one for the write end (
filedes[1]).
As with any file descriptor, we can use the read() and write() system calls to
perform I/O on the pipe.
Once written to the write end of a pipe, data is immediately available to be
read from the read end.
A read() from a pipe obtains the lesser of the number of bytes requested and
the number of bytes currently available in the pipe
We can also use the stdio functions (printf(), scanf(), and so on) with pipes by
first using fdopen() to obtain a file stream corresponding to one of the
descriptors in files 9
USING PIPES
A pipe has few uses within a single process. Normally, we use a pipe to allow
communication between two processes. To connect two processes using a
pipe, we follow the pipe() call with a call to fork(). During a fork(), the child
process inherits copies of its parent’s file descriptors
10
USING PIPES…
11
12
READING & REFERENCES
13