A "clone" problem

include<stdio.h>
#include<sched.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int fn(void *a)
{
int *p;
printf(“Value of a is = %d”,p);
printf("
");
return 0;
}
int main()
{
int i,a=125;
pid_t child;
printf("
This is parent with id : %d",getppid());
i=(int
)malloc(100
sizeof(int))+100;
child=clone(&fn,i,CLONE_PARENT,&a);
printf("
Got the child id as : %d",child);
return 0;
}

In the above program, In function “fun()” the first printf statement (i.e, printing the value of a) is not printing anything in the screen. If i give the second printf(simply a new line) i’m getting the output.

  1. can anyone explain me why?

  2. My friends are telling that I should not use “printf”, if I create a process by using “clone()”. They are telling me to use “write()” instead.
    If they are telling the correct answer, also explain me why I should not use printf in clone?

printf is buffered I/O. You should flush all buffers using fflush() prior to forking processes because the child will inherit any buffers in that state. write() is unbuffered and will bypass the problem, but avoiding buffered I/O without understanding how to use it properly is just superstition.

Thanks ken… you gave exactly the answer which i’m looking for… I will tell this answer to my friends…