【发布时间】:2013-01-16 09:59:11
【问题描述】:
我正在处理进程间通信中的文件锁定, 以下代码使我感到困惑... 在 Macintosh 中通过终端运行时
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc , char *argv[])
{
// l_type , l_whence , l_start , l_len , l_pid
struct flock f1 = {F_WRLCK , SEEK_SET , 0 , 0 , 0};
int fd;
f1.l_pid = getpid() ;
// if command line arguments , then assign a Read Lock
if (argc > 1)
{
f1.l_type = F_RDLCK ;
}
if ((fd = open("lockdemo.c", O_RDWR)) == -1)
{
perror("open");
exit(1);
}
printf("Press <RETURN> to try to get lock");
getchar() ;
printf("trying to get lock...");
if (fcntl(fd, F_SETLKW , &f1) == -1)
{
perror("fcntl");
exit(1);
}
printf("got lock !\n");
printf("Press <RETURN> to release lock:");
getchar();
f1.l_type = F_UNLCK ; //set to unlock same region
if (fcntl(fd, F_SETLK , &f1) == -1)
{
perror("fcntl");
exit(1);
}
printf("Unlocked .. \n");
close(fd);
return 0;
}
但出现以下错误: fnctl : 无效参数
请帮助我解决这个问题...
【问题讨论】:
-
哪个
fcntl调用会导致错误? -
“试图获得块”之后的那个
-
先生,您有 gud 声望点...请您为 C 程序员创建一个聊天室...已经有一个房间
..但不活动...和 不允许我提问......所以 PLZ 先生......
标签: c file-locking inter-process-communicat