Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The difference between pidfile and logfile

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Original answer: http://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for

The main idea is translated as follows:

Pidfile is when the user program is starting to run, the process ID is written into the pidfile file for three main purposes:

1. Identify for other programs or operating systems that this program is running, or at least started successfully.

two。 If the plan is to be terminated with the kill command, it allows a writable script to easily detect the running status of the program.

3. With pidfile, you can get the reason why the previous running instance did not exit successfully at very little cost.

The existence of pidfile alone does not guarantee whether the process id is running. This method is not 100% simple, but it is good enough for most programs, and the way to detect whether the process id is in the schedule cannot be used across platforms unless you use the ps tool.

Lockfile is usually used to ensure that two instances of the same program cannot be run at the same time. Don't forget to delete this file after using it.

Pid files are written by some programs to record their process ID while they are starting. This has multiple purposes:

It's a signal to other processes and users of the system that that particular program is running, or at least started successfully.

It allows one to write a script really easy to check if it's running and issue a plain kill command if one wants to end it.

It's a cheap way for a program to see if a previous running instance of it did not exit successfully.

Mere presence of a pid file doesn't guarantee that that particular process id is running, of course, so this method isn't 100% foolproof but "good enough" in a lot of instances. Checking if a particular PID exists in the process table isn't totally portable across UNIX-like operating systems unless you want to depend on the ps utility, which may not be desirable to call in all instances (and I believe some UNIX-like operating systems implement ps differently anyway).

Lock files are used by programs to ensure two (well-behaved) separate instances of a program, which may be running concurrently on one system, don't access something else at the same time. The idea is before the program accesses its resource, it checks for presence of a lock file, and if the lock file exists, either error out or wait for it to go away. When it doesn't exist, the program wanting to "acquire" the resource creates the file, and then other instances that might come across later will wait for this process to be done with it. Of course, this assumes the program "acquiring" the lock does in fact release it and doesn't forget to delete the lock file.

This works because the filesystem under all UNIX-like operating systems enforces serialization, which means only one change to the filesystem actually happens at any given time. Sort of like locks with databases and such.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report