1 / 6

Pipes

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.

joann
Download Presentation

Pipes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 callint pipe(int* filedes); • Two file descriptors are returned • filedes[0] is open for reading • filedes[1] is open for writing • Typical size is 512 bytes (Minimum limit defined by POSIX)

  2. user process readfd writefd pipe flow of data kernel

  3. main() { int pipefd[2], n; char buff[100]; if (pipe(pipefd) < 0) { error ("pipe error"); } printf ("readfd = %d, writefd = %d\n", pipefd[0], pipefd[1]); if (write(pipefd[1], "hello world\n", 12) != 12) { error ("write error"); } if ((n=read(pipefd[0], buff, sizeof[buff])) < 0) { error ("read error"); } write (1, buff, n); exit (0); } 0: stdin 1: stdout 2: stderr

  4. Question If pipefd[0] = 3 and pipefd[1] = 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. parent process child process fork readfd readfd writefd writefd pipe flow of data kernel UNIX Pipe Creation • First, a process creates a pipe, and then forks to create a copy of itself.

  6. parent process child process fork readfd writefd pipe flow of data kernel Pipe Examples Parent opens file, child reads file • parent closes read end of pipe • child closes write end of pipe

More Related