Understanding fork (), sleep (), and process flow

Working with these system calls, but I'm stuck in this code:

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> main() { pid_t pid; switch(pid = fork()) { case -1: printf("fork failed"); break; case 0: //first child printf("\ni'm the first child, my pid is %d", getpid()); fflush(stdout); break; default: //parent sleep(5); /** sleep is generating problems **/ printf("\ni'm the parent process, my pid is %d", getpid()); printf("\ngenerating a new child"); fflush(stdout); switch(pid = fork()) { case -1: printf("fork failed"); break; case 0: //second child printf("\nhere i am, the second child, my pid is %d", getpid()); break; default: //parent wait((int *)0); printf("\nback to parent, my pid is %d", getpid()); } } return 0; } 

The output I get is:

  i'm the first child, my pid is 6203
 i'm the parent process, my pid is 6202
 generating a new child
 back to parent, my pid is 6202
 Process returned 0 (0x0) execution time: 5.004 s
 Press ENTER to continue

 here i am, the second child, my pid is 6204 

What I'm trying to do is a simple fingerprint of these messages controlling synchronization via sleep() . I can’t understand why the program returns before printing the second child message. The default case (one immediately after the second fork) was printed before its child (second) output action, as it ignores its wait() . Therefore, his child was printed after the return process.

I could not understand what the problem was. I noted the sleep () function, because if I replace it with wait((int *)0); , the flux process works the way it was designed (in any case, without any time). At the moment, I'm no longer sure about the process flow or the use of sleep () (pages with people were not useful, too briefly, to be honest).

+4
source share
2 answers

Actually, your call to wait is working. It discovers the end of the first child process and continues afterwards. If you make two consecutive calls to wait (), you will get the correct behavior.

Updated test code:

 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> main() { pid_t pid; int status; switch(pid = fork()) { case -1: printf("fork failed"); break; case 0: //first child printf("\ni'm the first child, my pid is %d", getpid()); fflush(stdout); break; default: //parent sleep(5); /** sleep is generating problems **/ printf("\ni'm the parent process, my pid is %d", getpid()); printf("\ngenerating a new child"); fflush(stdout); switch(pid = fork()) { case -1: printf("fork failed"); break; case 0: //second child printf("\nhere i am, the second child, my pid is %d", getpid()); break; default: //parent pid = wait(&status); printf("\nParent detects process %d was done", pid); pid = wait(&status); printf("\nParent detects process %d was done", pid); printf("\nback to parent, my pid is %d", getpid()); } } return 0; } 

Output:

 i'm the first child, my pid is 30897 i'm the parent process, my pid is 30896 generating a new child here i am, the second child, my pid is 30940 Parent detects process 30897 was done Parent detects process 30940 was done back to parent, my pid is 30896 
+4
source

The human wait page says the following:

The wait () function must suspend the execution of the calling thread until state information is available for one of the completed child processes of the calling process or until a signal is issued, the action of which must either perform the function of capturing the signal or to complete the process. If more than one thread is suspended waiting () or waitpid (), waiting for the completion of the same process, exactly one thread must return the status of the process at the time the target process is completed.

The wait is returned due to the first child.

+1
source

Source: https://habr.com/ru/post/1381688/


All Articles