Sponsored Content
Top Forums Shell Programming and Scripting Where are lock and unlock commands? Post 302903567 by Corona688 on Wednesday 28th of May 2014 12:07:05 PM
Old 05-28-2014
You will already find advisory locks in many different systems and languages as "flock".

Code:
$ man 2 flock


FLOCK(2)                   Linux Programmer's Manual                  FLOCK(2)



NAME
       flock - apply or remove an advisory lock on an open file

SYNOPSIS
       #include <sys/file.h>

       int flock(int fd, int operation);

DESCRIPTION
       Apply or remove an advisory lock on the open file specified by fd.  The
       argument operation is one of the following:

           LOCK_SH  Place a shared lock.  More than one  process  may  hold  a
                    shared lock for a given file at a given time.

           LOCK_EX  Place  an  exclusive  lock.   Only one process may hold an
                    exclusive lock for a given file at a given time.

           LOCK_UN  Remove an existing lock held by this process.

       A call to flock() may block if an incompatible lock  is  held  by
       another  process.  To make a nonblocking request, include LOCK_NB
       (by ORing) with any of the above operations.

...

$ perldoc -f flock

       flock FILEHANDLE,OPERATION
               Calls flock(2), or an emulation of it, on FILEHANDLE.  Returns
               true for success, false on failure.  Produces a fatal error if
               used on a machine that doesn't implement flock(2), fcntl(2)
               locking, or lockf(3).  "flock" is Perl's portable file locking
               interface, although it locks only entire files, not records.

...

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies

2. Solaris

How to unlock my editor?

When I want to use the editor ,whether vi or textedit,it prompts for entering a key. How can I disable this function?many thanks for offering solutions! (1 Reply)
Discussion started by: smartboy1461
1 Replies

3. Solaris

how to unlock user ID in solaris?

I only able to lock user ID with passwd -l username It seems there is no option for me to unlock ID in solaris? Is there any command as below? passwd -u username Appreciate someome can share with me the way to do it. (1 Reply)
Discussion started by: dwarf007
1 Replies

4. HP-UX

not able to unlock user

Hi, not able to unlock user eventhough executed /usr/lbin/modprpw -k username Thanks in advance (2 Replies)
Discussion started by: bpsunadm
2 Replies

5. AIX

how to unlock a dvd in HMC

When I do HMC upgrade, I download HMC code from IBM web site and burn it to a DVD disk, when I get the first DVD out of HMC's dvd rom and input the second DVD, it shows error: The requested device is already locked by the process: Install Corrective Service. Would you like to try to request a... (2 Replies)
Discussion started by: rainbow_bean
2 Replies

6. UNIX for Dummies Questions & Answers

Do mv and cp commands lock files in different ways

We have two processes, one which copies a control file into a folder and another which is polling this folder and reading the control files. Sometimes they clash and the reader fails because the copy is still happening. We have been told that we should change the copy to a move because the... (1 Reply)
Discussion started by: sbarnes
1 Replies

7. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

8. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

9. Shell Programming and Scripting

Need help in lock and unlock and after the changes

Requirement:First i need to unlock the directory which i had a script for it.If i select app1 it should unlock the directory and after chnages in the script once need to lock the directory with lock command The below highlighed variables in lock and unlock has to be changed according... (2 Replies)
Discussion started by: bhas85
2 Replies

10. UNIX for Beginners Questions & Answers

Can't unlock encrypted disk

I use Debian default encryption disk encryption. Only /boot it's not encrypted. When I boot I need to type my password, and then I will be logged in. But now I can't login. It always says that I typed the wrong password, but I typed the correct password. I tried to boot in live-CD and try unlock... (3 Replies)
Discussion started by: zognadal
3 Replies
FLOCK(2)						      BSD System Calls Manual							  FLOCK(2)

NAME
flock -- apply or remove an advisory lock on an open file LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <fcntl.h> #define LOCK_SH 1 /* shared lock */ #define LOCK_EX 2 /* exclusive lock */ #define LOCK_NB 4 /* don't block when locking */ #define LOCK_UN 8 /* unlock */ int flock(int fd, int operation); DESCRIPTION
flock() applies or removes an advisory lock on the file associated with the file descriptor fd. A lock is applied by specifying an operation parameter that is one of LOCK_SH or LOCK_EX with the optional addition of LOCK_NB. To unlock an existing lock operation should be LOCK_UN. Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee consistency (i.e., processes may still access files without using advisory locks possibly resulting in inconsistencies). The locking mechanism allows two types of locks: shared locks and exclusive locks. At any time multiple shared locks may be applied to a file, but at no time are multiple exclusive, or both shared and exclusive, locks allowed simultaneously on a file. A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate lock type; this results in the pre- vious lock being released and the new lock applied (possibly after other processes have gained and released the lock). Requesting a lock on an object that is already locked normally causes the caller to be blocked until the lock may be acquired. If LOCK_NB is included in operation, then this will not happen; instead the call will fail and the error EAGAIN will be returned. NOTES
Locks are on files, not file descriptors. That is, file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock, but rather multiple references to a single lock. If a process holding a lock on a file forks and the child explicitly unlocks the file, the parent will lose its lock. Processes blocked awaiting a lock may be awakened by signals. RETURN VALUES
Zero is returned if the operation was successful; on an error a -1 is returned and an error code is left in the global location errno. ERRORS
The flock() call fails if: [EAGAIN] The file is locked and the LOCK_NB option was specified. [EBADF] The argument fd is an invalid descriptor. [EINVAL] The argument operation does not include exactly one of LOCK_EX, LOCK_SH, or LOCK_UN. [EOPNOTSUPP] The argument fd refers to an object other than a file. SEE ALSO
close(2), dup(2), execve(2), fork(2), open(2), flockfile(3), lockf(3) HISTORY
The flock() function call appeared in 4.2BSD. BSD
October 15, 2011 BSD
All times are GMT -4. The time now is 04:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy