Hi,
The open group specification says that write() is atomic as long
as the number of bytes is not larger than PIPE_BUF. In the following
program, sometimes only one process successfully writes to the file.
I thought that fprintf also uses write() underneath, so the file
should contains strings from both processes. Any insight?
#include
#include
#include
int main() {
int i;
FILE *f;
printf("bufsiz=%d pipe_buf=%d\n", BUFSIZ, PIPE_BUF);
f = fopen( "test.txt" , "w" );
/* f = stdout; */
fprintf( f, "Parent is process %d\n", getpid() );
assert(fork() >= 0);
for (i=1; i<4; i++)
fprintf( f, " %8d: %d\n", getpid(), i );
fclose( f );
return 0;
}
Here are some results, the first correctly contains strings from
both processes, but the second only contains strings from one
process:
cincai@verdimar:/tmp> ./a.out; cat haha.txt
bufsiz=8192 pipe_buf=4096
Parent is process 25831
25832: 1
25832: 2
25832: 3
bufsiz=8192 pipe_buf=4096
Parent is process 25831
25831: 1
25831: 2
25831: 3
cincai@verdimar:/tmp> ./a.out ;cat haha.txt
bufsiz=8192 pipe_buf=4096
Parent is process 25836
25837: 1
25837: 2
25837: 3
--
Bis zu 70% Ihrer Onlinekosten sparen: GMX SmartSurfer!
Kostenlos downloaden: http://www.gmx.net/de/go/smartsurfer