On Fri, Mar 12, 2010 at 7:19 AM, Pedro <[email protected]> wrote:
> --- Paul Herring wrote:
>>
>> I 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.
>
>  If fwrite (ptr, size, 1, f) is atomic

Is it?

> and
>  the sh operator >> use the system call dup(int) then
>  the sh operator >> is atomic.

">>" will use a lot more functions that can be pre-empted than just
those two. A lot more.

If you want to guarantee what passes for atomicity, you're going to
either have to roll your own, or change what you're trying to do such
that it doesn't require it.

-- 
PJH

http://shabbleland.myminicity.com/tra
http://www.chavgangs.com/register.php?referer=9375

Reply via email to