Class Notes (808,208)
Canada (493,072)
CSC209H1 (41)
Karen Reid (20)


5 Pages
Unlock Document

University of Toronto St. George
Computer Science
Karen Reid

Inter-process Communication (Pipes)  wait, process term and zombie moved to mar01c o Producer/Consumer Problem  Exchanging data between processes  Simple example: who | wc -l o After fork() is called we end up with two independent processes  Both the writing process (who) and the reading process (wc) o Cannot use variables to communicate btwn processes since they each of a pipeline execute concurrently have separate address spaces, and separate memory  A pipe is usually implemented as an internal OS buffer o One easy way to communicate is to use files  It is a resource that is concurrently accessed by the reader  Ex. Process A wirtes to a file, process B reads from it and the write so it must be managed carefully  Consumer blocks when buffer is empty  Producer blocks when buffer is full  They should run independently as far as buffer capacity and contents permits  They should never be updating the buffer at the same instant (otherwise data integrity cannot be guaranteed)  Detailed pipe() diagram o Start with pipe() – half-duplex (one-way) communication  File Objects and File Descriptors o The stdio library provides FILE objects which handle buffering  Why buffering? Efficiency o FILE objects are built on top of file descriptors  A file descriptor is an index into a pre-process table of open file descriptors  Also use file descriptors for other communications (pipes, sockets) o File Descriptors  Used by low-level I/O  open(), close(), read(), write() o Then fork()  declared as an integer  int fd  a useful system call to convert a FILE object to a fd  int fileno(FILE *fp);  of course it is possible to assign a stream interface to a file descriptor  FILE *fdopen(int fd, const char *mode); o Processes and File Descriptors  File descriptors are handles to open files  File descriptors belong to processes not programs  They are a process's link to the outside world  FDs preserved across fork and exec o Pick a direction of data flow  Child to Parent – close f[1] in parent, f[0] in child  Pipe and File Descriptors  Parent to Child – close f[0] in parent, f[1] in child o A forked child inherits file descriptors from its parent o pipe() creates an internal system buffer and two file descriptors, one for reading and one for writing o after the pipe call, the parent and child should close the file descriptors for the opposite direction  leaving them open does not permit full-duplex communication o Reading and Writing to a pipe  A read on an empty pipe will block until there is something to read  A write on a full pipe will block until there is more space  Pipes have a finite size  A write to a pipe that has been closed by other end will result in a SIGPIPE or "Broken pipe" message  A read will return 0 if the write end of the pipe is closed  dup2()  pipe(fd); o Often we want the stdout of one process to be connected to the stdin of another process int dup2(int oldFD, int newFD) o Set one file descriptor to the value of another  File descriptors newFD and oldFD now refer to the same file  If newFD is open, it is first automatically closed o Note that dup2() refer to file descriptors, not streams o Ex.1 oldfd = open(“file”); dup2(oldfd, newfd); o Ex.2  Code /* equivalent to “sort < file1 | uniq” */  if((pid = fork()) == 0) { int fd[2], pid; int filedes = open("file1", O_RDONLY); dup2(filedes, fileno(stdin)); close(filedes); pipe(fd); if((pid = fork()) == 0) { // child dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort", "sort", (char *) 0); } else if(pid > 0) { // parent dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq", "uniq", (char *) 0); } else { perror("fork"); exit(1); }  dup2(fd[1], fileno(stdout));  int filedes = open("file1", O_RDONLY);  dup2(filedes, fileno(stdin));  close(fd[0]); close(fd[1]);  close(filedes);  dup2(fd[0], fileno(stdin));  close(fd[1]); close(fd[0]);  Ex.4 pipe_ex.c (mar01c) o Example using pipe that writes from parent to child o Write as many bytes are you are expected to read 1. #include 2. #include 3. #include 4. #include 5. 6. int main() { 7. char line[256] = "initial"; 8. int fd[2]; 9. int pipe_ret; 10. if ((pipe_ret = pipe(fd)) == -1) { 11. perror("pipe"); 12. exit(1); 13. } 14.  popen() and pclose() 15. int r; FILE *popen(const char *command, const char *type); 16. if((r = fork()) == -1) { 17. perror("fork"); o popen() simplifies the sequences of 18. exit(1);  generating a pipe 19. 20. } else if (r > 0) { // parent: write to child  forking a child process 21. close(fd[0]); // close read end of pipe  duplicating file descripotrs 22. strncpy(line, "hello", sizeof(line));  passing command execution via an exec() 23. printf("[%d] PARENT %d %s\n", getpid(), r, line); 24. sleep(10); // wait 10 seconds o Ex.3 FILE *pipeFP; 25. printf("now writing\n"); pipeFP = popen(“/usr/bin/ls *.c”, “r”); 26. write(fd[1], line, sizeof(line)); 27. printf("finished writing\n"); 28. 29. } else { // child: read from parent 30. close(fd[1]); // close write end of pipe 31. printf("[%d] CHILD %d\n", getpid(), r); 32. char read_buffer[256]; 33. printf("calling read \n"); 34. read(fd[0], read_buffer, sizeof(line)); 35. printf("[%d] CHILD new: %s, old: %s\n", getpid(), 36. read_buffer, line); 37. close(fd[1]); 38. } 39. 40. return 0; 41. } o Output $ gcc -Wall -g -o pipe_ex pipe_ex.c $ ./pipe_ex [559] parent 560 hello [560] child 0 calling read now writing finished writing [560] child new: hello, old: initial  Ex.5 fork_pipe.c 85. // close all the pipes to the children 1. #include 86. for (i = 0; i < count; i++ ) { 2. #include 87. if (close(fd[i][1]) != 0) { 3. #include 88. perror("close"); 89. exit(1); 4. #include 90. } 5. 91. } 6. int main(int argc, char ** argv) { 7. if (argc != 2) { 92. 8. printf("Usage: fork_pipe \n"); 93. // wait for all the children 9. exit(1); 94. for (i = 0; i < count; i++) { 10. } 95. wait(&status); 11. int count = strtol(argv[1],NULL,10); 96. if (WIFEXITED(status)) { 97. printf("child exit with return code of %d
More Less

Related notes for CSC209H1

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.