Skip to content

Instantly share code, notes, and snippets.

@Xztty
Created July 15, 2018 01:53
Show Gist options
  • Save Xztty/f15862fa1d0f6f0d6bb78e5057adeca1 to your computer and use it in GitHub Desktop.
Save Xztty/f15862fa1d0f6f0d6bb78e5057adeca1 to your computer and use it in GitHub Desktop.
#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