1Inter-process CommunicationCS 2172Networks• Mechanism by which two processes exchange information and coordinate activities ComputerComputerComputerComputerComputerNetworkNetworkprocessprocess3Inter-process Communication• Pipes Processes must be on same machine One process spawns the other Used mostly for filters• Sockets Processes can be on any machine Processes can be created independently Used for clients/servers, distributed systems, etc.4Pipes• Provides an interprocess communication channel• A filteris a process that reads from stdin and writes to stdoutProcess A Process BoutputinputFilterstdinstdoutProg1 Filter Filter Prog25Pipes (cont)• Many Unix tools are written as filters grep, sort, sed, cat, wc, awk ...• Shells support pipesls | wc -lwho | grep mary | wc -lcat < foo | grep bar | sort > save• The combination of these features gives Unix incredible power and flexibility: Standard I/O I/O redirection Pipes678910Creating a Pipe• Pipe is a communication channel abstraction Process A can write to one end using “write” system call Process B can read from the other end using “read” system call• System callint pipe( int fd[2] );return 0 upon success –1 upon failurefd[0] is open for readingfd[1] is open for writing• Two coordinated processes created by fork can pass data to each other using a pipe.Process A Process Boutputinput11Pipe Exampleint pid, p[2];...if (pipe(p) == -1) exit(1);pid = fork();if (pid == 0) {close(p[1]);... read using p[0] as fd until EOF ...}else {close(p[0]);... write using p[1] as fd ...close(p[1]); /* sends EOF to reader */wait(&status);} parent childwriteread12Dup• Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor• Commonly used to implement redirection of stdin/stdout• Example: redirect stdin to “foo”int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);13Dup• Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor• Commonly used to implement redirection of stdin/stdout• Example: redirect stdin to “foo”int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);fd14Dup• Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor• Commonly used to implement redirection of stdin/stdout• Example: redirect stdin to “foo”int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);fd015Dup• Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor• Commonly used to implement redirection of stdin/stdout• Example: redirect stdin to “foo”int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);fd016Dup• Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor• Commonly used to implement redirection of stdin/stdout• Example: redirect stdin to “foo”int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);fd017Dup2• For convenience…dup2( int fd1, int fd2 );use fd2(new) to duplicate fd1 (old)closes fd2 if it was in use• Example: redirect stdin to “foo”fd = open(“foo”, O_RDONLY, 0);dup2(fd,0);close(fd);fd018Pipes and Standard I/Oint pid, p[2];if (pipe(p) == -1) exit(1);pid = fork();if (pid == 0) {close(p[1]);dup2(p[0],0);close(p[0]);... read from stdin ...}else {close(p[0]);dup2(p[1],1);close(p[1]);... write to stdout ...wait(&status); }parent childwritereadstdout stdinfd=0fd=119Pipes and Exec()int pid, p[2];if (pipe(p) == -1) exit(1);pid = fork();if (pid == 0) {close(p[1]);dup2(p[0],0);close(p[0]);execl(...); }else {close(p[0]);dup2(p[1],1);close(p[1]);... write to stdout ...wait(&status); }parent childwritereadstdout stdinfd=0fd=120Unix shell (sh, csh, bash, ...)• Loop Read command line from stdin Expand wildcards Interpret redirections < > | pipe (as necessary), fork, dup, exec, wait• Start from code on previous slides, edit it until it’s a Unix
View Full Document