Page 449 - DCAP103_Principle of operating system
P. 449

Principles of Operating Systems



                   Notes         sort would be set (by the shell) to write to the pipe and file descriptor 0 (standard input) in the process
                                 running head would be set to read from the pipe. In this way, sort just reads from file descriptor
                                 0 (set to the file in) and writes to file descriptor 1 (the pipe) without even being aware that these
                                 have been redirected. If they have not been redirected, sort will automatically read from the
                                 keyboard and write to the screen.

                                               Figure 14.28: The Fields Returned by the Stat System Call

                                           Device the file is on

                                           I-node number (which file on the device)
                                           File mode (includes protection information)
                                           Number of links to the file
                                           Identify of the file’s owner

                                           Group the file belongs to
                                           File size (in bytes)
                                           Creation time
                                           Time of last access

                                           Time of last modification

                                 Similarly, when head reads from file descriptor 0, it is reading the data sort put into the pipe
                                 buffer without even knowing that a pipe is in use. This is a clear example where a simple concept
                                 (redirection) with a simple implementation (file descriptors 0 and 1) leads to a powerful tool
                                 (connecting program in arbitrary ways without having to modify them at all). The last system
                                 call in Figure 14.28 is fcntl. It is used to lock and unlock files, apply shared or exclusive locks,
                                 and  perform  a  few  other  file-specific  operations.  Now  let  us  look  at  some  system  calls  that
                                 relate more to directories or the file system as a whole, rather than just to one specific file. Some
                                 common ones are listed in Figure 14.29. Directories are created and destroyed using mkdir and
                                 rmdir, respectively. A directory can only be removed if it is empty.


                                                Figure 14.29: Some System Calls Relating to Directories


                                          System call                    Description
                                          s = mkdir(path, mode)          Create a new directory

                                          s = rmdir(path)                Remove a directory
                                          s = link(oldpath, newpath)     Create a link to an existing file
                                          s = unlink(path)               Unlink a file

                                          s = chdir(path)                Change the working directory
                                          dir = opendir(path)            Open a directory for reading

                                          s = closedir(dir)              Close a directory
                                          dirent = readdir(dir)          Read one directory entry
                                                                         Rewind a directory so it can be
                                          rewinddir(dir)
                                                                         reread




        442                               LOVELY PROFESSIONAL UNIVERSITY
   444   445   446   447   448   449   450   451   452   453   454