Skip to content

Instantly share code, notes, and snippets.

@rkujawa
Created October 17, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkujawa/87fbd2ac11f9cb9c90f6875918285d55 to your computer and use it in GitHub Desktop.
Save rkujawa/87fbd2ac11f9cb9c90f6875918285d55 to your computer and use it in GitHub Desktop.
#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