Created
July 15, 2018 01:53
-
-
Save Xztty/2a39729def5ca71c2e3164e2b56ac9af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
int main(int argc, char **argv) { | |
if (argc > 1) { | |
int fd = open(argv[1], O_RDONLY); | |
if(fd == -1) { | |
printf("Unable to open the file\n"); | |
exit(1); | |
} | |
static struct flock lock; | |
lock.l_type = F_RDLCK; | |
lock.l_start = 0; | |
lock.l_whence = SEEK_SET; | |
lock.l_len = 0; | |
lock.l_pid = getpid(); | |
int ret = fcntl(fd, F_SETLK, &lock); | |
printf("Return value of fcntl:%d\n",ret); | |
if(ret==0) { | |
while (1) { | |
scanf("%c", NULL); | |
} | |
} else { | |
printf("error, ret:%d, errno:%d\n", ret, errno); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment