Project 1: Dues Oct 7 @ 12 Noon You must subscribe to the Project Notes discussion and set notifications for when posts occur ---------------------------------------------------------------- Pipes Pipes are of great use to a Unix/Linux opsys. Used in redirection and piping Using pipes to communicate between parent and child... establishing communication 1. pipe() 2. fork() 3. close unused pipe ends... 4. Communicate Pipe ends are closed so that when the sender is done sending and they close the write end, the reader will receive a 0 as the return value and know the write is done. - If the reader doesn't close the write end, the eof they are expecting will not arrive. Unnamed pipe is commonly used in shell commands, e.g. ls | grep sem lists only entries containing 'sem' PIPE_BUF is the maximum number of bytes that can be written to a pipe atomically. - BUFSIZ should also have this value. Notes - pipes are retained by both parent and child on a fork because the child gets a copy of the parent's file table. - A pipe must exist before a child process is created. Otherwise, the doesn't have access to the pipe. - A named pipe can be shared among all users on a machine, as long as its permissions allow it. - The owner can change permissions. dup & dup2 - returns copies of file descriptors * e.g. dup(myFd) will return a copy of myFd + the value returned will be the next available desciptor in the process' file table * if a program starts and immediately issues int newFD=dup(1), newFD will be a copy of stdout's file descriptor. + It will return 3 because that is the next available fd. e.g. myFD=open("abc.txt",O_WRONLY); close(1); dup(myFD); This makes it so the file can be written to as if I was writing to stdout, i.e. I can use printf to write to the file. We want to do this with pipes so we can write to a pipe via stdout and read via stdin. dup2 is dup that also closes a file descriptor, i.e. e.g. myFD=open("abc.txt",O_WRONLY); dup2(myFD,1); - closes 1 and creates a duplication of myFD that will have file descriptor 1 and thus be writable as if it is stdout. ----------------------------------------------------------------- File Tables & inodes|vnodes File Accessing - Process file table belongs to a process and is shared with its descendents. - System file table is used by all processes to access inodes (vnodes) that provide the file's data. When a process is forked, its child gets a duplicate process file table. This means that if a file is open, a parent can do something with it, and the child will see it. The system file table is shared among all active processes. The number of available entries, like the number of available inodes, is finite. Set 4, Slide 9: What happens to a process’ file table on a fork? - Duplicated What happens if a process with a pipe containing data calls fork()? - Buffer is duplicated What do file tables look like when complex shell commands are executed? - TBD How does dup2 affect the file table? - Opens a duplicate, closes the source When are entries in the system file table shared? - On a fork, parent and child are accessing same entries. What happens to fully buffered data if a fork occurs between buffering and output? - Duplicated When is output fully buffered? - To a file When are system file table entries shared, and when are inodes shared? - For you to ponder