Implementation of single process control of Linux C background service program

Implementation of single process control of Linux C background service program

introduce

Usually a background server program must have one and only one process, so how to have a single process?

This example uses the flock function to lock the pid file /var/run/myserver.pid.

  • If the lock is abnormal, it means that the background service process is already running, then the error will be reported and the system will exit.
  • If the lock is successful, it means that the background service process is not running, then the process can be enabled normally

Background service program single process control

Without going into details, let's look at the code directly

#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"

//Service process single instance running //Return value: 1--running, 0--not running, -1--error int server_is_running()
{
  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 shared lock. Multiple processes can share a lock on the same file at the same time.
  // LOCK_EX establishes a mutual exclusion lock. There can be only one exclusive lock on a file at a time.
  if (flock(fd, LOCK_EX|LOCK_NB) == -1)
  {
    //If the lock cannot be added, the service is running and has been locked printf("server is running now! errno=%d\n", errno);
    close(fd);
    return 1;
  }

  // Locking is successful, proving that the service is not running // Do not close or unlock the file handle // The process exits and is automatically unlocked 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 the process pid to the /var/run/myserver.pid file write(fd, pid_buf, strlen(pid_buf));

  return 0;
}

int main(void)
{

  //Process single instance running detection 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;
}

Operation Results

Run the program and you can see that the process pid 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 ...

/var/run/myserver.pid also records the pid number of this process. ps auxf | grep myserver shows that the myserver 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 0.0 3924 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]# 

At this time, if you run the myserver program again, it will report an error and exit, because it detects that the myserver program is already running and cannot start another process, thus achieving single-process control of the background service program

[root@lincoding singleprocess]# ./myserver 
server is running now! errno=11
myserver process is running!!!!! Current process will exit !

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux process control detailed explanation and examples
  • Summary of commands for controlling processes in Linux

<<:  MySQL Community Server 5.6 installation and configuration tutorial under Windows 8

>>:  MySQL 5.6.15 installation and configuration method graphic tutorial under Windows 8

Recommend

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

Pure CSS to achieve a single div regular polygon transformation

In the previous article, we introduced how to use...

How to enter directory/folder in Linux without using CD command

As we all know, without the cd command, we cannot...

Detailed explanation of downloading, installing and using nginx server

download http://nginx.org/en/download.html Unzip ...

Docker meets Intellij IDEA, Java development improves productivity tenfold

Table of contents 1. Preparation before developme...

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

Implementing a puzzle game with js

This article shares the specific code of js to im...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

A summary of some of the places where I spent time on TypeScript

Record some of the places where you spent time on...

WeChat applet implements video player sending bullet screen

This article shares the specific code for WeChat ...

JavaScript canvas to achieve raindrop effects

This article example shares the specific code of ...

Vue implements chat interface

This article example shares the specific code of ...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

Explore VMware ESXI CLI common commands

Table of contents 【Common commands】 [Summary of c...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...