Created
October 17, 2016 16:00
-
-
Save rkujawa/87fbd2ac11f9cb9c90f6875918285d55 to your computer and use it in GitHub Desktop.
This file contains 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 <stdlib.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <sys/file.h> | |
static const char *filename = "/tmp/lockme"; | |
int | |
main(int argc, char *argv[]) { | |
int fd; | |
fd = open(filename, O_RDWR | O_CREAT, S_IRWXU); | |
if (fd < 0) { | |
fprintf(stderr, "failed to open %s\n", filename); | |
return EXIT_FAILURE; | |
} | |
flock(fd, LOCK_EX); | |
printf("obtained lock on %s, press any key to unlock\n", filename); | |
getchar(); | |
flock(fd, LOCK_UN); | |
close(fd); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment