Pipes - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Pipes

Description:

A pipe is a one-way communications channel which ties one process to another. ... use the read and write system calls to send data down the pipe and retrieve it ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 10
Provided by: davidk67
Category:

less

Transcript and Presenter's Notes

Title: Pipes


1
Pipes
  • A pipe is a one-way communications channel which
    ties one process to another.
  • We can use the read and write system calls to
    send data down the pipe and retrieve it at the
    other end.
  • Pipes are available at the command line to tie
    utility programs together.
  • A function called pipe( ) is available to the
    programmer to establish piped communications.
  • int pipe (int filedes )
  • where filedes is a two element int array which
    holds the file descriptors to identify the pipe.
    If the call is successful, filedes0 will be
    opened for reading from the pipe, and filedes1
    will be opened to write to the pipe.

2
Using Pipes
  • / pipetest write and read a pipe /
  • include ltstdio.hgt
  • int main(void)
  • char inbuf100
  • int pfd2, nread
  • if (pipe(pfd) -1)
  • perror(pipe call)
  • exit(1)
  • if (write (pfd1, hello, 6) -1)
  • perror (write to pipe failed)
  • exit(1)

3
Using Pipes
  • switch (nread read(pfd0, inbuf,
    sizeof(inbuf)))
  • case -1
  • perror (read failed)
  • case 0
  • perror (EOF encountered)
  • exit(0)
  • default
  • printf (read d bytes s\n, nread ,
    inbuf)

4
Using Pipes with Multiple Processes
  • / the pwrite process /
  • include ltstdio.hgt
  • int main (void)
  • int pfd2
  • char fdstr10
  • if (pipe(pfd) -1)
  • perror (pipe failed)
  • exit(1)
  • switch (fork( ))
  • case -1
  • perror (fork failed)
  • exit(1)

5
Using Pipes with Multiple Processes
  • case 0
  • if (close(pfd1) -1)
  • perror (pipe close failed)
  • exit(1)
  • sprintf (fdstr, d, pfd0)
  • execlp (./pread, pread, fdstr, NULL)
  • perror (execlp failed)
  • exit(1)
  • if (close (pfd0) -1)
  • perror (2nd close failed)
  • if (write( pfd1, hello, 6) -1)
  • perror (write failed)

6
Using Pipes with Multiple Processes
  • / pread - the child process /
  • include ltstdio.hgt
  • int main(int argc, char argv )
  • int nread, fd
  • char s100
  • fd atoi (argv1)
  • printf (reading file descriptor d\n, fd)
  • switch (nread read (fd, s, sizeof(s)))
  • case -1
  • perror (read failed)
  • exit(1)
  • case 0
  • perror (Reached end of file)
  • exit (1)
  • default
  • printf (read d bytes s\n, nread, s)

7
Connecting Processes - A Procedure
  • Make the pipe
  • Fork to create the reading child
  • In the child, close the writing end of the pipe,
    and do any other required preparations
  • In the child, execute the child program
  • In the parent, close the reading end of the pipe
  • If a second child is to write to the pipe, create
    that child, make appropriate preparations, and
    execute its program. If the parent is to write,
    do so at this time.

8
Advanced Communications Mechanisms
  • FIFOs, or Named Pipes are available to overcome
    two shortcomings of regular pipes
  • 1. pipes can only connect processes that share a
    common ancestry.
  • 2. pipes cannot be permanent. They are created
    when needed and destroyed when processes
    terminate.
  • Named Pipes are given UNIX filenames and are
    permanent fixtures with all the normal attributes
    of a normal UNIX file.

9
Advanced Communications Mechanisms
  • Record Locking - allows a process to temporarily
    reserve part of a file for its own exclusive use.
    This is particularly applicable for database
    management.
  • Message Passing - these facilities allow a
    process to send and receive messages, with the
    messages being an arbitrary sequence of bytes or
    characters.
  • Semaphores - these provide a low-level means for
    process synchronization, although it cannot be
    used for transmission of large amounts of
    information.
  • Shared Memory - permits two or more processes to
    share data contained in specific memory segments.
Write a Comment
User Comments (0)
About PowerShow.com