Ubuntu: What is the role of lock files in a system?


I can see there are many lock file here and there in my system. What is the exact role and purpose of those lock files?

In my system I got this list,

/run/lock/whoopsie/lock  /proc/sys/dev/cdrom/lock  /var/lib/dpkg/lock  /var/lib/apt/lists/lock  /var/cache/apt/archives/lock  


They are all lock files. Link for more information.

Different applications and daemons create their lock files, if they need to protect themselves from crashing or from corrupting information.

For example:


lock for whoopsie, it is Ubuntu Error Reporting daemon. It collects information about crashes, so it need to protect some information from changes until it collects everything.


Lock file for you cdrom device. Only one application at a time can control cdrom device, that's why it is there. The same thing for every devices.

/var/lib/dpkg/lock  /var/lib/apt/lists/lock  /var/cache/apt/archives/lock  

Lock file for package managers. They usually blocking their sources database

Also a little quote from wikipedia:

The following example illustrates the interceding update problem:

  1. Process A reads a customer record from a file containing account information, including the customer's account balance and phone number.

  2. Process B now reads the same record from the same file so it has its own copy.

  3. Process A changes the account balance in its copy of the customer record and writes the record back to the file.

  4. Process B, which still has the original stale value for the account balance in its copy of the customer record, updates the account balance and writes the customer record back to the file.

  5. Process B has now written its stale account-balance value to the file, causing the changes made by process A to be lost.

This is one of examples. There many other situations that can lead to a problem. That's why OS need lock files


They're generally used to ensure only one instance of an application is running at once.

This is particularly important in programs like apt-get which may break if more than one instance runs at once.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »