Detailed description of PID files in / var / run / directory under Linux and the role of PID files

Time:2019-4-18

First of all, I will introduce the PID file under Linux / var / run / directory. The details are as follows:

The *. PID file in the / var / run / directory of Linux system is a text file with only one line of content, that is, the PID of a process. The function of the PID file is to prevent the process from starting multiple copies. Only the process that obtains the write permission (F_WRLCK) of the specific PID file (fixed path and file name) can start normally and write its own process PID to the file. The redundant process of the other same program will exit automatically.

Programming implementation:

Call fcntl () system call to set the specified PID file to F_WRLCK lock state, write the PID of the current process if the lock succeeds, and the process continues to execute downward; if the lock fails, it indicates that the same process is running and the current process exits.

#define PID_FILE "/var/run/xxxx.pid"
int lock_file(int fd)
{
  struct flock fl;
  fl.l_type = F_WRLCK;
  fl.l_start = 0;
  fl.l_whence = SEEK_SET;
  fl.l_len = 0;
  return (fcntl(fd, F_SETLK, &fl));
}
int alone_runnind(void)
{
  int fd;
  char buf[16];
  fd = open(PID_FILE, O_RDWR | O_CREAT, 0666);
  if (fd < 0)
  {
    perror("open");
    exit(1);
  }
  if (lock_file(fd) < 0)
  {
    if (errno == EACCESS || errno == EAGAIN)
    {
      close(fd);
      printf("alone runnind\n");
      return -1;
    }
    printf("can't lock %s: %s\n", PID_FILE, strerror(errno));
  }
  Ftruncate (fd, 0); // Set file size to 0
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf) + 1);
  return 0;
}

Attention should be paid to:

1. The locks added by the process will automatically fail after the process exits.
2. When the process closes the file descriptor fd, the lock is invalid. (So the FD cannot be shut down throughout the lifecycle of the process);
3. The state of the lock will not be inherited by the child process. If the process closes, it will fail regardless of whether the process is running or not.

Here’s how PID files work in the / var / run directory under Linux

Under the Linux system directory / var / run, we usually see a lot of *. PID files. And often the newly installed program will generate its own PID file under the / var / run directory after running. So what’s the use of these PID files? What is its content?

(1) The content of the PID file: the PID file is a text file with only one line of content, recording the ID of the process.

You can see it with the cat command.

(2) The role of PID files: prevent processes from starting multiple copies. Only by obtaining the write permission (F_WRLCK) of the PID file (fixed path fixed file name) can the process start normally and write its own PID into the file. The redundant processes of the other same program exit automatically.

(3) Programming skills:

Call fcntl to set the locked F_SETLK state of the PID file, where the locked flag bit F_WRLCK.

If the lock is successful, the current PID of the process is written and the process continues to execute.

If the lock is unsuccessful, it means that the same process is already running and the current process is terminated and exited.

lock.l_type = F_WRLCK;
 lock.l_whence = SEEK_SET;

 if (fcntl(fd, F_SETLK, &lock) < 0){
  // Lock in unsuccessfully, quit...
 }
sprintf (buf, "%d\n", (int) pid);
 pidsize = strlen(buf);
 if ((tmp = write (fd, buf, pidsize)) != (int)pidsize){
  // Write unsuccessfully, quit...
 }

(4) Some matters needing attention:

I) If the process exits, the lock added by the process will automatically fail.
Ii) If the process closes the file descriptor fd, the lock is invalid. (This file descriptor cannot be closed during the entire process run)
Iii) The state of the lock is not inherited by the child process. If the process closes, the lock fails regardless of whether the process is running.

summary

The above is a detailed description of the PID files in the Linux / var / run / directory and the role of PID files. I hope it will be helpful to you. If you have any questions, please leave a message for me, and the editor will reply to you in time. Thank you very much for your support to developpaer.

Recommended Today

Kafka learning materials

Kafka 1、 Benefits of message middleware 1. Decoupling It allows you to extend or modify processes on both sides independently, as long as you make sure they comply with the same interface constraints. It would be a great waste to put resources on standby to handle such peak visits. The use of message queue can […]