How to properly wait for foreground/background processes in my own shell in C?

后端 未结 4 1322
感动是毒
感动是毒 2021-02-06 06:16

In this previous question I posted most of my own shell code. My next step is to implement foreground and background process execution and properly wait for them to terminate so

4条回答
  •  遥遥无期
    2021-02-06 07:01

    Instead of using a global variable, I thought of a different solution:

    if(!background) {
        signal(SIGCHLD, NULL);
    
        waitpid(pid, NULL, 0);
    
        signal(SIGCHLD, childSignalHandler);
    }
    

    If I'm running a foreground process "delete" the handler for SIGCHLD so it doesn't get called. Then, after waitpid(), set the handler again. This way, only the background processes will be handled.

    Do you think there's anything wrong with this solution?

提交回复
热议问题