目录
为了解决不同进程操作同一个公共资源时,产生冲突的问题,
引入文件锁,当进程操作共享资源时,要求每一个进程都先获取文件锁,然后才能操作共享资源
文件锁分为:
共享锁:多个进程可以同时获取
互斥锁:在同一时刻只能有一个进程获取
当多个进程操作公共资源时,谁先拿到互斥锁谁就可以对资源进行操作,其他进程就必须要等待,
释放了互斥锁后,才可以获取,这样就解决了冲突的问题。
上锁有两种方式:
#include
int flock(int fd, int operation);
//参数1 ------ 要上锁的文件描述符
//参数2 ------ 锁的类型 LOCK_SH LOCK_EX LOCK_UN
//返回值 ---- 成功:0,失败:-1
例如:
- int main(int argc,char **argv)
- {
- int fd;
- char *str = "farishgt";
- int i;
-
- if((fd = open(argv[1],O_RDWR|O_CREAT,0666)) < 0){
- perror("open");
- exit(1);
- }
-
- //给文件上互斥锁
- if(flock(fd,LOCK_EX) < 0){
- perror("flock");
- exit(1);
- }
-
- //操作公共资源
- for(i = 0; i < 20; i++){
- write(fd,str,strlen(str));
- printf("%s\n",str);
- sleep(1);
- }
-
- //解锁
- if(flock(fd,LOCK_UN) < 0){
- perror("flock");
- exit(1);
- }
- close(fd);
- return 0;
- }
- int main(int argc,char **argv)
- {
- int fd;
- char *str = "helloworld";
- int i;
-
- if((fd = open(argv[1],O_RDWR|O_CREAT,0666)) < 0){
- perror("open");
- exit(1);
- }
-
- //给文件上互斥锁
- if(flock(fd,LOCK_EX) < 0){
- perror("flock");
- exit(1);
- }
-
- //操作公共资源
- for(i = 0; i < 20; i++){
- write(fd,str,strlen(str));
- printf("%s\n",str);
- sleep(1);
- }
-
- //解锁
- if(flock(fd,LOCK_UN) < 0){
- perror("flock");
- exit(1);
- }
- close(fd);
- return 0;
- }
#include
include
int fcntl(int fd, int cmd, ... /* arg */ );
//参数1 ----- 文件描述符
//参数2 ----- 功能选项:
功能1:Duplicating a file descriptor(复制文件描述符)
功能2:File descriptor flags (设置或获取文件描述符的标志位)
功能3:File status flags (设置或获取文件状态标志位:O_RDONLY, O_WRONLY, O_RDWR O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC)
功能4: Advisory record locking(设置或获取文件锁)
struct flock {
...
short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock 相对l_whence的偏移,也是锁的起始位置*/
off_t l_len; /* Number of bytes to lock 锁的区域大小*/
pid_t l_pid; /* PID of process blocking our lock (set by F_GETLK and F_OFD_GETLK) 进程ID*/
...
};
功能5: Managing signals(管理进程中收到的信号)
//变参 ------ 根据参数2的不同,而传递不同类型的变参
//返回值 ----- 成功:0,失败:-1
例如:
- int main(int argc,char **argv)
- {
- int fd;
- char *str = "老王正在上厕所";
- int i;
-
- if((fd = open(argv[1],O_RDWR|O_CREAT,0666)) < 0){
- perror("open");
- exit(1);
- }
-
- printf("等待厕所的门被打开\n");
- //给文件的某个区域上锁
- struct flock fl = {F_WRLCK, SEEK_SET,100,1024};
- if(fcntl(fd,F_SETLKW,&fl) < 0){
- perror("flock");
- exit(1);
- }
-
- //操作公共资源
- for(i = 0; i < 20; i++){
- write(fd,str,strlen(str));
- printf("%s\n",str);
- sleep(1);
- }
-
- //解锁
- fl.l_type = F_UNLCK;
- if(fcntl(fd,F_SETLK,&fl) < 0){
- perror("flock");
- exit(1);
- }
- printf("老王上完厕所了!\n");
- close(fd);
- return 0;
- }
- #include <sys/file.h>
- int main(int argc,char **argv)
- {
- int fd;
- char *str = "小明正在上厕所";
- int i;
-
- if((fd = open(argv[1],O_RDWR|O_CREAT,0666)) < 0){
- perror("open");
- exit(1);
- }
-
- printf("等待厕所门打开\n");
- //给文件的某个区域上锁
- struct flock fl = {F_WRLCK, SEEK_SET,100,1024};
- if(fcntl(fd,F_SETLKW,&fl) < 0){
- perror("flock");
- exit(1);
- }
-
- //操作公共资源
- for(i = 0; i < 20; i++){
- printf("%s\n",str);
- sleep(1);
- }
-
- //解锁
- fl.l_type = F_UNLCK;
- if(fcntl(fd,F_SETLK,&fl) < 0){
- perror("flock");
- exit(1);
- }
- printf("小明上完厕所了\n");
- close(fd);
- return 0;
- }