|
Home > Archive > Unix Programming > February 2006 > Concurrent Updates of the Same File by 2 Instances of the Same Shell Script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Concurrent Updates of the Same File by 2 Instances of the Same Shell Script
|
|
| John Smith 2006-02-17, 10:40 pm |
| OS: solaris
shell: bash
I have a UNIX shell script which appends new text to a file. Multiple
instances of this program may run concurrently, hence there is a
possiblity that 2 or more instances (say S1 and S2) of this shell
script will be modifying the file at the same time.
I want to make sure that S1 can finish appending it's changes to the
file, before S2 starts to append its changes.
How do I handle this concurrently update issue so that the changes made
by S1 and S2 are not interpersed with each other's?
| |
| John Smith 2006-02-17, 10:40 pm |
| 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?
--Have them both write to a daemon that serializes the file contents.
--Change the design so they don't touch the same file.
| |
| Pascal Bourguignon 2006-02-17, 10:40 pm |
| "John Smith" <wleung7@gmail.com> 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.
| |
| Roger Leigh 2006-02-17, 10:40 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pascal Bourguignon <usenet@informatimago.com> writes:
> "John Smith" <wleung7@gmail.com> writes:
>
>
> This is done automatically by the lock syscall.
lock(2) isn't implemented by all systems (e.g. Linux). The SUS/POSIX
lockf(2) (or fcntl(2) F_SETFL/F_GETFL operations) are most portable,
and work over NFS. There's also the older and less useful flock(2).
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>
iD8DBQFD9OZhVcFcaSW/uEgRAra/AKCU/K3CRYYXARFyR4Bju3BzW9hgiwCfVXZi
jM3tjgp8kl5PY90n5YtFkZ0=
=0s6L
-----END PGP SIGNATURE-----
|
|
|
|
|