Unix/Linux Go Back    

UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

How to lock a file in unix?

UNIX for Dummies Questions & Answers

Thread Tools Search this Thread Display Modes
Old Unix and Linux 05-25-2001
tammy_schmuki tammy_schmuki is offline
Registered User
Join Date: May 2001
Last Activity: 25 May 2001, 1:39 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

We wish to keep a sequence number in a file. When someone wants to get the next sequence number we need to lock the file, get the next number and increment it by one. How do you do that?

I know how to get the number and increment it but how do I lock the file and test that it is locked or not locked.
x=`cat filewithseq`
echo $x > filewithseq
Someone must be doing this already. I know that printer job numbers work in a similar way.
Sponsored Links
Old Unix and Linux 05-25-2001
Neo's Unix or Linux Image
Neo Neo is online now Forum Staff  
Join Date: Sep 2000
Last Activity: 22 October 2017, 12:36 AM EDT
Location: Asia pacific region
Posts: 13,777
Thanks: 892
Thanked 1,219 Times in 569 Posts
In this situation, people usually create a test condition before opening the file and operating on the sequence number. The normal test condition is the existance of a temporary file i.e. yourprogramname.LOCK.

Before a script or command opens the file, it first tests for the existance of the LOCK file. If the file exists, the process terminates. How it terminates depends on the user requirements.

That is how it is normally done with scripts. If you need a more atomic method, you must use system calls closer to the kernel and this will require C (or some other) programming wrapper.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied Dedalus UNIX for Advanced & Expert Users 3 09-29-2011 12:02 PM
Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length nstarz Red Hat 1 06-25-2010 01:24 AM
Applying lock on a file in Unix Ksh kum5256 Shell Programming and Scripting 3 07-06-2009 09:37 AM
How to lock an inbox using UNIX scripting Manju-he202 Shell Programming and Scripting 1 05-22-2009 07:36 AM
Folder Lock in Unix tsunami Shell Programming and Scripting 2 11-05-2008 05:37 AM

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