I am trying to understand fork() using some test program. And I find different behaviors between cout and printf() :
program 1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
printf("Hi , %d\n" , getpid());
fork();
return 0;
}
I get:
Hi , 9375
Hi , 9375
program 2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
cout << "Hi , " <<getpid() << endl;
fork();
return 0;
}
I get:
Hi , 7277
The only difference between two program is the first use printf()
to print the output while second use cout
Can anyone explain it? Thanks