Sample Header Ad - 728x90

FIFO-based semaphore explanation

2 votes
2 answers
1786 views
I am trying to work on some parallelization of many processes (task send/to be executed on many (let's say hundreds) nodes). I came across this solution: https://unix.stackexchange.com/a/216475
-bash
    # initialize a semaphore with a given number of tokens
    open_sem(){
        mkfifo pipe-$$
        exec 3pipe-$$
        rm pipe-$$
        local i=$1
        for((;i>0;i--)); do
            printf %s 000 >&3
        done
    }
    
    # run the given command asynchronously and pop/push tokens
    run_with_lock(){
        local x
        # this read waits until there is something to read
        read -u 3 -n 3 x && ((0==x)) || exit $x
        (
         ( "$@"; )
        # push the return code of the command to the semaphore
        printf '%.3d' $? >&3
        )&
    }
    
    N=4
    open_sem $N
    for thing in {a..g}; do
        run_with_lock task $thing
    done
I need some explanation here: **open_sem()** 1. what does exec 3pipe-$$ do? 2. why is it removed afterwards? **run_with_lock()** 1. what does this && ((0==x)) || exit $x part mean? 2. ( "$@"; ) - as far as i know this is list of all arguments passed... but what does it do here? These are main obstacles for me to understand the process, but feel free to explain the whole flow :) PS: I would just make a comment under that post, but I have just registered and do not have reputation to do so. Also it might be useful for others. Thanks! J.
Asked by Jozef Dzurenda (21 rep)
Nov 4, 2020, 08:43 PM
Last activity: Mar 3, 2022, 12:05 AM