Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Operation of Linux C file lock

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Type of file lock

1 read lock

Shared lock, if process An adds a read lock to an area of the file, process B can also add a read lock to that area, but not a write lock to that area.

2 write lock

Exclusive lock, if process An adds a write lock to an area of the file, process B cannot add a write lock or read lock to that area.

When multiple processes read and write a file at the same time, in order to ensure the integrity and consistency of the file, these processes should be locked and synchronized.

When the process starts to read an area of the file, add a read lock first, and then unlock it after reading it.

3 file record lock

File record lock

1. Function prototype: int fcntl (int fd,int cmd,struct flck lock)

Parameter: fd: file descriptor

    cmd: function symbol; (F_SETLK is used to set or release locks; F_GETLK is used to obtain lock information;)

    lock: structural pointer to store lock information

    return value: 0 for successful call and-1 for failure

two。 Lock information structure

Struct flock

{

Short lumped type; / type of lock /

Short lumped where; / start position of the offset: /

Off_t lumped start; / offset from l_whence /

The number of bytes starting with off_t l_start /

Pid_t lumped id; / ID of the process to which the lock belongs (generally not used) * /

}

L_type has F_RDLCK read lock, F_WRLCK write lock and F_UNLCK empty lock.

(! Note: read locks do not affect data and can be added all the time, but write locks can only be added once)

L_whence has SEEK_SET, SEEK_CUR and SEEK_END.

A l_len of 0 means starting from the starting point to the maximum possible position.

Here is an example of adding a write lock:

# include

< stdio.h>

# include

< unistd.h>

# include

< fcntl.h>

/ * * int fctnl (int fd,int cmd,struct * lock); / / cmd:F_SETLK setting or Jiefang lock; F_GETLK obtains lock information Return value: 0-successful,-1-failed struct flock {short lumped type; / / of lock:F_RDLCK,F_WRLCK,F_UNLCK short lumped start; / / How to interpreter lumped startgroup SEEK.SEEK.SEK.SEK.SEKEKEURCEND off_t lumped start; / / Starting offset for lock off_t lumped; / / Number of bytes to lock pid_t l_pid / / PID of process blocking our lock (F_GETLK only)}; * / int main () {int fd; int res; struct flock lock = {0}; if ((fd = open ("a.txt", O_RDWR)) = =-1) {fd = open ("a.txt", O_CREAT);} if (fd = =-1) {printf ("file open failed!\ n") Return 1;} / printf ("fd=%d\ n", fd); / / determine whether there is a lock res = fcntl (fd,F_GETLK,&lock); if (res = =-1) {perror ("failed to determine a lock"); return 1;} if (lock.l_type! = F_UNLCK) {printf ("fail:the file has locked!\ n") } else / / write lock, read lock can be added all the time, write lock can only add one {memset (& lock,0,sizeof (struct flock)); lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 10; lock.l_type = FairWRLCK; res = fcntl (fd,F_SETLK,&lock) If (res = =-1) {perror ("lock failed"); return 1;} / / operation file getchar (); / / unlock lock.l_type = Flying UNLCK; res = fcntl (fd,F_SETLK,&lock) If (res = =-1) {perror ("unlock failed"); return 1;}} close (fd); return 0;}

With regard to the locking of multiple processes, the steps are essentially the same, and they will be further reviewed one after another.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report