Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 9 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan
Announcements Reading assignment –APUE Chapter 14 Section 14.2 –APUE Chapter 10
Review UNIX file system –File system abstraction –Directories –File descriptors Unix API Programming Examples and Techniques –Example with direct IO open, close, fdopen, lseek, unlink –Variable argument list
Review File I/O –File descriptors open, creat, close, dup, dup2 –I/O redirection Process management –fork, exit, wait, waitpid, execv Pipes –Named and unnamed pipes –Implementing pipe in a shell
Week 4 Topics Pipes –Named and unnamed pipes –Implementing pipe in a shell Signals –Catching signals –signal –Manipulate signal sets sigemptyset, sigfillset, sigdelset, sigaddset, sigdelset, sigismember –Manipulate signal mask sigprocmask –sigaction –kill
Pipes Named pipe (not our emphasis) Unnamed pipe Implementing pipe in a shell
Named pipe (not our emphasis) –Like a file (create a named pipe (mknod), open, read/write) – Can be shared by any process Unnamed pipe –An unnamed pipe does not associate with any physical file –It can only be shared by related processes (descendants of a process that creates the unnamed pipe) –Created using system call pipe() Named and unnamed pipes
int pipe(int fds[2]) –Creates a pipe and returns two file descriptors, fds[0] and fds[1 –fds[0] is used for reading and fds[1] for writing –The pipe has a limited size (64K on some systems) We cannot write to the pipe infinitely –See example1.c and example2.c The pipe system call
example3.c – Once the processes can communicate with each other, their execution order can be controlled An example for the use of a pipe
Example /usr/bin/ps –ef | /usr/bin/more How does the shell realize this command? –Create a process to run ps -ef –Create a process to run more –Create a pipe from ps -ef to more The standard output of the process to run ps -ef is redirected to a pipe streaming to the process to run more The standard input of the process to run more is redirected to be the pipe from the process running ps –ef –See example4.c Implementing pipe in shell
Implement a pipe –a.out cmd1 cmd2 cmd1 | cmd2 Something for you to try