unix7_2

download unix7_2

of 14

Transcript of unix7_2

  • 8/13/2019 unix7_2

    1/14

    File and record locking

  • 8/13/2019 unix7_2

    2/14

    File locking versus record lockingFile 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 odisk files.

    Range blocking: As unix kernel doesn't support the concept of records. It is the range of the file th

    is locked.

    Lockf function is used to lock the region. Lockf function has the following sequence.

    #include

    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

  • 8/13/2019 unix7_2

    3/14

    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 therunning . 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 exi

    since it knows that a copy is already running. (F_TLOCK)

    if( lockf(fd, F_TEST, size)==0)

    {rc= lockf( fd, F_LOCK, size);

    ..

    }

  • 8/13/2019 unix7_2

    4/14

    fcntl Record locking

    #include< sys/types.h>

    #include

    #include

    int fcntl( int filedes, int cmd, .../* struct flock *flockptr */);

    returnsdepends on cmd, if ok(F_GETLK, F_SETLK,F_SETLKW)

    -1 on error.

    Structure of flock

    struct flock

    {short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */

    off_t l_start; /* offset in bytes, relative to l_whence */

    short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */

    off_t l_len; /* length in bytes; 0 means lock to EOF */

    pid_t l_pid; /* returned with F_GETLK */

    };

    The structure defines

    the type of lock desired:

    F_RDLCK( a shared lock )

    F_WRLCK ( an exclusive lock

    F_UNLCK ( unlocking a regio

  • 8/13/2019 unix7_2

    5/14

    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 o

    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 o

    EAGAIN.

    F_SETLKW:This command is a blocking version of F_SETLK. (The W in the command na

    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

    sleep. The process wakes up either when the lock becomes available or when interrupted

    si nal.

  • 8/13/2019 unix7_2

    6/14

    No locks Ok ok

    One or more readlocks

    Ok denied

    One write lock Denied denied

    Region currently

    had

    Request for

    Read write

  • 8/13/2019 unix7_2

    7/14

    ADVISORY LOCKING & MANDATORY LOCKING

    Advisory locking means the operating system maintains the a correct knowledge of which

    have been locked. But it doesn't prevents some process from writing to a file that is lockeanother 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.

    #includeint flock(int fd, int operation);

    Operations

    LOCK_SH shared lock

    LOCK_EX exclusive lock

    LOCK_UN unlock

    LOCK_NB dont block when locking

    Mandatory locking:

    Operating system checks every read and write request to verify that the operation doesnt

    interfere with a lock held by a process.

  • 8/13/2019 unix7_2

    8/14

    STREAMS The STREAMS mechanism is provided by System V as a general way to

    interfacecommunication drivers into the kernel.

    We need to discuss STREAMS to understand the terminal interface in SysteV, the use of the poll function for I/O multiplexing , and the implementation

    STREAMS-based pipes and named pipes

    Be careful not to confuse this usage of the word stream with our previous usaof it in the standard I/O library

    History

    1. Developed by dennis ritchie

    2. STREAMS-based terminal I/O system SVR4

  • 8/13/2019 unix7_2

    9/14

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

    Figure : A simple Stream

  • 8/13/2019 unix7_2

    10/14

    Figure : A stream with a processing module

  • 8/13/2019 unix7_2

    11/14

    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 issimilar 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

  • 8/13/2019 unix7_2

    12/14

    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.

  • 8/13/2019 unix7_2

    13/14

    #include

    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 th

    three alloc functions.

    sbrk can expand or contract the memory of a process, most versions ofmalloc and f

    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.

  • 8/13/2019 unix7_2

    14/14

    MEMORY ALLOCATION OF A C-PROGRAM

    text

    Initialized data

    uninitialized

    data

    stack

    heap

    High address

    Low address

    Command line arguments andenvironment variables

    Initialized to zero by exec

    Read from program file by exec