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

mysql8.0 windows x64 zip package installation and configuration tutorial

MySQL 8 Windows version zip installation steps (d...

JS implementation of Apple calculator

This article example shares the specific code of ...

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

Detailed explanation of SQL injection - security (Part 2)

If there are any errors in this article or you ha...

How to expand the disk partition for centos system

Problem/failure/scenario/requirement The hard dis...

Teach you how to build Tencent Cloud Server (graphic tutorial)

This article was originally written by blogger We...

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

Detailed explanation of real-time backup knowledge points of MySQL database

Preface The need for real-time database backup is...

A brief introduction to MySQL dialect

Putting aside databases, what is dialect in life?...

Analysis of MySQL query sorting and query aggregation function usage

This article uses examples to illustrate the use ...

Vue.js implements simple folding panel

This article example shares the specific code of ...

Examples and comparison of 3 methods for deduplication of JS object arrays

Table of contents 1. Comparison of data before an...