In <co**********@domitilla.aioe.org> Giulio <fr*************@email.it.invalid> writes:
I made for experiment this simple program:
#include <stdlib.h>
int main (){
int i;
if (fork() != 0){
for (i = 0; i < 100000; i++) printf("|");
} else {
for (i = 0; i < 100000; i++) printf("-");
}
exit (0);
}
I tried to count how many '|' and how many '-' in my screen.
they where almost always blocks of 1024 '|' and 1024 '-'
sometimes longer blocks o shorter ones.. but most frequently blocks of
1024 chars...
do someone has an explanation for this fact??
Your code is broken, so there is little point in trying to explain its
behaviour. Try the following fixed version and play with the setvbuf
call (replace _IOFBF by _IONBF or comment out the setvbuf call) and see
if it makes any difference.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#define SIZE 100000
char buff[SIZE];
int main()
{
int i;
setvbuf(stdout, buff, _IOFBF, sizeof buff);
if (fork() != 0) {
for (i = 0; i < SIZE; i++) putchar('p');
wait(NULL);
printf("\nBUFSIZ = %d\n", BUFSIZ);
}
else for (i = 0; i < SIZE; i++) putchar('c');
return 0;
}
As such, my program is quite likely to generate two compact blocks of
output (one generated by the parent, the other by the child) and then
the final line printed by the parent. But even this is not guaranteed.
Using the default buffering or disabling the stdio buffering is likely
to change the output pattern.
BTW, the issue is semi-topical, because it involves the buffering
performed by the standard C library. The off topic parts are the ones
related to creating a second process and waiting for its termination,
but without them the topical part could not be explored.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
Da*****@ifh.de
Currently looking for a job in the European Union