In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
1. Pay attention to the correct use of the fcntl () parameter cmd
F_GETFL is used to test lock usage
F_SETFL non-blocking setting lock fcntl () will try several times and return-1 if it fails
F_SETLKW blocking setting lock fcntl () will try, and if it fails, it will be suspended by the system until it receives a signal to unlock it.
two。 When testing the lock, the l_stype in the member of the struct flock lock structure needs to be set to F_WRLCK.
3.struct flock lock members use the
Test or lock how the entire file is set
L_whence = SEEK_SET
L_start = 0
L_len = 0; if l_len is 0, it starts from start to the last EOF of the file
Struct flock {
...
Short lumped type; / * Type of lock: F_RDLCK
F_WRLCK, F_UNLCK * /
Short lame whence; / * How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END * /
Off_t lumped start; / * Starting offset for lock * /
Off_t Lilien; / * Number of bytes to lock * /
Pid_t lumped pad; / * PID of process blocking our lock
(F_GETLK only) * /
...
}
5. The program tests its own lock, and it must be unlocked after the test.
6. If the two intervals set by the program are continuous and the nature of the lock is the same, the system will automatically merge the two locks into one lock.
7. For more information, please refer to the following awesome people.
Http://zhuyunxiang.blog.51cto.com/653596/132548
Execution result:
Execute on another terminal
Main.c
# include "setlock.h" # include#include # include#include int main (void) {int fd = open (". / src", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IXOTH); check_lock (fd,0,10); check_lock (fd,11,20); set_read_lock (fd,0,10); set_write_lock (fd,11,20); sleep (15); unlock (fd,0,10); unlock (fd,11,20); return 0;}
Setlock.h
# ifndef SETLOCK_H#define SETLOCK_Hvoid check_lock (int fd,int start,int len); void unlock (int fd,int start,int len); void set_read_lock (int fd,int start,int len); void set_write_lock (int fd,int start,int len); # endif / / end SETLOCK_H
Setlock.c
# include#include#include # include#include# includevoid check_lock (int fd,int start,int len) {struct flock lock;lock.l_type = FairWRLCKTock. Lager start = start;lock.l_whence = SEEK_SET;lock.l_len = start;printf ("check_lock-\ n"); if ((fcntl (fd, F_GETLK, & lock)) = =-1) {printf ("1-check_lock: fcntl error\ n"); exit (1) } switch (lock.l_type) {case F_RDLCK: {printf ("[% d]: FRDLCK From% d To% d\ n", lock.l_pid, start, len); break;} case F_WRLCK: {printf ("[% d]: F_WRLCK From% d To% d\ n", lock.l_pid, start, len); break;} case F_UNLCK: {printf ("F_UNLCK\ n"); break;} default: {printf ("2-check_lock: fcntl error"); break } void set_read_lock (int fd,int start,int len) {printf ("set_read_lock-\ n"); struct flock lock;lock.l_type = FairRDLCKlock.llockstart = 0lock.llockwhence = SEEK_SET;lock.l_len = 0lock if ((fcntl (fd, F_SETLKW, & lock)) = =-1) {printf ("set_lock: fcntl error\ n"); exit (1) } else {printf ("[% d] set readlock From% d To% d\ n", getpid (), start, len);}} void set_write_lock (int fd,int start,int len) {printf ("set_write_lock-\ n"); struct flock lock;lock.l_type = FairWRLCKlock.lumped start = start;lock.l_whence = SEEK_SET;lock.l_len = len If ((fcntl (fd, F_SETLKW, & lock)) =-1) {printf ("set_lock: fcntl error\ n"); exit (1);} else {printf ("[% d] set writelock From% d To% d\ n", getpid (), start, len);}} bool unlock (int fd,int start,int len) {printf ("unlock-\ n"); struct flock lock;lock.l_type = Flying UNLCKlock.lame start = start Lock.l_whence = SEEK_SET;lock.l_len = len;if ((fcntl (fd, F_SETLK, & lock)) =-1) {printf ("1-unlock: fcntl error\ n"); exit (1);} printf ("unlock [% dumped% d]\ n", start,len);}
Makefile
Srcs=$ (wildcard * .c) objs = $(patsubst% cMagna% OJE $(srcs)) CC = gccTarget = main#.PHONY: all clean # command: make all or make cleanclean:rm-f $(obj) main * ~ * gch#all: $(Target) $(Target): $(objs) $(CC)-o $@ $^ main.o:main.c$ (CC)-c $
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.