pipes - Unix Pipes (CSE 422S) Dante Cannarozzi Washington...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Pipes (CSE 422S) Dante Cannarozzi Washington University djc2@cse.wustl.edu www.cse.wustl.edu/~djc2 2-Ken Wong, Sep 2008 Topics Interprocess Communication (IPC) with named and unnamed pipes Unbuffered I/O system calls ( open , read , write , close ) unlink system call Pipes (Half Duplex) pipe ( ) FIFOs (Named Pipes) mkfifo system call and mkfifo command Examples Simple FIFO Simple pipe Structured messages 3-Ken Wong, Sep 2008 In A Nutshell pipe fork close, close mkfifo open, open User Process fd[0] fd[1] OS Kernel Pipe Pipe fd[0] fd[1] fd[0] fd[1] Parent Child head tail Unnamed Pipe User Process OS Kernel Pipe Pipe File User Processes fd[0] fd[1] fd[0] fd[1] Parent Child User Processes fd fd Parent Child Pipe File Named Pipe 4-Ken Wong, Sep 2008 Pipe (Unnamed FIFO) in the Shell The Script xxx Output of ps lx while xxx is sleeping y | z & # y,z are shell scripts which just sleep sleep 20 wait $! # $! is PID of most recently # backgrounded process (/bin/sh ./z) UID PID PPID TIME COMMAND 1024 17273 17271 0:03 tcsh 1024 17648 17273 0:00 /bin/sh ./xxx 1024 17649 17648 0:00 /bin/sh ./z 1024 17650 17648 0:00 /bin/sh ./y 1024 17651 17650 0:00 sleep 20 1024 17652 17648 0:00 sleep 20 1024 17653 17649 0:00 sleep 20 5-Ken Wong, Sep 2008 Unix Pipe Movie Parent creates pipe Parent forks child User Process fd[0] fd[1] OS Kernel Pipe User Process OS Kernel Pipe fd[0] fd[1] fd[0] fd[1] Parent Child Parent closes head Child closes tail Pipe fd[0] fd[1] fd[0] fd[1] Parent Child head tail 6-Ken Wong, Sep 2008 Unix Pipe Example 1 (1) ... Requisite includes and stdinc.h ... int main(void) { ... Variable definitions ... Pipe(fd); pid = Fork( ); if (pid > 0) { // parent close(fd[0]); // close head of pipe Write (fd[1], msg, msgsz);// write msg to pipe } else { // child close(fd[1]); // close tail of pipe n = Read (fd[0], line, msgsz); // read msg from pipe printf(%s \ n, line); // write msg to stdout } return 0; } 7-Ken Wong, Sep 2008 Unix Pipe Example 1 (2) Variable Definitions stdinc.h int Pipe(int fd) Error if pipe( ) returns negative ssize_t Write(int fd, const void *buf, size_t n) If write( ) returns negative: Error occurred and errno is set Call perror( ) to display human-readable error message If write( ) doesnt return n: Error (unusual) ssize_t Read(int fd, const void *buf, size_t n) If read( ) returns negative: Error occurred and errno is set If read( ) returns 0: EOF (pipe was closed) If read( ) returns less than n: OK int n, fd[2]; pid_t pid; char line[MAXLINE]; char *msg = hello \ n; const int msgsz = sizeof(msg); 8-Ken Wong, Sep 2008 Unix Pipes Limitations They are half-duplex (data flows in one direction) Can only be used between 2 processes that have a common ancestor Process A creates a pipe P Process A calls fork to create child B A pipe is used between processes A and B (i.e.,A | B)A pipe is used between processes A and B (i....
View Full Document

This document was uploaded on 11/13/2009.

Page1 / 28

pipes - Unix Pipes (CSE 422S) Dante Cannarozzi Washington...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online