Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file locker?

Posted on 2000-04-03
4
Medium Priority
?
195 Views
Last Modified: 2013-12-25
how to make a file locker, so only one person can access the file a time?
0
Comment
Question by:syu1
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 200 total points
ID: 2680805
if you are using PERL, you can use the flock function.

flock FILEHANDLE, OPERATION

Establishes or removes a lock on a file opened with filehandle. This function calls one of the Unix functions flock, lockf, or
the locking capabilities of fcntl, whichever your system supports. If none of these functions exist on your system, flock will
produce a fatal error.

operation is the type of locking function to perform. The number by each operation name is the argument that Perl's flock
takes by default. You may also use the operation names if you explicitly import them from the Fcntl module with use Fcntl
":flock".

     LOCK_SH (1)

          Establishes a shared lock on the file (read lock).

     LOCK_EX (2)

          Establishes an exclusive lock on the file (write lock).

     LOCK_UN (8)

          Removes a lock from the file.

     LOCK_NB (4)

          Prevents flock from blocking while trying to establish a lock with LOCK_SH or LOCK_EX and instructs it to return
          immediately. LOCK_NB must be ored with the other operation as an expression for the operation argument, i.e.,
          (LOCK_EX | LOCK_NB).



This function calls flock(2) on FILEHANDLE. See the manual page for flock(2) for the definition of OPERATION. Invoking flock will produce a fatal error if used on a machine that doesn't implement flock(2) or emulate it through some other locking mechanism. Here's a mailbox appender for some BSD-based systems:

$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

sub lock {
    flock MBOX, $LOCK_EX;
    # and, in case someone appended
    # while we were waiting...
    seek MBOX, 0, 2;
}

sub unlock {
    flock MBOX, $LOCK_UN;
}

open MBOX, ">>/usr/spool/mail/$ENV{'USER'}" or die "Can't open mailbox: $!";

lock();
print MBOX $msg, "\n\n";
unlock();

Note that flock is unlikely to work on a file being accessed through a network file system.
=====================================================
0
 

Expert Comment

by:mwhuen
ID: 2684073
what is the use of
seek MBOX,0,2;
???
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2684103
seek MBOX, 0, 2;


seek FILEHANDLE, OFFSET, WHENCE

This function positions the file pointer for FILEHANDLE, just like the fseek(3) call of standard I/O. The first position in a file is at offset 0, not offset 1, and offsets refer to byte positions, not line numbers.
0
 
LVL 85

Expert Comment

by:ozo
ID: 2684834
But since you opened MBOX with >> won't it automatically seek to the end to append?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question