我正在尝试编写我的shell克隆。 现在,我正在将SIGINT发送到前台进程,这是我的工作方式:
void performPipeCmd(char *cmd[maxCmdSize][maxArgSize], int waitOption, int numOfCmds){
waitpid(-1, NULL, WNOHANG);
int fd[2];
int status,
fdIn = 0;
pid_t childpid;
if(pipe(fd) == -1) {
perror("Pipe failed");
exit(1);
}
for(int i = 0; i < numOfCmds; i++){
status;
switch ((childpid = fork())){
case -1:
perror("fork");
break;
case 0:
dup2(fdIn, 0);
if (i+1<numOfCmds)
dup2(fd[1], 1);
close(fd[0]);
execvp(cmd[i][0], cmd[i]);
printf("execvp failure\n");
exit(EXIT_FAILURE);
break;
default:
currentPid = childpid; //setting current childpid
setpgid(childpid, childpid);
waitpid(childpid, &status, waitOption);
close(fd[1]);
fdIn = fd[0];
break;
}
}
}
这是我的信号处理程序:
void sigIntHandler(int dummy){
killpg(currentPid,SIGINT);
}
现在我有两个问题:
Shell正在运行:
10061 13511 13511 13511 ? -1 Ssl 1000 0:03 \_ /usr/lib/gnome-terminal/gnome-terminal-server
13511 13520 13520 13520 pts/0 14242 Ss 1000 0:00 \_ bash
13520 14242 14242 13520 pts/0 14242 S+ 1000 0:00 | \_ ./a.out
14242 14243 14243 13520 pts/0 14242 S 1000 0:00 | \_ sleep 100000
离开外壳后:
10061 13511 13511 13511 ? -1 Ssl 1000 0:04 \_ /usr/lib/gnome-terminal/gnome-terminal-server
13511 13520 13520 13520 pts/0 13520 Ss+ 1000 0:00 | \_ bash
13511 13966 13966 13966 pts/1 14256 Ss 1000 0:00 | \_ bash
13966 14256 14256 13966 pts/1 14256 R+ 1000 0:00 | \_ ps ajfx
10061 14243 14243 13520 pts/0 13520 S 1000 0:00 \_ sleep 100000
我试图这样做,但它不起作用:
if(checkIfShouldStop(input, result)) {
killpg(getgid(),SIGINT);
break;
}