简体   繁体   中英

putting job into foreground shell implemented c

I am trying to make my own linux shell. I have fixed bg command it is working with this code:

if (strcmp(worte[0], "bg")==0){
      pid_t pidnumber;
      pidnumber=atoi(worte[1]);
      printf("PID: %d", pidnumber);
      kill(pidnumber, SIGCONT);
      return 0;
    } 

However, fg command is not working correctly. When I type "fg 12345" (12345 is process id), it puts that process into foreground, but I can not stop it with Ctrl-Z, also I can not use Ctrl-C. My code is following

if (strcmp(worte[0], "fg")==0){
      pid_t pidnumber;
      pidnumber=atoi(worte[1]);
      tcsetpgrp(0, getpgid(pidnumber));
      waitpid(getpgid(pidnumber), NULL, WUNTRACED);
      tcsetpgrp(0, getpgid(shellpid));

      return 0;
    }

In the code worte[0] refers fg and worte 1 refers process id (ex:12345). How can I solve my problem thanks for helping. Please look at the image to see my problem obviously

Try ignoring the SIGTTOU.

pid_t pidnumber;
pidnumber=atoi(worte[1]);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, getpgid(pidnumber));
signal(SIGTTOU, SIG_DFL);
waitpid(getpgid(pidnumber), NULL, WUNTRACED);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, getpgid(shellpid));
signal(SIGTTOU, SIG_DFL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM