Only one process (Singleton)
There are several ways to prevent two or more of the same processes from running at the same time. Typical methods include using socket binds, using semaphores, and file locking. The following simple example shows file locking to run only one process. C/C++ #include <sys/file.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #include <fcntl.h> #include <stdbool.h> bool singleton () { struct flock lock; int pid_file = open( "/tmp/whatever.pid" , O_CREAT | O_RDWR, 0666 ); if (pid_file < 0 ){ fprintf(stderr, "pid file open error \n " ); return false; } fprintf(stderr, "pid file lock start \n " ); lock.l_type = F_WRLCK; lock.l_start = 0 ; lock.l_whence = SEEK_SET; lock.l_len = 0 ; ...