Created
July 15, 2018 01:53
-
-
Save Xztty/f15862fa1d0f6f0d6bb78e5057adeca1 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_WRONLY); | |
if(fd == -1) { | |
printf("Unable to open the file\n"); | |
exit(1); | |
} | |
static struct flock lock; | |
lock.l_type = F_WRLCK; | |
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