Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

dpkg uses a lock file (/var/lib/dpkg/lock), when in use.

  • Why are these lockfiles needed?
  • Why are multiple instances not possible?
share|improve this question
    
Dpkg does installations. This is a safety mode to avoid conflicts. Just one installation at same time. –  albfan Jul 10 at 6:44

3 Answers 3

This is not a dpkg-specific issue (as the title of my edit suggested). Rather, this is something that every package manager (of which I am aware) does; and for good reason. Though, I do understand why it might be confusing.

Package managers rely on databases to track the information for installed packages. If multiple users attempt to write to a database at the same time, it has a high chance of corrupting data (which would really screw with the system).

As a result, many (all?) package managers rely on a lockfile to signal that the database is being written to, so another client should not be allowed to do so.


Note that intelligent package managers may be able to determine when a request is read-only and might not need to lock the database. As a result; it is possible for some actions to be able to be run concurrently where others will not be.

share|improve this answer
2  
I think portage and paludis (Gentoo's package managers) is able to work in parallel, if it uses locks at all it does so only briefly when adding packages to the installed list. –  Vality Jul 10 at 13:18
    
I will take a look at Gentoo's package managers and update my answer shortly. –  HalosGhost Jul 10 at 16:32
    
I cannot find up-to-date information on this; but at least, for a while, both paludis and portage utilized lock files. Note, this is such a common strategy because it's one of the only safe ways to prevent data corruption; I would be surprised if either portage or paludis didn't still use such a method. –  HalosGhost Jul 10 at 17:01
1  
Ah, it seems you are correct, however the scope of these locks is different, they are only used when updating the package index (the lost of packages available), actually installing packages is a read only operations and so does not invoke the lock. However, yes, I see what you mean, it makes sense the strategy is so common. –  Vality Jul 10 at 19:52

The lock file is used to prevent parallel execution of multiple instances.

Why is this important for a package managers?

A package manager — from a high level view — is a program which applies complex changes to the hard disk.

The changes cannot be done in one step (“atomic”), so there are multiple steps; many of the steps depend on the result of earlier steps.

So, the package manager needs to either analyze the hard disk before executing each step, or simply analyze it once and keep track of the changes it applies itself. The first option is extremely slow. The second requires that no other instance make changes.

There are many other problems that could appear.

It is not impossible to implement a package manager that can work in parallel, but it’s too complicated to be worth it. As in, You cannot imagine how complicated. Really.

share|improve this answer

dkpg (and rpm and most other traditional package managers) work by installing packages into a global space, which means that packages can conflict with each other (e.g. A and B can't be installed at the same time, because they both install /usr/lib/libfoo.so). Package managers must detect such conflicts and reject such installation requests in order to keep the system in a consistent state. Having multiple instances of the package manager running at the same time would be very complicated and error prone.

Conflict-free package managers (e.g. http://0install.net) can and do allow multiple packages to be installed in parallel¹, and don't need lock files (A/libfoo.so and B/libfoo.so will go in different directories).


1 Parallel both in the sense of being present and available on the system at the same time, and in the sense of being downloaded and added to the system concurrently.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.