c - A program where parent process creates a child process and both parent and child run same program different code -
//same program different code #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() { int pid; pid=fork(); if(pid<0) { printf("\n error "); exit(1); } else if(pid==0) { printf("\n hello child process "); printf("\n pid %d ",getpid()); exit(0); } else { printf("\n hello parent process "); printf("\n actual pid %d \n ",getpid()); exit(1); } } i tried , hope correct .
not satisfied output .
the output :
hello parent process actual pid 4287 ashu@ashu-virtualworld:~/desktop/4thsemester/testprep$ hello child process pid 4288 please me cant understand output , want child process occur first , parent process . , when execution ends control transferred program , return terminal have use ctrl+c , want after execution of program ends control transfers terminal .
the thing parallel processes don't map idea of "happening first"; they're running in parallel.
of course, can change odds having delay in code parent, before print-outs.
not @ sure mean "the control transferred program"; since both processes hit exit() after having printed messages, there should no program left transfer control to.
note don't need use exit() main(), should end plain old return since return type int.
Comments
Post a Comment