Analysis on the function of PID file under Linux/unix

Source: Internet
Author: User
Tags flock

Reprint: http://blog.csdn.net/changli_90/article/details/8911191

In the Linux system directory/var/run below generally we will see a lot of *.pid files. And often the newly installed program will also generate its own PID file under the/var/run directory after running. So what are the effects of these PID files? What is the content of it?

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

You can see it with the Cat command.

(2) the role of the PID file: Prevents the process from starting multiple replicas . Only the process that obtains the PID file (fixed path fixed file name) Write permission (F_WRLCK) can start normally and write its own PID to the file. Extra processes for the same program are automatically exited.

(3) Programming Skills:

Call Fcntl to set the PID file's lock f_setlk state, which locks the 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, the same process is already running and the current process ends exiting.

Lock.l_type = F_wrlck; Lock.l_whence = Seek_set;

if (Fcntl (FD, F_SETLK, &lock) < 0) {//Lock unsuccessful, exit ...} sprintf (buf, "%d\n", (int) PID); Pidsize = strlen (BUF); if (tmp = Write (FD, buf, pidsize))! = (int) pidsize) {//write unsuccessful, exit ...}

(4) Some precautions: i) if the process exits, the lock that the process adds automatically expires. II) If the process closes the file descriptor FD, the added lock is invalidated. (This file descriptor cannot be closed during the entire process run) III) the state of the lock is not inherited by the quilt process. If the process is closed, the lock fails without the pipe process running. (Locks is associated with processes. A process can only has one kind of lock set for each byte of a given file. When any file descriptor for that file was closed by the process, all of the locks that process holds on that file is Rele ased, even if the locks were made using other descriptors that remain open. Likewise, locks is released when a process exits, and is not inherited by the child processes created using fork.)

(5) Reference: Fcntl (File Lock) header file #include <unistd.h> #include <fcntl.h> function definition int fcntl (int fd, int cmd, struct floc K *lock); function explanation FD: File descriptor set of file descriptors, parameter cmd for the call to monopoly f_dupfd copy parameter FD file descriptor, the enforcement wins return the new copy of the file descriptor, F_GETFD get close-on-exec symbol, if some symbols fd_ The cloexec bit is 0, which means that when the EXEC () coherence function is called, the file will not close the F_SETFD setting close-on-exec symbol, which takes the fd_cloexec bit of the parameter arg to decide F_GETFL get the open () set symbol f_ SETFL Change Open () set the symbol F_GETLK get the file lock of the state, according to the description of lock, decide whether the file lock f_setlk set file lock state of affairs, at the moment Flcok, the L_tpye value of the structure will be f_rdlck, F_wrlck or f_ Unlck, in case the lock can not occur, then return-1 f_setlkw is the blocking version of F_SETLK, in the inability to obtain a lock will go into the sleep state, in case the lock can be captured to catch the signal return parameter lock pointer for the flock construction pointer defines the following struct flock { ... short l_typejngaoy.com; Short l_whence; off_t L_start; The switching position of the locked area off_t L_len; The size of the locked area pid_t l_pid; The process of locking the action ...}; 1_type has three kinds of things: F_rdlck read lock (Share lock) F_wrlck write lock (Repel lock) F_unlck unlock l_whence There are also three measures seek_set start with file as the starting position of the lock Seek_ Cur with the current file read and write position as the starting position of the lock seek_end with the end of the file as the starting position of the lock is returned 0, if there is errors return-1 L_len: The length of the lock area L_pid: Has blocked the current course of the lock, the course number of its holding process is stored in l_pid, by f_ GETLK return the L_start setting to 0,l_whence is set to Seek_set,l_len to 0

Analysis of the function of PID file under Linux/unix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.