Reopen voters: flock()
doesn't work on named pipes, this is not a dupe.
I was trying to do the following with named pipes:
mkfifo example.txt
( printf "456"; printf "abc"; printf "\n"; ) > example.txt &
( printf "456"; printf "abc"; printf "\n"; ) > example.txt &
( printf "456"; printf "abc"; printf "\n"; ) > example.txt &
If I cat the example.txt
sometimes I got:
456abc
456abc
456abc
Other times I got:
456456456abcabc
abc
I was hoping the writes would be atomic. Is there any way to achieve atomic writes?
printf "456abc\n"
and it will be atomic. The answer in the other question, is in your case unusable. – peterh Oct 13 '18 at 16:30flock()
doesn't work on named pipes"? I just tried it and it seemed to work fine. – Joseph Sible-Reinstate Monica Oct 13 '18 at 17:30flock
with fifos (in his example, he should use(flock 1; printf ...
-- obvious things likemkfifo ff; flock ff -c 'echo yes > ff' & cat ff
really do not work. – Oct 13 '18 at 22:52O_RDONLY
which will lock hard if you're calling it from the fifo writer (it will wait until the writing side of the fifo is opened, which will not happened until the lock is acquired) – Oct 13 '18 at 23:06flock 1;
before eachprintf
as I already suggested in a comment. – Oct 15 '18 at 03:21printf
from each subshell, eg.( flock 1; printf "456"; printf "abc"; printf "\n"; ) > example.txt &
– Oct 15 '18 at 03:30