将作业放入前台shell实现c
我正在尝试制作自己的linux shell。我已经修复了bg命令,它正在使用此代码:将作业放入前台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”
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);