Subject: Re: kernel stack traceback
To: Bill Studenmund <wrstuden@nas.nasa.gov>
From: Andrew Brown <atatat@atatdot.net>
List: tech-kern
Date: 03/19/1999 16:35:20
>> >So the fmode is O_RDWR | O_NONBLOCK | O_APPEND | O_CREAT | O_TRUNC.
>> 
>> >Why do you have O_APPEND & O_TRUNC set? If you have O_TRUNC set, wouldn't
>> >all writes naturally append?
>> 
>> No.
>
>Why not? Obviously if you use pwrite, or throw a seek in there, they won't
>append. But if you open with O_TRUNC, isn't the current file position set
>to the front of the file? Thus writes w/o a seek start from the current
>possition, and update that possition. ??

if you have only one writer, then

	f=open("foo",O_WRONLY);
	lseek(f,0,SEEK_END);
	write(f,foo,sizeof(foo));
	write(f,foo,sizeof(foo));
	...

is the same as:

	f=open("foo",O_WRONLY|O_APPEND);
	write(f,foo,sizeof(foo));
	write(f,foo,sizeof(foo));
	...

but if you add more writers, it's not.  each write() will only update
that single writer's file pointer, not append the data.  likewise, if
you are alternately seeking and writing in an attempt to write to the
end of the file, you're in a race condition where you can get messed
up by someone else doing the same thing.

O_TRUNC is presumably there to empty the file.  O_APPEND is there to
make sure that all writes go to the end of the file, regardless of any
seek()s and read()s (it is opened O_RDRW) that have been done.

-- 
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org             * "ah!  i see you have the internet
twofsonet@graffiti.com (Andrew Brown)                that goes *ping*!"
andrew@crossbar.com       * "information is power -- share the wealth."