免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: xpkiller
打印 上一主题 下一主题

进程间文件加锁的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-08-07 20:48 |只看该作者
如果是两个进程的话,用fcntl()设置锁,参考APUE 14.3:
#include <fcntl.h>

int fcntl(int filedes, int cmd, ... /* struct
flock *flockptr */ );




Returns: depends on cmd if OK (see following), 1 on error


For record locking, cmd is F_GETLK, F_SETLK, or F_SETLKW. The third argument (which we'll call flockptr) is a pointer to an flock structure.

   struct flock {
     short l_type;   /* F_RDLCK, F_WRLCK, or F_UNLCK */
     off_t l_start;  /* offset in bytes, relative to l_whence */
     short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
     off_t l_len;    /* length, in bytes; 0 means lock to EOF */
     pid_t l_pid;    /* returned with F_GETLK */
   };

This structure describes

The type of lock desired: F_RDLCK (a shared read lock), F_WRLCK (an exclusive write lock), or F_UNLCK (unlocking a region)

The starting byte offset of the region being locked or unlocked (l_start and l_whence)

The size of the region in bytes (l_len)

The ID (l_pid) of the process holding the lock that can block the current process (returned by F_GETLK only)

论坛徽章:
0
12 [报告]
发表于 2008-08-09 21:58 |只看该作者
用fcntl()文件锁

论坛徽章:
0
13 [报告]
发表于 2008-08-10 00:18 |只看该作者
如果B进程是你自己写的,那么当你不希望B进程对文件进行写操作的时候,为什么会在B进程中加入写文件的代码???
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP