/* lock-file.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int
main (int argc, char *argv[])
{
struct flock lock_info;
int fd_no;
fd_no = open ("a.out", O_RDWR);
if (fd_no == -1)
{
perror ("open ()");
exit (1);
}
while (1)
{
if (-1 == fcntl (fd_no, F_GETLK, &lock_info)) /* get lock info */
{
perror ("fcntl()");
exit (1);
}
printf ("Lock stat of %s : %d \n", lock_info.l_type);
if (lock_info.l_type != F_UNLCK) sleep (1); /* if file is locked then
sleep */
else
{
lock_info.l_type = F_RDLCK; /* lock for reading */
lock_info.l_whence = SEEK_SET;
lock_info.l_start = 0l;
lock_info.l_len = 0;
fcntl (fd_no, F_SETLK, &lock_info);
break;
}
}
printf (" %s file locked ...\n", argv[0]);
printf (" I'm going to sleep. \n");
printf (" Press Ctrl-C to unlock! \n");
while (1) sleep (5);
return 0;
}
-----
현재 디렉토리에 a.out이라는 파일은 있고
gcc -o lock-file lock-file.c
이렇게 컴파일도 잘 됩니다.
그런데 아래와 같은 메시지를 출력하고 마는 군요.
fcntl(): Invalid argument
분명이 문제가 없는것 같은데, 왜 이런 에러가 나는지 잘 모르겠네요.
누구 아시는 분 없어요?