DOC PREVIEW
Princeton COS 217 - Inter-process Communication

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

Princeton COS 217 - Inter-process Communication

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Inter-process Communication
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inter-process Communication and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inter-process Communication 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?