assign4[1]

download assign4[1]

of 2

Transcript of assign4[1]

  • 7/29/2019 assign4[1]

    1/2

    Assignment 4

    Problem for Assign4:

    Write a C program which will be able to read a file using read( ) system call.Now you provide a mechanism to set a lock by using fcntl( ) system call and torelease the lock by using fcntl( ) system call. For setting and releasing a lock, youhave to fill out a structure flock (struct flock) with proper values.

    Now you run the program from two different terminals simultaneously anddemonstrate that if one process has got the lock, then that process can read the file.But other one process can not read the file until the lock released by the previous

    one process. That means, only one process who have got the lock, can read the file atany instant time.

    Related Information:

    * Read read(), write() and fcntl() system call from W. R. Steven's "Unix Network

    programming" book .

    * Also open the man page for fcntl to learn quickly about its usage pattern.

    Hints:* The structure for the flock consists of the following members

    l _ typeThis is where you signify the type of lock you want to set. It's eitherF_ RDLCK, F_WRLCK, or F_UNLCKif you want to set a read lock, write lock,or clear the lock, respectively.

    l _whenceThis field determines where the l _st ar t field starts from (it's like anoffset for the offset). It can be either SEEK_SET, SEEK_CUR, or SEEK_END,for beginning of file, current file position, or end of file.

    l _ star t This is the starting offset in bytes of the lock, relative to l _whence.

    l _l enThis is the length of the lock region in bytes (which starts froml _st ar t which is relative to l _whence.

    l _pi dThe process ID of the process dealing with the lock. Usegetpi d( ) to getthis.

    * Know about the system call fcntl( ). The second arguments of fcntl( ) systemcall may be of the following types.

    F_SETLKWThis argument tells f cnt l ( ) to attempt to obtain the lockrequested in thest r uct f l ock structure. If the lock cannot beobtained (since someone else has it locked already), f cnt l ( ) will

    wait (block) until the lock has cleared, then will set it itself. This isa very useful command. I use it all the time.

  • 7/29/2019 assign4[1]

    2/2

    F_SETLKThis function is almost identical toF_SETLKW. The only differenceis that this one will not wait if it cannot obtain a lock. It will returnimmediately with - 1. This function can be used to clear a lock bysetting the l _t ype field in thest r uct f l ock toF_UNLCK.

    F_GETLKIf you want to only check to see if there is a lock, but don't want toset one, you can use this command. It looks through all the filelocks until it finds one that conflicts with the lock you specified inthest r uct f l ock. It then copies the conflicting lock's informationinto thestruct and returns it to you. If it can't find a conflictinglock, f cnt l ( ) returns thestruct as you passed it, except it setsthe l _t ype field toF_ UNLCK.