1 / 17

Lab 15

Lab 15. Record Locking. 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.

morna
Download Presentation

Lab 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 15 Record Locking

  2. 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. NCHU System & Network Lab

  3. 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. NCHU System & Network Lab

  4. 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. Advisorylockaccesslibrary database A rude process with write permission of the file NCHU System & Network Lab

  5. 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. Locking check kernel fget() write() file readn() fput() NCHU System & Network Lab

  6. fcntl() (1/3) • fcntl() function can change the properties of a file that is already open. • The fcntl() is used for five different purposes : #include <fcntl.h> int fcntl (int filedes, int cmd, struct flock *lockptr…); NCHU System & Network Lab

  7. 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 */} NCHU System & Network Lab

  8. 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. NCHU System & Network Lab

  9. 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 Lock request Lock request Process B NCHU System & Network Lab

  10. 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() . NCHU System & Network Lab

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

  12. 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. NCHU System & Network Lab

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

  14. 1 4 2 3 5 NCHU System & Network Lab

  15. #include <stdio.h>#include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> 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);} NCHU System & Network Lab

  16. 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. NCHU System & Network Lab

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

More Related