There is a simple C program that opens / writes / closes a .bin file using flock() . I would like to make the same lock in the bash script, which will copy this .bin to avoid simultaneous work with the file.

As I understood from the help ( https://linux.die.net/man/1/flock ) this lock can be made, but will it cooperate with the sishny flock() ?

If yes, then I beg you to show a simple example script that shows work with flock.

1 answer 1

The flock program uses the flock () system call. source quotation:

  while (flock(fd, type | block)) { 

The function description is connected with this line :

 #include <sys/file.h> 

a simple example script that shows work with flock

for example :

 #!/bin/bash ( # ожидать блокировки файла /var/lock/.myscript.exclusivelock # (файловый дескриптор 200) не более 10 секунд flock -x -w 10 200 || exit 1 # сделать что-нибудь при удачной блокировке ) 200>/var/lock/.myscript.exclusivelock 
  • Is it possible to block the device this way or is it better to create a lock file next to it? - eri
  • @eri, theoretically, you can. but for reliability it is better to create a file. but not “near”, but in the /var/lock directory specifically designed for this stanadart lsb (as given in man flock and in my example). This directory is available for all to write. By the way, in the standard and about “devices” it is mentioned, so this directory is the most suitable place for your task. - aleksandr barakin