Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121798
  • 博文数量: 30
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-14 10:05
个人简介

坚持学习,每天进步一点点……

文章分类

全部博文(30)

文章存档

2017年(13)

2016年(17)

分类: C/C++

2017-05-18 11:08:24

文章出处:http://blog.csdn.net/lin_fs/article/details/7804494
    最近在工作中遇到一个多进程操作同一文件的问题,一个进程会一直向文件中写入,另一个进程会一直读。为了防止读写冲突,所以需要对文件的操作加以保护。
以下是转载部分:

表头文件  #include

  定义函数  int flock(int fd,int operation);

  函数说明  flock()会依参数operation所指定的方式对参数fd所指的文件做各种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文件的某一区域。

  参数  operation有下列四种情况:

  LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁定。

  LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。

  LOCK_UN 解除文件锁定状态。

  LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通常与LOCK_SH或LOCK_EX 做OR(|)组合。

  单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork()时文件描述词不会继承此种锁定。

  返回值  返回0表示成功,若有错误则返回-1,错误代码存于errno。

flock只要在打开文件后,需要对文件读写之前flock一下就可以了,用完之后再flock一下,前面加锁,后面解锁。其实确实是这么简单,但是前段时间用的时候发现点问题,问题描述如下:

一个进程去打开文件,输入一个整数,然后上一把写锁(LOCK_EX),再输入一个整数将解锁(LOCK_UN),另一个进程打开同样一个文件,直接向文件中写数据,发现锁不起作用,能正常写入(我此时用的是超级用户)。google了一大圈发现flock不提供锁检查,也就是说在用flock之前需要用户自己去检查一下是否已经上了锁,说明白点就是读写文件之前用一下flock检查一下文件有没有上锁,如果上锁了flock将会阻塞在那里(An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor ),除非用了LOCK_NB。
原文中的文件编译有错误,我在它基础上修改的code:
//lockfile.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>

  7. int main()
  8. {
  9.     int fd,i;
  10.     char path[]="test.txt";
  11.     fd=open(path,O_RDWR|O_CREAT);
  12.     if(fd!=-1)
  13.     {
  14.         if(flock(fd,LOCK_EX)==0)
  15.         {
  16.             printf("the file was locked.\n");
  17.         }
  18.         else
  19.         {
  20.             printf("the file was not locked.\n");
  21.         }
  22.         sleep(10);
  23.         if(flock(fd,LOCK_UN)==0)
  24.         {
  25.             printf("the file was unlocked.\n");
  26.         }
  27.         else
  28.         {
  29.             printf("the file was not unlocked.\n");
  30.         }
  31.         close(fd);
  32.     }
  33.     else
  34.     {
  35.         printf("cannot open file %s\n",path);
  36.     }
  37. }


//testprocess.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <sys/file.h>

  8. int main()
  9. {
  10.     int fd,i;
  11.     char path[]="test.txt";
  12.     char s[]="writing.../nwriting......\n";
  13.     fd=open(path,O_RDWR|O_CREAT|O_TRUNC);
  14.     if(fd!=-1)
  15.     {

  16.         if(flock(fd,LOCK_EX|LOCK_NB)==0)
  17.         {
  18.             printf("the file was locked by the process.\n");
  19.             if(-1!=write(fd,s,128))
  20.             {
  21.                 printf("write %s to the file %s\n",s,path);
  22.             }
  23.             else
  24.             {
  25.                 printf("cannot write the file %s\n",path);
  26.             }
  27.                
  28.         }
  29.         else
  30.         {
  31.             printf("the file was locked by other process.Can't write...\n");
  32.         }
  33.         flock(fd,LOCK_UN);
  34.         close(fd);
  35.     }
  36.     else
  37.     {
  38.         printf("cannot open file %s\n",path);
  39.     }
  40. }


阅读(2293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~