Tag Info

Hot answers tagged

20

If lockfile is not installed on your system, then mkdir will do the work: it's an atomic operation, and it fails if the directory already exists (as long as you don't add the -p command-line switch). create_lock_or_wait () { path="$1" wait_time="${2:-10}" while true; do if mkdir "${path}.lock.d"; then break; fi ...


15

flock(1) #!/bin/bash # Makes sure we exit if flock fails. set -e ( # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds flock -x -w 10 200 # Do stuff ) 200>/var/lock/.myscript.exclusivelock This ensures that code between "(" and ")" is run only by one process at a time and that the process does wait for a lock too ...


9

lockfile(1) looks like a good candidate, though beware that it's part of the procmail package, which you may not have installed on your machine yet. It's a popular enough package that it should be packaged for your system if it's not installed yet. Three of the four systems I checked have it, and the other has it available. Using it is simple: #!/bin/sh ...


9

Both manage a limited resource. I'll first describe difference between binary semaphore (mutex) and spin lock. Spin locks perform a busy wait - i.e. it keeps running loop: while (try_acquire_resource ()); ... release(); It performs very lightweight locking/unlocking but if the locking thread will be preempted by other which will try to access the same ...


8

My only experience in dealing with semaphores and shared memory is through the use of the command ipcs. Take a look at the ipcs man page for more details. This command shows you what processes have semaphores: $ ipcs -s ------ Semaphore Arrays -------- key semid owner perms nsems 0x4d114854 65536 saml 600 8 ...


7

If you read the manpage for semget, in the Notes section you'll notice: System wide maximum number of semaphore sets: policy dependent (on Linux, this limit can be read and modified via the fourth field of /proc/sys/kernel/sem). On my system, cat /proc/sys/kernel/sem reports: 250 32000 32 128 So do that on your system, and then echo it back after ...


4

As the question implies in saying that spinlocks are a "waste", spinlocks should be held only briefly. Spinlocks are not the only way to synchronize multiple threads. Mutexes/semaphores are also used in the Linux kernel, as are other synchronization primitives (e.g. waitqueues, events). However the kernel has to deal with cases that userspace never sees, ...


4

The system call mkdir() is atomic on POSIX filesystems. So, using the mkdir command in such a way that it involves exactly one call to mkdir() would achieve your purpose. (IOW, don't use mkdir -p). The corresponding unlock is rmdir of course. Caveat emptor: mkdir() might not be atomic on network filesystems.


3

Maybe the lockfile command will do what you need. lockfile ~/.config/mylockfile.lock ..... rm -f important.lock


3

The choice between a spinlock and another construct which causes the caller to block and relinquish control of a cpu is to a large extent governed by the time it takes to perform a context switch (save registers/state in the locking thread and restore registers/state in another thread). The time it takes and also the cache cost of doing this can be ...


1

Spinlocks are used in an interrupt context, where sleeping is not allowed. They poll in a tight loop, doing nothing else until the resource is acquired. Mostly used in ISRs, and more secure and efficient. Semaphores can be used in a process context, where sleeping is ok.


1

Others have answered. I shall summarize the cases where you would use spinlock and rules to use spinlock. 1. When spinlock is used ? Ans: In the following situations. The thread that holds the lock is not allowed to sleep. The thread that is waiting for a lock does not sleep, but spins in a tight loop. When properly used, spinlock can give higher ...


1

If you're only running on Unix, use fifos. You can write work records to the fifo and have processes read from this file and your readers will block on the fifos. Lock files are ok, but for what you describe I would go with fifos



Only top voted, non community-wiki answers of a minimum length are eligible