Pipes - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Pipes

Description:

A pipe provides a one-way flow of data example: who | sort| lpr output of who is input to sort output of sort is input to lpr The difference between a file and a pipe: – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 7
Provided by: Virgi64
Learn more at: https://www.cs.gsu.edu
Category:
Tags: pipe | pipes

less

Transcript and Presenter's Notes

Title: Pipes


1
Pipes
  • A pipe provides a one-way flow of data
  • example who sort lpr
  • output of who is input to sort
  • output of sort is input to lpr
  • The difference between a file and a pipe
  • pipe is a data structure in the kernel.
  • A pipe is created by using the pipe system
    call int pipe(int filedes)
  • Two file descriptors are returned
  • filedes0 is open for reading
  • filedes1 is open for writing
  • Typical size is 512 bytes (Minimum limit defined
    by POSIX)

2
(No Transcript)
3
main() int pipefd2, n char buff100
if (pipe(pipefd) lt 0) error ("pipe
error") printf ("readfd d, writefd
d\n", pipefd0, pipefd1) if
(write(pipefd1, "hello world\n", 12) ! 12)
error ("write error") if
((nread(pipefd0, buff, sizeofbuff)) lt 0)
error ("read error") write (1, buff,
n) exit (0)
0 stdin 1 stdout 2 stderr
4
Question
If pipefd0 3 and pipefd1 4, what is the
output of the above code? Answer Note
"printf" is buffered, but "write" is not, so
output is hello world readfd 3, writefd
4
5
UNIX Pipe Creation
  • First, a process creates a pipe, and then forks
    to create a copy of itself.

6
Pipe Examples
  • Parent opens file, child reads file
  • parent closes read end of pipe
  • child closes write end of pipe
Write a Comment
User Comments (0)
About PowerShow.com