LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2019, 08:07 PM   #1
squinter
LQ Newbie
 
Registered: Jan 2019
Posts: 2

Rep: Reputation: Disabled
Smile 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?
 
Old 01-14-2019, 12:42 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.

Last edited by berndbausch; 01-14-2019 at 12:42 AM. Reason: typo
 
Old 01-14-2019, 02:22 AM   #3
squinter
LQ Newbie
 
Registered: Jan 2019
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for the response, it makes sense now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Docker, FESCo Election, Android App for Flock, Flock Video Volunteers, and Release Naming (5tF LXer Syndicated Linux News 0 07-17-2014 05:30 PM
LXer: Docker, FESco Election, Android App for Flock, Flock Video Volunteers, and Release Naming (5tF LXer Syndicated Linux News 0 07-16-2014 11:51 PM
[SOLVED] shell scripting: value of argument dissapears depending on argument order akelder Programming 5 03-21-2011 11:27 PM
difference between socket returned descriptor and accept returned descriptor naveenisback Programming 1 08-29-2009 04:55 AM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration