Fork and wait - how to wait for the completion of all the grandchildren

I am working on an assignment to create a simple shell, and I am trying to add a few functions that are not yet needed, but I ran into a problem with channels.

After my team is parsed, I create a process to execute them. This process is a routine that will execute the command if there is only one left, otherwise it will be fork. The parent will execute the first command, the child process will process the rest. The pipes are configured and working correctly.

My main process then calls wait() and then issues a prompt. When I execute a command like ls -la | cat ls -la | cat , the request is displayed before exiting cat.

I tried calling wait() once for every command that needs to be executed, but the first call works, and all subsequent calls return ECHILD .

How to make the main thread wait until all children, including children of children, come out?

+4
source share
2 answers

You can not. Or make your child process wait for their children and do not exit until everyone has been waiting or unlocking all children from the same process.

+7
source

See this answer as wait() for child processes: How to wait until all child processes called by fork () are complete?

It is impossible to wait for a grandson; You need to implement the waiting logic in every process. Thus, each child will leave only after all the children leave (and this will include all the grandchildren in return).

+4
source

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


All Articles