Transcript Lab 15

Lab 15
Record Locking
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.
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.
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.
A rude process with write
permission of the file
database
NCHU System & Network Lab
Advisory
lock
access
library
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
fget()
write()
file
kernel
readn()
fput()
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 :
cmd
description
F_DUPFD
Duplicate an existing descriptor
F_GETFD,F_SETFD
Get/set file descriptor flags
F_GETFL,F_SETFL
Get/set file status flags (O_RDWR … etc)
F_GETOWN,F_SETOWN
Get/set asynchronous I/O ownership
F_GETLK,F_SETLK,
F_SETLKW
Get/set/lock file lock, it discussed later.
#include <fcntl.h>
int fcntl (int filedes, int cmd, struct flock *lockptr…);
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;
off_t l_start;
short l_whence;
off_t l_len;
pid_t l_pid;
}
/* F_RDLCK, F_WRLCK, F_UNLCK */
/* offset in bytes, relative to l_whence */
/* SEEK_SET, SEEK_CUR, SEEK_END */
/* length, in bytes ; 0 means lock to EOF */
/* returned with F_GETLK */
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.
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
Lock request
Lock request
Process B
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() .
NCHU System & Network Lab
Ex : FreeBSD file lock structure
Figure : FreeBSD data structures for record locking
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.
NCHU System & Network Lab
Lab 1 (cont.)
Input : R/W, start, length
Show the lock
and wait..
Is it locked?
No
Lock the region
Wait for user
unlock
NCHU System & Network Lab
yes
1
4
3
2
5
NCHU System & Network Lab
#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
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
Reference
•Advanced Programming in the UNIX Environment 2nd
Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley
•Beginning Linux Programming
Author : Richard Stones, Neil Matthew
Publisher : Wrox
•http://linux.vbird.org/
•http://www.jollen.org/blog/ jollen’s Blog
NCHU System & Network Lab