Page 283 - DCAP403_Operating System
P. 283

Operating System




                    Notes                                          Figure 13.8: Pipes

                                          Process 1                                  Process 2
                                             file                                       file
                                         f_mode                                     f_mode
                                         f_pcs                                      f_pcs
                                         f_flags                                    f_flags
                                         f_count                                    f_count
                                         f_ownet                                    f_ownet
                                         f_inode                                    f_inode
                                         f_op                                       f_op
                                                                   inode
                                         f_version                                  f_version




                                                                                     Data Page


                                                     Pipe                                       Pipe
                                                     Write                                      Read
                                                     Operations                                 Operations








                                   pipes the output from the ls command listing the directory’s files into the standard input of the

                                   pr command which paginates them. Finally the standard output from the pr command is piped
                                   into the standard input of the lpr command which prints the results on the default printer. Pipes
                                   then are unidirectional byte streams which connect the standard output from one process into the
                                   standard input of another process. Neither process is aware of this redirection and behaves just
                                   as it would normally. It is the shell which sets up these temporary pipes between the processes.
                                   In Linux, a pipe is implemented using two file data structures which both point at the same

                                   temporary VFS inode which itself points at a physical page within memory. Figure 13.8 shows

                                   that each file data structure contains pointers to different file operation routine vectors; one for

                                   writing to the pipe, the other for reading from the pipe.
                                   This hides the underlying differences from the generic system calls which read and write to

                                   ordinary files. As the writing process writes to the pipe, bytes are copied into the shared data
                                   page and when the reading process reads from the pipe, bytes are copied from the shared data
                                   page. Linux must synchronize access to the pipe. It must make sure that the reader and the writer
                                   of the pipe are in step and to do this it uses locks, wait queues and signals.
                                   When the writer wants to write to the pipe it uses the standard write library functions. These

                                   all pass file descriptors that are indices into the process’s set of file data structures, each one

                                   representing an open  file or, as in this case, an open pipe. The Linux system call uses the

                                   write routine pointed at by the file data structure describing this pipe. That write routine uses

                                   information held in the VFS inode representing the pipe to manage the write request.
                                   If there is enough room to write all of the bytes into the pipe and, so long as the pipe is not
                                   locked by its reader, Linux locks it for the writer and copies the bytes to be written from the
                                   process’s address space into the shared data page. If the pipe is locked by the reader or if there is
                                   not enough room for the data then the current process is made to sleep on the pipe inode’s wait
                                   queue and the scheduler is called so that another process can run. It is interruptible, so it can



          276                              LOVELY PROFESSIONAL UNIVERSITY
   278   279   280   281   282   283   284   285   286   287   288