After suspending child process with SIGTSTP, shell not responding

后端 未结 4 1656
误落风尘
误落风尘 2020-12-20 19:23

I\'m coding a basic shell in C, and I\'m working on suspending a child process right now.

I think my signal handler is correct, and my child process is suspending, b

4条回答
  •  有刺的猬
    2020-12-20 20:28

    It's an old question but still I think I found an answer.
    You didn't write your parent's code but I'm assuming its looks something like:

    int main(){ 
         pid_t pid = fork();
         if(pid == 0){ //child process
            //call some program
         else //parent process
            wait(&status); //or waitpid(pid, &status, 0)
            //continue with the program
    }
    

    the problem is with the wait() or waitpid(), it's look like if you run your program on OS like Ubuntu after using Ctrl+Z your child process is getting the SIGTSTP but the wait() function in the parent process is still waiting!

    The right way of doing that is to replace the wait() in the parent with pause(), and make another handler that catch SIGCHLD. For example:

    void sigHandler(int signum){
         switch(signum){
            case SIGCHLD:
                 // note that the last argument is important for the wait to work
                 waitpid(-1, &status, WNOHANG);
                 break;
         }
    }
    

    In this case after the child process receive Ctrl+Z the parent process also receive SIGCHLD and the pause() return.

提交回复
热议问题