Page 447 - DCAP103_Principle of operating system
P. 447

Principles of Operating Systems



                   Notes         When placing a lock, a process must specify whether it wants to block or not in the event that
                                 the lock cannot be placed. If it chooses to block, when the existing lock has been removed, the
                                 process is unblocked and the lock is placed. If the process chooses not to block when it cannot
                                 place a lock, the system call returns immediately, with the status code telling whether the lock
                                 succeeded or not.
                                 Locked regions may overlap. In Figure 14.26, we see that process A has placed a shared lock on
                                 bytes 4 through 7 of same file. Later, process B places a shared lock on bytes 6 through 9. Finally,
                                 C locks bytes 2 through 11. As long as all these locks are shared, they can co-exist.


                                           Figure 14.26: (a) A File with One Lock, (b) Addition of a Second Lock
                                                               and (c) A Third Lock


























                                 Now consider what happens if a process tries to acquire an exclusive lock to byte 9 of the file
                                 of Figure 14.26(c), with a request to block if the lock fails. Since two previous locks cover this
                                 block, the caller will block and will remain blocked until both B and C release their locks.
                                 14.6.2 File System Calls in Linux

                                 Many system calls relate to files and the file system. First we will look at the system calls that
                                 operate on individual files. Later we will examine those that involve directories or the file system
                                 as a whole. To create a new file, the create call can be used. The parameters provide the name
                                 of the file and the protection mode. Thus
                                 fd = creat(“abc”, mode);
                                 creates a file called abc with the protection bits taken from mode. These bits determine which
                                 users may access the file and how. That will be described later. The create call not only creates
                                 a new file, but also opens it for writing. To allow subsequent system calls to access the file, a
                                 successful create returns as its result a small non-negative integer called a file descriptor, fd in
                                 the example above. If a create is done on an existing file, that file is truncated to length 0 and its
                                 contents are discarded. Files can also be created using the open call with appropriate arguments.
                                 Now let us continue looking at the principal file system calls, which are listed in Figure 14.27.
                                 To read or write an existing file, the file must first be opened using open. This call specifies
                                 the file name to be opened and how it is to be opened: for reading, writing, or both. Various
                                 options can be specified as well. Like create, the call to open returns a file descriptor that can
                                 be used for reading or writing.

                                 Afterward, the file can be closed by close, which makes the file descriptor available for reuse on
                                 a subsequent create or open. Both the create and open calls always return the lowest numbered
                                 file descriptor not currently in use. When a program starts executing in the standard way, file



        440                               LOVELY PROFESSIONAL UNIVERSITY
   442   443   444   445   446   447   448   449   450   451   452