3
votes

I am trying make my own shell for my school homework, after the sucessfull fork call, I want to put pid which comes from fork() function into foreground and then I want to put my own shell into background. Then after the waitpid function, I need to my own shell into foreground again. For this I think like this:

        if(tcsetpgrp(0, getpgid(pid))!=0)
        perror("Foreground error: ");

        waitpid(pid, NULL, 0);

        if(tcsetpgrp(0, getpgid(shellpid))!=0)
        perror("Foreground error: ");}

But after new process finishes, the linux shell stops my own shell. For instance, ls command is the new process in the picture. Please look at here: for terminal screen shot

1
Related: How do I get tcsetpgrp() to work in C. Your shell will stop when it's the background process and tries to change the tty's pgrp. Fix is to (temporarily) ignore SIGTTOU. - Mark Plotnick
Thank you for your help adding "signal(SIGTTOU, SIG_IGN);" before tcsetpgrp solved my problem. However, I need to use Ctrl+Z command to stop process, so I can't do that when I ignore it. - Ali Can Üstünel
Control-Z sends a SIGTSTP signal to the foreground process group. It has a similar effect as SIGTTOU, but they're different signals. Is adding the line of code that ignores SIGTTOU preventing Control-Z from working correctly? - Mark Plotnick
Before you added the sigprocmask call, did Control-Z work? - Mark Plotnick
Can you ask that as a new question? If you can, please include all the code in the parent and child that does any setpgid, tcsetgrp, fork, waitpid, and exec* calls. - Mark Plotnick

1 Answers

1
votes

adding "signal(SIGTTOU, SIG_IGN);" before tcsetpgrp solved my problem. – Ali Can Üstünel