Flocking a filedescriptor in a shell script Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Flocking a filedescriptor in a shell script without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I thought this would give me uninterrupted begin-end pairs, but it doesn’t:

#!/bin/bash
fun()(
    flock 1 || { echo >&2 "$BASHPID: FAIL: $?"; exit 1; }
    echo "$BASHPID begin"
    sleep 1;
    echo "$BASHPID end"
)
fun &
fun &
fun &
fun &
fun &
fun &
fun &
fun &
fun &
wait

What am I doing wrong?

Answer

The reason for the failure can be found in man 2 flock:

Locks created by flock() are associated with an open file description (see open(2)). This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors.

This means that because all your processes inherit the same file descriptor, when one of them does a lock, they all share it. And locking the same file descriptor twice is a no-op.

My usual solution to stuff like this is to lock the script itself (though this does pose problems if you run the script multiple times simultaneously).

#!/bin/bash
fun()(
    exec 3<"$0"
    flock 3 || { echo >&2 "$BASHPID: FAIL: $?"; exit 1; }
    echo "$BASHPID begin"
    sleep 1;
    echo "$BASHPID end"
)
fun &
fun &
fun &
fun &
fun &
fun &
fun &
fun &
fun &
wait
We are here to answer your question about Flocking a filedescriptor in a shell script - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji