Fcntl (file lock)

Source: Internet
Author: User
Tags flock flock lock

Fcntl (file lock)
Header file # include <unistd. h>
# Include <fcntl. h>
Function Definition int fcntl (int fd, int cmd, struct flock * Lock );
Function Description FD: file descriptor
The configured file descriptor. The cmd parameter indicates the command to operate.
F_dupfd
Copy the file descriptor of the FD parameter. If the execution succeeds, the newly copied file descriptor is returned,
F_getfd
Obtain the close-on-exec flag. If the fd_cloexec bit of some flag is 0
Files will not be closed when exec () related functions
F_setfd: Set the close-on-exec flag, which is determined by the fd_cloexec bit of the ARG parameter.
F_getfl get the flag set for open ()
F_setfl: flag for changing open () settings
F_getlk: gets the file lock status and determines whether to apply the file Lock Based on the lock description.
F_setlk sets the file lock status. In this case, the l_tpye value of the structure must be f_rdlck, f_wrlck, or f_unlck,
-1 is returned if a lock cannot be created.
F_setlkw is a blocking version of f_setlk. When the lock cannot be obtained, it enters the sleep state. If the lock can be obtained or the signal is captured, the system returns
The parameter lock pointer is defined as the Flock Structure pointer as follows:
Struct flock {
...
Short l_type;/* type of lock: f_rdlck, locked
F_wrlck, f_unlck */
Short l_whence;/* how to interpret l_start: determines the Rochelle statr location.
Seek_set, seek_cur, seek_end */
Off_t l_start;/* Starting offset for Lock */switch position in the locked area
Off_t l_len;/* number of bytes to lock */size of the locked area
Pid_t l_pid;/* PID of process blocking our lock
(F_getlk only) */lock Action Process
...
};
There are three statuses of consumer type:
F_rdlck read lock (shared lock)
F_wrlck write lock (rejection lock)
F_unlck unlock
Rochelle whence also has three methods
Seek_set starts with a file and is the starting position of the lock.
Seek_cur uses the current file read/write location as the starting position of the lock
Seek_end start position with the end of the file as the lock
If the return value is successful, 0 is returned. If an error exists,-1 is returned.
L_len: length of the lock area
Rochelle PID: a lock that blocks the current process. The process number held by the lock is stored in Rochelle PID, Which is returned by f_getlk.
Set l_start to 0, l_whence to seek_set, and l_len to 0.
Bytes ---------------------------------------------------------------------------------------------
Lock_set.c
Bytes ----------------------------------------------------------------------------------------------
# Include <stdio. h>
# Include <unistd. h>
# Include <fcntl. h>
Int lock_set (int fd, int type)
{
Struct flock lock;
Lock. l_whence = seek_set;
Lock. l_start = 0;
Lock. l_len = 0;
Lock. l_type = type;
Lock. l_pid =-1;
Fcntl (FD, f_getlk, & lock); // get the FD and lock status
If (lock. l_type! = F_unlck)
{
If (lock. l_type = f_rdlck)
{
Printf ("read lock already set by % d/N", lock. l_pid );
}
Else if (lock. l_type = f_wrlck)
{
Printf ("Write lock already set by % d/N", lock. l_pid );
}
}
Lock. l_type = type; // f_getlk maybe chmod the lock. l_type type
If (fcntl (FD, f_setlkw, & lock) <0) // set lock
{
Printf ("lock failed: TYPE = % d/N", lock. l_type );
Return 1;
}
Switch (lock. l_type)
{
Case f_rdlck:
{
Printf ("read lock set by % d/N", getpid ());

}
Break;
Case f_wrlck:
{
Printf ("Write lock set by % d/N", getpid ());
}
Break;
Case f_unlck:
{
Printf ("Release lock by % d/N", getpid ());
}
Break;
Default:
Break;
}
Return 0;
}
Bytes --------------------------------------------------------------------------------------------
Test_write_read_lock.c
Bytes -------------------------------------------------------------------------------------------
# Include <unistd. h>
# Include <sys/file. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include "lock_set.c"
Char Buf [] = "Shui Xian bin you are best! ";
Char s [100] = "";
Int main (void)
{
Int FD;
FD = open ("t.txt", o_rdwr | o_creat | o_append, 0777 );
If (FD <0)
{
Perror ("open ");
Exit (1 );
}
Lock_set (FD, f_wrlck );
Write (FD, Buf, sizeof (BUF ));
Lock_set (FD, f_unlck );
Close (FD );
FD = open ("t.txt", o_rdwr, 0777 );
If (FD <0)
{
Perror ("open ");
Exit (1 );
}
Lock_set (FD, f_rdlck );
Read (FD, S, sizeof (s ));
Lock_set (FD, f_unlck );
Printf ("read... % s/n", S );
Close (FD );
Exit (0 );
}
Bytes ----------------------------------------------------------------------------------------
[Root @ localhost test] #./s
Writes lock set by 6224
Release lock by 6224
Read lock set by 6224
Release lock by 6224
Read... Shui Xian bin you are best!
Bytes -----------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

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.