Sara wrote:
> (sub get_number {
> open(NUMBER,">>data.txt");
> flock (NUMBER, 2);

Use the constants from the Fcntl module.

>
> Do blah blah blah
>
> close (NUMBER);
>
> closing a file automatically removes the lock??

Yes.

> or should I have to
> unlock it by placing
>
> flock (NUMBER, 8);
> close (NUMBER);

No, you don't need to do that. In fact, it used to be dangerous, because of
buffering. If you unlocked the file, buffered data could be written after
the lock was released. Perl now automatically flushes the file before
unlocking to avoid this. Bottom line: if you're going to close the file,
don't worry about explicitly unlocking. You only need to unlock if you want
to allow another process to gain the lock while you still hold the file open
(i.e. coordinating changes to the file among multiple processes).

>
> My second question is how I can delay the execution of a script (If
> two users trying to execute the same script) for 30 sec by using sleep
> function? and If I implement sleep function in my script ..... Do I
> really need to use 'flock' in my script or there is no need then.

If you want to make sure the two programs aren't changing the file
simultaneously, you should use flock. If you dont' want to block forever if
another process has the file locked, you need to use the LOCK_NB parameter
and then sleep() or whatever until you want to retry the lock.

Note that if multiple processes need to append to the file and each writes
its data with individual calls to syswrite(), you don't need to use locking
(although it doesn't really hurt anything). The kernel will perform
individual writes atomically.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to