I am a little confused write now. I have some code that #include <stdio.h> #incl
ID: 3527204 • Letter: I
Question
I am a little confused write now.
I have some code that
#include <stdio.h>
#include <stdlib.h>
main ()
{
int k;
printf ("Main Process' PID = %d ", getpid());
fflush(stdout);
for (k = 1; k <= 3; k++)
{
fork ();
printf ("k = %d, PPID = %d, pID = %d, I'm Alive! ", k, getppid(), getpid());
fflush(stdout);
}
}
When I run the program to the terminal I get
k =1, PPID =25078, pID =25079. I'm Alive!
k =1, PPID =25034, pID =25078. I'm Alive!
k =2, PPID =25078, pID =25079. I'm Alive!
k =2, PPID =25034, pID =25078. I'm Alive!
k =3, PPID =25078, pID =25082. I'm Alive!
k =3, PPID =25034, pID =25078. I'm Alive!
k =2, PPID =25078, pID =25081. I'm Alive!
k =3, PPID =1, pID =25079. I'm Alive!
[noecr@stu3 proj1]$ k =3, PPID =25079, pID =25083. I'm Alive!
k =2, PPID =25079, pID =25080. I'm Alive!
k =3, PPID =25081, pID =25084. I'm Alive!
k =3, PPID =25080, pID =25085. I'm Alive!
k =3, PPID =1, pID =25081. I'm Alive!
k =3, PPID =1, pID =25080. I'm Alive!
Which is what I expected when I run this code, becuase fork creates a child process that inherits the buffer from teh parent, so when I flush it the printf statement is flushed to the terminal and the I'm alive statement is printed.
What I am consued about is when I redirect the output
when I run ./a.out > file
I get :
Main Process' PID =25209
k =1, PPID =25161, pID =25209. I'm Alive!
k =2, PPID =25161, pID =25209. I'm Alive!
k =3, PPID =25161, pID =25209. I'm Alive!
What is heppening when I redirect the file?