c语言代码父进程杀死子进程,c

编程入门 行业动态 更新时间:2024-10-10 07:26:15

c语言代码父<a href=https://www.elefans.com/category/jswz/34/1771450.html style=进程杀死子进程,c"/>

c语言代码父进程杀死子进程,c

我正在尝试编写我的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

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

}

现在我有两个问题:

即使此程序仅杀死前台进程,我也不知道为什么它不影响后台进程,即使将currentPid设置为后台进程的pid。

离开外壳后如何杀死外壳的子进程? 杀死主进程后,子进程仍然存在:

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;

}

更多推荐

c语言代码父进程杀死子进程,c

本文发布于:2024-02-13 19:41:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   语言   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!