How to lock a file in unix? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to lock a file in unix?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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
Lightbulb

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`
x=x+1
echo $x > filewithseq
Someone must be doing this already. I know that printer job numbers work in a similar way.
Sponsored Links
    #2  
Old 05-25-2001
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 20 April 2014, 3:08 PM EDT
Location: Asia pacific region
Posts: 12,898
Thanks: 493
Thanked 846 Times in 381 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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 10:01 PM.