Re: Concurrent Updates of the Same File by 2 Instances of the Same Shell Script



"John Smith" <wleung7@xxxxxxxxx> writes:

Chris wrote:
Some possibilities:

--Use the presence of another file in the filesystem as a lock.
if i use a lockfile, i can only restrict access to the file by
terminating the S2. How do I make S2 "busy-wait" for S1 to finish such
that S1 and S2 are serialized?

This is done automatically by the lock syscall.

From the shell, you'd use a tool that'd call it.

For example, on unix you can use flock(1):

#!/bin/bash
file=/tmp/example
line="Random line ${RANDOM}"
flock "${file}" bash -c "echo \"${line}\" >> \"${file}\""




You can also try:

flock --timeout=60 "${file}" \
bash -c "sleep 10;echo \"waiter ${RANDOM}\">>\"${file}\"" &
flock --timeout=60 "${file}" \
bash -c "echo \"quicker ${RANDOM}\" >> \"${file}\""


--
__Pascal Bourguignon__ http://www.informatimago.com/

NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
.



Relevant Pages

  • Re: Concurrent Updates of the Same File by 2 Instances of the Same Shell Script
    ... --Use the presence of another file in the filesystem as a lock. ... if i use a lockfile, i can only restrict access to the file by ... This is done automatically by the lock syscall. ... Printing on GNU/Linux? ...
    (comp.unix.programmer)
  • Re: Concurrent Updates of the Same File by 2 Instances of the Same Shell Script
    ... --Use the presence of another file in the filesystem as a lock. ... if i use a lockfile, i can only restrict access to the file by ... --Have them both write to a daemon that serializes the file contents. ...
    (comp.unix.programmer)
  • Re: [PATCH] prune_icache_sb
    ... In Linux a filesystem is a dumb layer which sits between the VFS and the ... inode (that this cluster lock is created for). ...
    (Linux-Kernel)
  • Re: nfs: infinite loop in fcntl(F_SETLKW)
    ... this looping behaviour was introduced recently in commit ... that was indeed a behavioral change introduced in a commit ... And error values from filesystem methods are typically like ... a behavioral change by calling the lock method repeatedly as long as it ...
    (Linux-Kernel)
  • Re: nfs: infinite loop in fcntl(F_SETLKW)
    ... asynchronous locking looks really fishy. ... question is how the caller knows that the filesystem is really going to ... an asynchronous lock? ... goto out; ...
    (Linux-Kernel)

Quantcast