Fork basic question
Hi all
I hope this is the proper sub forum to ask this . In the following code a process is created with father and child . 19 pid_t pID = fork(); 20 if (pID == 0) // child 21 { 22 // Code only executed by child process 23 24 sIdentifier = "Child Process: "; 25 globalVariable++; 26 iStackVariable++; 27 } 28 else if (pID < 0) // failed to fork 29 { 30 cerr << "Failed to fork" << endl; 31 exit(1); 32 // Throw exception 33 } 34 else // parent 35 { 36 // Code only executed by parent process 37 What is hard to understand is the fact that the fork() function in line 19 can and will return only ONE value : 0 , <0 or >0 in case of 0 the child code will be done in case of >0 the father code else an error message . So how father and child can come to execution from one single fork() function? Thanks Elico |
fork means you start a new process. The new process is [almost] exactly the same as the original one. To find out which one is the current process you can use the return value of fork (0 means it is the child process, >0 means the parent process and the returned value is the pid of the child, <0 means error and also no child was created)
|
"The new process is [almost] exactly the same as the original one"
Which is the original process ? Elico |
|
All times are GMT -5. The time now is 12:24 AM. |