Herd lock order?

im using a simple test script from http://www.tuxradar.com/practicalphp/8/11/0 like this

<?php
$fp = fopen("foo.txt", "w");
if (flock($fp, LOCK_EX)) {
    print "Got lock!\n";
    sleep(10);
    flock($fp, LOCK_UN);
}

i opened 5 shells and executed the script one by one the scripts are blocked until the lock is free and then continues to work after release

im was not very interested in the php file, but my question is: does anyone know the order in which flock () is acquired?

e.g.
t0: process 1 lock's
t1: process 2 try_lock < blocking
t2: process 3 try_lock < blocking
t3: process 1 releases lock
t4: ?? which process get the lock?

is there a simple deterministic order, for example, a queue or does the kernel “just” choose one of the “more complex rules"?

+3
source share
1 answer

, , , . - .

, , . fs/locks.c:

/* Insert waiter into blocker block list.
 * We use a circular list so that processes can be easily woken up in
 * the order they blocked. The documentation doesn't require this but
 * it seems like the reasonable thing to do.
 */

, , flock(). SysV (semget()/semop()).

, -1. semop() sem_op - . semop() sem_op 1 - . semop() sem_op 1 ..

+8

Source: https://habr.com/ru/post/1741055/


All Articles