[TriLUG] Why do File IO event seem to be out-of-order in strace?

Igor Partola via TriLUG trilug at trilug.org
Tue Sep 8 16:33:57 EDT 2015


Looks to me like you are doing fork() incorrectly:

 if(childPID >= 0) // fork was successful
    {
        if(childPID == 0) // child process
        {

This code after the above statement will run for both parent and child.
Since you are doing this in a loop the second process you fork will inherit
the parent's lock.

The correct way to write this would be like so:

childPID = fork();

if (childPID < 0) {
    puts("Could not fork!");
    abort(1);
}

if (childPID == 0) {
    // Do the lock test.
}

if (childPID > 0) {
     // Spawn more children or wait on existing children to exit.
}


For your test, you don't need to fork(). Simply have a program that does
the locking with a 10 second sleep before unlocking it. Fire off this same
program twice and watch the second one take longer than 10 seconds while it
waits for the lock.

Igor


More information about the TriLUG mailing list