Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将作业放入前台shell实现c_C_Linux_Shell_Gnu - Fatal编程技术网

将作业放入前台shell实现c

将作业放入前台shell实现c,c,linux,shell,gnu,C,Linux,Shell,Gnu,我正在尝试制作自己的linux shell。我已经修复了bg命令,它正在使用此代码: if (strcmp(worte[0], "bg")==0){ pid_t pidnumber; pidnumber=atoi(worte[1]); printf("PID: %d", pidnumber); kill(pidnumber, SIGCONT); return 0; } 但是,fg命令无法正常工作。当我键入“fg 12345”

我正在尝试制作自己的linux shell。我已经修复了bg命令,它正在使用此代码:

if (strcmp(worte[0], "bg")==0){
      pid_t pidnumber;
      pidnumber=atoi(worte[1]);
      printf("PID: %d", pidnumber);
      kill(pidnumber, SIGCONT);
      return 0;
    } 
但是,fg命令无法正常工作。当我键入“fg 12345”(12345是进程id)时,它会将该进程置于前台,但我不能使用Ctrl-Z停止它,也不能使用Ctrl-C。我的代码如下

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;
    }

在代码中,worte[0]表示fg,worte表示进程id(例如:12345)。我怎样才能解决我的问题谢谢你的帮助

尝试忽略该符号

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);