Presentation is loading. Please wait.

Presentation is loading. Please wait.

NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.

Similar presentations


Presentation on theme: "NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same."— Presentation transcript:

1 NCHU System & Network Lab Lab 15 Record Locking

2 NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same file at the same time ? Record locking is the ability of a process to prevent other processes from modifying or reading a region of a file while the first process is working on it.

3 NCHU System & Network Lab Record Locking (2/4) Here are two types of locks : –Read lock (shared lock) Any number of processes can have a shared read lock on a given region of a file. All of them can read from the file but can’t write to it. –Write lock (exclusive lock) Only one process can have an exclusive lock on a given region of a file. The other process are prevented from getting locks or reading and writing.

4 NCHU System & Network Lab Record locking (3/4) Advisory V.S. Mandatory locking –Advisory lock (cooperative lock) All access functions in a library handle record locking in a consistent way. But advisory locking doesn’t prevent some other process that has write permission for file from writing. database Advisory lock access library A rude process with write permission of the file

5 NCHU System & Network Lab Record locking (4/4) –Mandatory locking cause the kernel to check every read(), write(), open() to prevent the calling process from violating the lock rule. file kernel fget() write() readn() fput() Locking check

6 NCHU System & Network Lab fcntl() (1/3) fcntl() function can change the properties of a file that is already open. –The fcntl() is used for five different purposes : cmddescription F_DUPFD Duplicate an existing descriptor F_GETFD,F_SETFDGet/set file descriptor flags F_GETFL,F_SETFLGet/set file status flags (O_RDWR … etc) F_GETOWN,F_SETOWNGet/set asynchronous I/O ownership F_GETLK,F_SETLK, F_SETLKW Get/set/lock file lock, it discussed later. #include int fcntl (int filedes, int cmd, struct flock *lockptr…);

7 NCHU System & Network Lab fcntl() (2/3) For record locking,the third argument of fcntl() is a pointer to a flock structure : struct flock { short l_type;/* F_RDLCK, F_WRLCK, F_UNLCK */ off_t l_start;/* offset in bytes, relative to l_whence */ short l_whence;/* SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_len;/* length, in bytes ; 0 means lock to EOF */ pid_t l_pid;/* returned with F_GETLK */ }

8 NCHU System & Network Lab fcntl() (3/3) Three commands of fcntl( record lock ) : –F_GETLK To check the lock described by flockptr : –The information pointed by flockptr is overwritten by an existing lock. –If no lock exists, flockptr.l_type = F_UNLCK –F_SETLK Set the lock described by flockptr. If we try to obtain an illegal record lock, fcntl() will return errono = EAGAIN. –F_SETLKW A blocking version of F_SETLK. It will wait until the desired region released.

9 NCHU System & Network Lab Deadlock Deadlock –Deadlock occurs when two processes are each waiting for a resource that the other has locked. –When a deadlock is detected, the kernel must choose one to receive the error return. It depends on different implementations of systems. Process A Process B Lock request

10 NCHU System & Network Lab Inheritance and Release of Locks Inheritance and release of locks : –Release of locks : When a process terminates, all its locks are released. When a descriptor is closed, any locks on it are automatically released. –Inheritance Locks are never inherited by the child process across a fork(). Locks are inherited by a new program across an exec().

11 NCHU System & Network Lab Ex : FreeBSD file lock structure Figure : FreeBSD data structures for record locking

12 NCHU System & Network Lab Lab 1 Write a program that : –User can define a lock with type, start, and length. –This program will try to get a lock or it will be blocked by an existing lock. If it is blocked, it should show the lock information. –By running this program in two or more windows, you can see how programs interact while waiting locks.

13 NCHU System & Network Lab Lab 1 (cont.) Input : R/W, start, length Is it locked? Show the lock and wait.. yes Lock the region unlock Wait for user No

14 NCHU System & Network Lab 1 2 3 4 5

15 #include #include #include #include #include int main() { struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 }; int fd; fd = open(“test", O_RDWR) fcntl (fd, F_SETLKW, &fl) ; // set the lock ‘fl’ getchar(); printf(“file is locked…”); … exit(1); }

16 NCHU System & Network Lab Lab 2 Use the program you have done before : –You try to make a deadlock situation to see that how does Linux solve this problem.

17 NCHU System & Network Lab Reference Advanced Programming in the UNIX Environment 2nd Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley Beginning Linux Programming Author : Richard Stones, Neil MatthewPublisher : Wrox http://linux.vbird.org/ http://www.jollen.org/blog/ jollen’s Bloghttp://www.jollen.org/blog/


Download ppt "NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same."

Similar presentations


Ads by Google