Hi all , this may be a weird question , but please I need your help !!!
how this C program work ? :confused: :confused:
Code:int main() {
int p1 , p2;
p1 = fork();
p2 = fork();
printf("end\n");
}
Printable View
Hi all , this may be a weird question , but please I need your help !!!
how this C program work ? :confused: :confused:
Code:int main() {
int p1 , p2;
p1 = fork();
p2 = fork();
printf("end\n");
}
If you instrument the code to see what is going on...
Code:output:
Starting process: 7033
end of process: 7033
I'm a brand new process: 7034 My daddy was : 7033
I'm a brand new process: 7035 My daddy was : 7033
end of process: 7035
end of process: 7034
I'm a brand new process: 7036 My daddy was : 7034
end of process: 7036
Code:#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void whoami(pid_t *); /* show the process id's of the parent and the current process */
int main(int argc , char *argv[]) {
int p1 , p2;
pid_t pids[10]={0};
pids[0]=getpid(); /* pid of parent process */
printf("Starting process: %5d\n",pids[0]);
whoami(pids);
p1 = fork();
whoami(pids);
p2 = fork();
whoami(pids);
printf("end of process:%5d\n",getpid());
return 0;
}
void whoami(pid_t * pids){ /* this looks to see if the process is a new one */
pid_t this_process;
int found=0;
pid_t *pidptr;
this_process=getpid();
for(found=0,this_process=getpid(),pidptr=pids;*pidptr && !found;pidptr++)
if(this_process==*pidptr) found=1;
if(!found){
*pidptr=this_process;
printf("I'm a brand new process: %5d ",this_process);
printf(" My daddy was :%5d\n", getppid());
}
}
thank you very much , that was a great help :) :)