[13] Apue: (file) record lock

Source: Internet
Author: User
Tags flock

[A] Concept

    • Recommended lock: Effective across processes that follow the same record lock rule, often used to guarantee data consistency across multiple processes in a program itself
    • Force Lock: Intended to guarantee data consistency across all processes, but not necessarily effective, such as the act of establishing a copy of the same name after the first unlink

[b] Fcntl

#include <fcntl.h>int fcntl (intintstruct// Error return-1
 struct   flock { short  l_type; //     lock type: F_rdlck/f_wrlck/f_unlck  short  l_whence; //  offset datum: seek_set/seek_cur/seek_end  off_t L_start; //  offset (byte)  off_t L_len relative to l_whence; //  pid_t l_pid; // } that currently owns the lock   
    • When manipulating record locks, the third parameter of FCNTL is a pointer to the flock struct
    • CMD can be f_getlk/f_setlk/f_setlkw, respectively, to get the file lock state, set the lock (non-blocking), set the lock (blocking)
    • The l_len field in the struct flock is set to 0, indicating that the extents are dynamically extended to the end of the file, and any data appended to the end of the file, such as locking, will be locked if set to-1, representing all data before the L_start The L_type field is set to F_unlck to clear the specified lock
    • The same process repeatedly locks the same section of the same file, the new lock replaces the old lock, and the old lock is immediately invalidated.
    • when F_getlck is executed, the current lock state information is written to the flock structure

[C] Deadlock

    • A deadlock occurs when two processes wait for each other's locks and do not release their locked resources.
    • The modern operating system automatically unlocks the deadlock, usually returning an error in one of the processes, while the other succeeds in obtaining the lock, but it is not certain which process is acquiring the lock

[d] Implicit inheritance and release of locks

    • When the process terminates, all its established locks are released
    • When multiple file descriptors are associated to the same file, any one of the file descriptors is closed, causing the lock to be released
    • The child process after the fork does not inherit the lock of the parent process, and the process following the exec inherits the lock of the original process (if the CLOSE-ON-EXEC flag is set, it is not inherited)

[E] Lock at the end of the file

    • Because the position at the end of the file changes with the append data, it is important to specify the correct range when unlocking

[F] combination and splitting of the lock

    • If you lock all the parts in the middle of the two locked sections, the three parts are combined into one lock
    • If a part of the middle of the locked section is unlocked, the original lock is automatically split into two locks

[13] Apue: (file) record lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.