I am just experimenting with the fork function . I am having some problem with my code :
HTML Code:
#include <stdio.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <stdlib.h> childprocess(int pid, int pnum); main () { int pid, pid0, pid1, status, FD, tochild[2], toparent[2]; FD = creat("new file.txt", S_IRWXU); if((pid = fork()) < 0) { printf("Could not fork process"); } else { if(pid !=0) [COLOR=Red]printf("\nProcess forked");[/COLOR] } if(pid == 0) childprocess(pid,1); //if(pid != 0) { pid = wait(&status); [COLOR=Red]printf("\nProcess (id %d ) exited ", pid);[/COLOR] //} if((pid1 = fork()) < 0) { printf("Could not fork second process"); }else{ if(pid1 !=0) [COLOR=Red]printf("\nSecond process forked");[/COLOR] } if(pid == 0) childprocess(pid,2); // if(pid != 0) { pid = wait(&status); [COLOR=Red]printf("\nProcess (id %d ) exited " , pid);[/COLOR] // } close(FD); exit(0); } childprocess(int pid, int pnum) { float i = 0; int index = 0; for(index = 0; index < 900; index++) { } //printf("\nPID %d matches index : %d", pid, pnum); exit(0); }
However the output is showing some strange behaviour. THis is the output of the code :
root@unitedroad-desktop:~/Desktop# ./wait3.out
Process forked
Process (id 13402 ) exited Process (id 13402 ) exited
Second process forked
Process (id 13403 ) exited
This seems a little stran as the process id 13402 sems to exit twice and inspite of me using the newline character at the begining of each string that I am printing, the second "exit line" does not start on the new line.
KIndly help me in identifing the reason for this.
Regards,
Dhruwat
