You are on page 1of 14

File and record locking

File locking versus record locking


File locking : locks an entire file
Record locking: allows a process to lock a specified portion of file. Locks the region form starting byte offset in the file and number of bytes from that position. The term record locking is derived from other operating systems that enforce the record structure on disk files. Range blocking: As unix kernel doesn't support the concept of records. It is the range of the file that is locked.

Lockf function is used to lock the region. Lockf function has the following sequence.
#include<unistd.h> int lockf( int fd, int function, long size);

where function has one of the following values


F_ULOCK unlock a previously locked region. F_LOCK lock a region ( blocking ) . F_TLOCK test and lock a region (non blocking ) . F_TEST test a region to see if it is locked

The lockf function uses the current file offset , which the process can set using the lseek system call.

The record starts at the current offset and extends forward for a positive size, or extends backward for a negative size.
If the size is Zero the record extends from the current offset to the end of the file. Blocking: if a lock is set and the region is already locked by another process, the calling process is put to sleep until the region is available. (F_LOCK) Non blocking: if a process is started and it wants to assure that only a single copy of the it is running . When the daemon starts, it executes a non blocking lock on a specified file. If the lock succeeds, then the daemon is the only copy running, but if it fails the daemon can exit, since it knows that a copy is already running. (F_TLOCK)

if( lockf(fd, F_TEST, size)==0) { rc= lockf( fd, F_LOCK, size); .. }

fcntl Record locking #include< sys/types.h> #include<unistd.h> #include<fcntl.h> int fcntl( int filedes, int cmd, .../* struct flock *flockptr */); returns depends on cmd, if ok(F_GETLK, F_SETLK,F_SETLKW) -1 on error. Structure of flock struct flock { short l_type; off_t l_start; short l_whence; off_t l_len; pid_t l_pid; };

The structure defines the type of lock desired: F_RDLCK( a shared lock ) F_WRLCK ( an exclusive lock ) F_UNLCK ( unlocking a region)

/* F_RDLCK, F_WRLCK, or F_UNLCK */ /* offset in bytes, relative to l_whence */ /* SEEK_SET, SEEK_CUR, or SEEK_END */ /* length in bytes; 0 means lock to EOF */ /* returned with F_GETLK */

The starting offset of the region being locked or unlocked ( l_start and l_whence) The size of the region (l_len) F_GETLK: determines if the lock described by flockptr id blocked by some other lock. If a lock exists that would prevent ours from being created the information on existing lock overwrites the information pointed by the flockptr. If no lock exists that would prevent ours from being created., the structure pointed by flock pointer is left unchanged except the l_type member which is set to F_UNLCK. F_SETLK: Set the lock described by flockptr. If we are trying to obtain a read lock l_type of F_RDLCK) or a write lock ( l_type of F_WRLCK) and the compatibility rule prevents the system from giving us the lock fcntl returns immediately with errno set to either EACCES or EAGAIN. F_SETLKW: This command is a blocking version of F_SETLK. (The W in the command name means wait.) If the requested read lock or write lock cannot be granted because another process currently has some part of the requested region locked, the calling process is put to sleep. The process wakes up either when the lock becomes available or when interrupted by a

Region currently had

Request for Read write

No locks

Ok

ok denied denied

One or more read Ok locks One write lock Denied

ADVISORY LOCKING & MANDATORY LOCKING


Advisory locking means the operating system maintains the a correct knowledge of which files have been locked. But it doesn't prevents some process from writing to a file that is locked by another process. A process can ignore the lock and can write to a file that is locked . If the process has adequate permissions. The flock function is used to lock and unlock a file. #include<sys/file.h> int flock(int fd, int operation); Operations LOCK_SH shared lock LOCK_EX exclusive lock LOCK_UN unlock LOCK_NB dontblockwhenlocking Mandatory locking: Operatingsystemcheckseveryreadandwriterequesttoverifythattheoperationdoesnt interfere with a lock held by a process.

STREAMS

The STREAMS mechanism is provided by System V as a general way to interface communication drivers into the kernel. We need to discuss STREAMS to understand the terminal interface in System V, the use of the poll function for I/O multiplexing , and the implementation of STREAMS-based pipes and named pipes Be careful not to confuse this usage of the word stream with our previous usage of it in the standard I/O library History

1. Developed by dennis ritchie 2. STREAMS-based terminal I/O system SVR4

A stream provides a full-duplex path between a user process and a device driver. There is no need for a stream to talk to a hardware device; a stream can also be used with a pseudo-device driver.

Figure : A simple Stream

Figure : A stream with a processing module

Any number of processing modules can be pushed onto a stream. We use the term push, because each new module goes beneath the stream head, pushing any previously pushed modules down. (This is similar to a last- in, first-out stack. We access a stream with the functions from open, close, read, write, and ioctl All STREAMS devices are character special files. The pathname that we open for a stream normally lives beneath the /dev directory Before STREAMS, terminals were handled with the existing c-list mechanism

Malloc, calloc, realloc functions


ISO C specifies three functions for memory allocation: 1. malloc, which allocates a specified number of bytes of memory. The initial value of the memory is indeterminate. 2. calloc, which allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits. 3. realloc , which increases or decreases the size of a previously allocated area. When the size increases, it may involve moving the previously allocated area somewhere else, to provide the additional room at the end. Also, when the size increases, the initial value of the space between the old contents and the end of the new area is indeterminate.

#include <stdlib.h> void *malloc(size_t size); void *calloc(size_t nobj , size_t size); void *realloc(void * ptr, size_t newsize); All three return: non-null pointer if OK, NULL on error void free(void *ptr); The function free causes the space pointed to byptr to be deallocated. This freed space is usually put into a pool of available memory and can be allocated in a later call to one of the three alloc functions.

sbrk can expand or contract the memory of a process, most versions ofmalloc and free never decrease their memory size. The space that we free is available for a later allocation, but the freed space is not usually returned to the kernel; that space is kept in the malloc pool.

MEMORY ALLOCATION OF A C-PROGRAM


High address

stack

Command line arguments and environment variables

heap

uninitialized data Initialized data

Initialized to zero by exec

Read from program file by exec

Low address

text

You might also like