【发布时间】:2012-08-15 16:16:46
【问题描述】:
在模拟读写器问题的 C 程序中,我使用 fork() 创建了多个子进程,每个子进程称为 execlp(),并在 xterm 窗口中运行另一个程序(读取器或写入器) .
当我结束main() 时,那些在xterm 中运行的孩子还活着。我如何也终止它们?
下面的代码示例-
main() {
while(1) {
scanf(choice);
switch(choice) {
case 1:
reader()
break;
case 2:
writer();
break;
default:
kill(getpgid(getpid()), SIGTERM); // killing the group id
return 0;
}
}
reader() {
/*
some semaphore manipulation
*/
execlp("xterm", "xterm", "-e", "./read", NULL);
/*
some semaphore manipulation
*/
return 0;
}
writer() {
/*
some semaphore manipulation
*/
execlp("xterm", "xterm", "-e", "./write", NULL);
/*
some semaphore manipulation
*/
return 0;
}
【问题讨论】:
标签: c kill-process