How Linux locks?

Prabesh
2 min readJan 6, 2021

--

Solve the re-occurring could not get lock issue.

Image: Linux Lock. Source: https://www.techsupportpk.com/2017/10/lock-linux-user-accounts.html

When you are new to Linux, you might bump into the following errors, and can be frustrating, if you don’t know what actually happened and how to resolve them.

So, take a deep breath and relax, we will try to fix the problem.

Before you try to solve the problem, let me tell you a little bit about how does lock works in Linux systems.

Lock files on Linux are created to avoid race conditions when multiple processes are running.

There are two types of lock:

  1. Exclusive lock
  2. Shared lock

Exclusive lock

When a process holds an exclusive lock on a resource, then it cannot be acquired by other processes until it releases it. This resource cannot acquire by a process whether an exclusive or shared lock on a resource until released.

Shared lock

When a process holds a shared lock, then other processes can acquire the shared lock, but not an exclusive lock on the resource. In short, on shared lock multiple processes can hold multiple resources, whereas exclusive lock is limited to a single process.

You can view all the system locks using lslock command.

Fig: Linux lock. Source: Author

Now, after seeing the above definition and picture, you are aware of why there are lock files in the Linux system.

Let’s try to resolve the issue.

Sometimes you might get the following errors stating

Unable to acquire frontend lock
Resource not available
Cloud not open lock file /var/lib/dpgk/lock-frontend

Example of error: Cloud not open lock file /var/lib/dgpk/lock-frontend

This normally occurs when another apt command is running in the background or synaptic package manager is running or a software update is running.

Fog: Lock Issue. Source: Author

In order to solve this issue, you can use the following series of commands. If. there are no apt or apt-get processes running then it's alright, move to the next step.

Now you should be able to run the commands apt commands like an update, install or upgrade.

Thank you so much for reading this article. I hope after reading this article, you got some understanding of how Linux lock works and how to solve the issue.

If you want to read some more interesting DevOps/ SRE related blogs, here is my website.

--

--

Prabesh

Senior Site Reliability Engineer & Backend Engineer | Docker Captain 🐳 | Auth0 Ambassador @Okta | https://www.linkedin.com/in/prabeshthapa