introduce
Usually a daemon program must have only one process, so how about a single process?
This example locks the pid file of the /var/run/myserver.pid record using the flock function
- If the lock is abnormal, it indicates that the background service process is already running. In this case, an error message is displayed to exit
- If the lock is added successfully, it indicates that the background service process is not running. In this case, you can start the process
Background server single process control
I won’t go into details, but I’ll just look at the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#define PID_BUF_LEN (20)
#define RUN_PID_FILE "/var/run/myserver.pid"
// The server process runs singleton
// Return values: 1-- running, 0-- not running, -1-- error
int server_is_running(a)
{
int fd = open(RUN_PID_FILE, O_WRONLY|O_CREAT);
if(fd < 0)
{
printf("open run pid err(%d)! %s\n", errno, RUN_PID_FILE);
return - 1;
}
/ / lock
// LOCK_SH Establishes a share lock. Multiple processes can simultaneously share a lock on the same file.
// LOCK_EX Establishes a mutex lock. A file has only one mutex lock at a time.
if(flock(fd, LOCK_EX|LOCK_NB) == - 1)
{
// If no lock is set, the service is running and locked
printf("server is runing now! errno=%d\n", errno);
close(fd);
return 1;
}
// The service is not running
// Do not close or unlock file handles
// The process exits and is unlocked automatically
printf("myserver is not running! begin to run..... pid=%ld\n", (long)getpid());
char pid_buf[PID_BUF_LEN] = {0};
snprintf(pid_buf, sizeof(pid_buf)- 1."%ld\n", (long)getpid());
// Write process pid to /var/run/myserver.pid
write(fd, pid_buf, strlen(pid_buf));
return 0;
}
int main(void)
{
// Process single instance running check
if(0! = server_is_running()) {printf("myserver process is running!!!!! Current process will exit ! \n");
return - 1;
}
while(1)
{
printf("myserver doing ... \n");
sleep(2);
}
return 0;
}
Copy the code
The results
Run the program, the pid of the process is 6965
[root@lincoding singleprocess]# ./myserver
server is not running! begin to run..... pid=6965
myserver doing ...
myserver doing ...
myserver doing ...
myserver doing ...
myserver doing ...
myserver doing ...
myserver doing ...
myserver doing ...
Copy the code
The/var/run/myserver. Pid also records the process of pid number, ps auxf | grep myserver knowable mysever process has been running
[root@lincoding singleprocess]# cat /var/run/myserver.pid 6965 [root@lincoding singleprocess]# [root@lincoding Singleprocess] # ps auxf | grep myserver root 6965 0.0 3924 0.0 460 PTS / 0 S + 00:32 0:00 | \ _ / myserver root 9976 0.0 0.0 103256 856 pts/1 S+ 00:35 0:00 \_ grep myserver [root@lincoding singleprocess]#Copy the code
At this time, run myServer program, then will report an error exit, because the detection of myServer program has been running, can not be another process, so as to achieve the background service program single process control
[root@lincoding singleprocess]# ./myserver
server is runing now! errno=11
myserver process is running!!!!! Current process will exit !
Copy the code