1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <unistd.h>
int main()
{
int pid_file = open ("/run/user/1000/seb128.pid", O_CREAT | O_RDWR, 0666);
int rc = flock (pid_file, LOCK_EX | LOCK_NB);
if (rc) {
if (errno == EWOULDBLOCK) {
printf ("Not the first instance.\n");
return EXIT_FAILURE;
}
} else {
printf ("Yay, it's just me!\n");
sleep (20);
}
}
|