Search code examples
shellunixflock

Does flock maintain a queue when there are multiple files waiting for a lock?


Would be great if someone can help me understand how flock functions. Lets says I have the below scenario:

process A opens the lock file, finds it does not exists, so it creates it.
process A acquires the lock
process B opens the lock (finds it already exists)
process B tries to acquire the lock but has to wait-- Wait 1
process C opens the lock (finds it already exists)
process C tries to acquire the lock but has to wait-- Wait 2
process D opens the lock (finds it already exists)
process D tries to acquire the lock but has to wait-- Wait 3

Based on the above scenario, there are three processes waiting to acquire lock. My question , once the process A releases the lock, how are a waiting jobs processed ? Is it FIFO?

Thanks, Kavin


Solution

  • I tried testing this scenarios with a working example script and I found that the waiting jobs are processed in a random manner.