+ All Categories

Lab 15

Date post: 18-Jan-2016
Category:
Upload: morna
View: 33 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
17
NCHU System & Network Lab NCHU System & Network Lab Lab 15 Lab 15 Record Locking
Transcript
Page 1: Lab 15

NCHU System & Network LabNCHU System & Network Lab

Lab 15Lab 15

Record Locking

Page 2: Lab 15

Record Locking (1/4)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.

Page 3: Lab 15

Record Locking (2/4)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.

Page 4: Lab 15

Record locking (3/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.

database

Advisorylock

accesslibrary

A rude process with write permission of the file

Page 5: Lab 15

Record locking (4/4)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

Page 6: Lab 15

fcntl()fcntl() (1/3) (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…);

Page 7: Lab 15

fcntl()fcntl() (2/3) (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 */

}

Page 8: Lab 15

fcntl()fcntl() (3/3) (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 regio

n released.

Page 9: Lab 15

DeadlockDeadlock

• 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

Lock request

Page 10: Lab 15

Inheritance and Release of LocksInheritance 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() .

Page 11: Lab 15

Ex : FreeBSD file lock structureEx : FreeBSD file lock structure

Figure : FreeBSD data structures for record locking

Page 12: Lab 15

Lab 1Lab 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.

Page 13: Lab 15

Lab 1 (cont.)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

Page 14: Lab 15

1

23

4

5

Page 15: 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);

}

Page 16: Lab 15

Lab 2Lab 2

• Use the program you have done before :– You try to make a deadlock situation to see that

how does Linux solve this problem.

Page 17: Lab 15

ReferenceReference•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


Recommended