On Fri, Mar 12, 2010 at 6:11 AM, Pedro <[email protected]> wrote: > I was thinking about: > > Is the sh operator >> to redirect and append a single fwrite to stdout > atomic or not?
Nope. Not in the conventional programming sense of the word anyway. > Can you prove your answer to the question above? Imagine two threads calling sh and using the >> operator. First thread gets preempted after opening file, but before writing (everything) to it. Second thread executes and manages to open, write and close the file. First thread swaps back in and completes. flock() may, or may not be involved here, but there's still a window of opportunity whereby thread 2 can come in before thread 1 has completed. > And on cmd.exe, is the operator >> atomic? Unlikely, see thought experiment above. > Then I thought about the following POS. It is a command for sh: > while [[ true ]] ; do cat a | cat /dev/stdin >> a ; done ; > > If the file a contains any byte it will fill your disk. Yup (well maybe not the disk, but certainly your quota,) but there are lots of opportunities for the kernel to schedule other stuff while it's doing it. > If the file a is empty it will consume your CPU time. That's one thing it certainly won't do, at least not to the detriment of other programs. Other than the >> there, you're calling at least two other programs (or pseudo programs): true and cat > It does not use dd. dd certainly isn't atomic. -- PJH http://shabbleland.myminicity.com/tra http://www.chavgangs.com/register.php?referer=9375
