LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   flock file descriptor argument (https://www.linuxquestions.org/questions/linux-newbie-8/flock-file-descriptor-argument-4175646144/)

squinter 01-13-2019 08:07 PM

flock file descriptor argument
 
Hi all,

I'm trying to understand the flock command and got stuck trying to understand the following code example:

(
flock -n 9 || exit 1
# ... commands executed under lock ...
) 9>/var/lock/mylockfile

I understand most of the above code but confused about the number 9. I know it's a file descriptor from the man page and I understand the famous usage of file descriptors like "/dev/null 2>&1" but what's the significance of file descriptor 9 in the above code? Or is it just arbitrary and I can just use number 10 or 199?

berndbausch 01-14-2019 12:42 AM

The code opens mylockfile as file descriptor 9, then applies the flock command on that file descriptor.

Any number is fine. I suppose a comparably high number avoids possible clashes with existing file descriptors. The man page I just read uses 200.

Note that the last line is executed first: Before the script within parentheses is executed, the shell opens mylockfile.

squinter 01-14-2019 02:22 AM

Thank you for the response, it makes sense now


All times are GMT -5. The time now is 02:18 PM.