Solved

file locker?

Posted on 2000-04-04
2
215 Views
Last Modified: 2013-12-25
how to create a file locker so that only one person can make writeing to
a file?

thanks!
0
Comment
Question by:she25
2 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2685805
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:dtabacco
ID: 2710417
I don't think flock works under windows implementations of perl such as activeperl, though.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 179
Automating a script for user accounts LINUX 14 76
Recursively Delete Files 5 95
Bartender label printing - switch on and off graphics 3 56
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

820 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