3

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

  • Related: [How do I get tcsetpgrp() to work in C](http://stackoverflow.com/questions/5341220/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 Dec 09 '15 at 23:52
  • 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 Dec 10 '15 at 01:18
  • 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 Dec 10 '15 at 09:57
  • I have used sigprocmask to ignore SIGTTOU temporarily, but when I press the Ctrl-Z it writes just ^Z not stop the current process, but Ctrl-C works well. For example when I write >>cat then press the Ctrl-C it terminates and turns my own shell. I am really confused – Ali Can Üstünel Dec 10 '15 at 23:59
  • 1
    Before you added the sigprocmask call, did Control-Z work? – Mark Plotnick Dec 11 '15 at 00:34
  • Not actually it stops the my own shell turns back the linux shell, but I need to stop process and turn back to my own shell to take next command – Ali Can Üstünel Dec 11 '15 at 00:51
  • 1
    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 Dec 11 '15 at 22:19

1 Answers1

1

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

Armali
  • 18,255
  • 14
  • 57
  • 171