fnctl:C中的参数无效

fnctl:C中的参数无效,c,file-locking,inter-process-communicat,C,File Locking,Inter Process Communicat,我正在处理进程间通信中的文件锁定, 下面的代码让我很烦。。。 在Macintosh中运行终端时 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> int main(int argc , char *argv[]) { // l_type ,

我正在处理进程间通信中的文件锁定, 下面的代码让我很烦。。。 在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;
 }
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
//l_类型、l_位置、l_开始、l_长度、l_pid
struct flock f1={F_WRLCK,SEEK_SET,0,0,0};
int-fd;
f1.l_pid=getpid();
//如果是命令行参数,则指定读锁
如果(argc>1)
{
f1.l_类型=F_RDLCK;
}
如果((fd=open(“lockdemo.c”,O_RDWR))=-1)
{
佩罗(“公开”);
出口(1);
}
printf(“按下以尝试锁定”);
getchar();
printf(“试图锁定…”);
如果(fcntl(fd、F_SETLKW和f1)=-1)
{
perror(“fcntl”);
出口(1);
}
printf(“已锁定!\n”);
printf(“按下以释放锁:”);
getchar();
f1.l_type=F_UNLCK;//设置为解锁同一区域
如果(fcntl(fd、F_SETLK和f1)=-1)
{
perror(“fcntl”);
出口(1);
}
printf(“解锁..\n”);
关闭(fd);
返回0;
}
但出现以下错误: fnctl:参数无效


请帮我回答这个问题……

我试过你的代码,它在Linux Min 12 64位上运行良好,无需任何修改

 gcc lock.c 
-Compaq-6200-Pro-MT-PC ~/Dropbox/Misc $ ./a.out 1
Press <RETURN> to try to get lock
trying to get lock...got lock !
Press <RETURN> to release lock:
Unlocked .. 
-Compaq-6200-Pro-MT-PC ~/Dropbox/Misc $ ./a.out
Press <RETURN> to try to get lock
trying to get lock...got lock !
Press <RETURN> to release lock:
Unlocked .. 
gcc lock.c -Compaq-6200-Pro-MT-PC~/Dropbox/Misc$/a.out 1 按此按钮尝试获取锁 试图得到锁…得到锁! 按下可释放锁: 解锁。。 -Compaq-6200-Pro-MT-PC~/Dropbox/Misc$/a.out 按此按钮尝试获取锁 试图得到锁…得到锁! 按下可释放锁: 解锁。。 当然,我在同一个文件夹中创建了一个lockdemo.c文件


mac上的锁定api似乎有问题。

在MacOs上,
struct flock
的成员顺序与Linux不同

为了使代码可移植,您应该按名称分配字段,而不是采用特定的顺序

从fcntl的MacOS手册页:

         struct flock {
             off_t       l_start;    /* starting offset */
             off_t       l_len;      /* len = 0 means until end of file */
             pid_t       l_pid;      /* lock owner */
             short       l_type;     /* lock type: read/write, etc. */
             short       l_whence;   /* type of l_start */
         };

哪一个
fcntl
呼叫给出了错误?在“试图获得阻止”之后的呼叫,先生,你有gud信誉点…你能为C程序员建立一个聊天室吗…已经有一个聊天室…但不活动…不允许我问问题…所以先生…根据程序,如果未传递任何参数,则文件应获得写锁定…但如果未传递任何参数,则显示上述错误您是否具有文件lockdemo.c的写访问权限?自己在本地创建,确保检查@Klas Lindbäck发布的解决方案。这很可能是导致错误的原因。将此作为一个教训,始终按名称填充结构字段。这是一个非常有效的观点,可能是OP面临问题的原因。+1是的,先生……谢谢!!!!!它起作用了。。。它正确地显示了文件锁定以及等待获得锁定的时间…:)非常感谢。