C 子进程终止后父进程未完成
我在进行进程分叉练习时遇到问题。我想分叉一个子进程,并在宣布它已分叉后将其挂起,并等待信号终止,之后父进程必须宣布它正在终止,然后退出。
我可以分叉进程并让父进程等待挂起的子进程被信号杀死,但它似乎也会杀死父进程。我尝试通过 PID 专门杀死子进程,但没有成功。
感谢您的帮助!
代码:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
void catchInt (int signum)
{
printf("\nMy sincerest apologies, master\n");
/*kill(0, SIGINT);*/
exit(0);
}
void ignoreInt (int signum)
{
wait(NULL);
}
int main () {
pid_t pid;
/* fork process */
pid = fork();
if (pid < 0) /* error handler */
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) /* child */
{
printf("Child reporting in\n");
signal(SIGINT, catchInt);
for ( ;; )
pause();
}
else /* parent */
{
/* parent will wait for the child to complete */
signal(SIGINT, ignoreInt);
wait(NULL);
printf("You're welcome\n");
exit(0);
}
}
I'm having trouble with a process forking exercise. I want to fork a child process and have it hang after announcing it has been forked, and wait for a signal to terminate, after which the parent process must announce it is terminating and then exit.
I can get the processes forked and have the parent wait for the hanging child to be killed by the signal, but it seems to kill the parent as well. I tried killing the child process specifically by its PID, but with no success.
Thanks for any help!
Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
void catchInt (int signum)
{
printf("\nMy sincerest apologies, master\n");
/*kill(0, SIGINT);*/
exit(0);
}
void ignoreInt (int signum)
{
wait(NULL);
}
int main () {
pid_t pid;
/* fork process */
pid = fork();
if (pid < 0) /* error handler */
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) /* child */
{
printf("Child reporting in\n");
signal(SIGINT, catchInt);
for ( ;; )
pause();
}
else /* parent */
{
/* parent will wait for the child to complete */
signal(SIGINT, ignoreInt);
wait(NULL);
printf("You're welcome\n");
exit(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使假设您修复了代码以便它可以编译(您没有定义 tempPID),也存在问题:
因此,您将处于一种状态,两个进程都不会再执行任何操作。
您可能需要父级向子级发送信号:
void main()
不正确 -int main()
或int main(void)
或int main(int argc , char **argv)
是main()
的批准声明。main()
返回值 (0),则更加整洁。 C99 标准确实允许您删除main()
的末尾,并将其视为返回零,但前提是该函数被正确声明为返回int
。wait()
及其相关项的标头为
。而且,因为我是个傻瓜,所以这里的代码可以编译,甚至可以做你想做的事情:
Even assuming you fix the code so it compiles (you've not defined tempPID), there are problems:
So, you have a state where neither process is going to do anything more.
You probably need the parent to send a signal to the child:
void main()
is incorrect -int main()
orint main(void)
orint main(int argc, char **argv)
are the approved declarations formain()
.main()
. The C99 standard does permit you to drop off the end ofmain()
and will treat that as returning zero, but only if the function is properly declared as returning anint
.wait()
and relatives in POSIX is<sys/wait.h>
.And, because I'm a sucker, here's code that compiles and might even do what you want:
刚刚弄清楚我做错了什么,我应该意识到 SIGINT 被发送到每个进程,因此父进程只是被发送了一个未处理的 SIGINT,导致它退出。感谢您的所有帮助(我对草率的编码表示歉意,我真的不应该等到程序完成才清理它),代码已在上面编辑并按预期工作。
再次感谢。
Just figured out what I was doing wrong, I should have realized SIGINT is sent to every process, and so the parent was simply being sent an unhandled SIGINT, causing it to exit. Thanks for all the help (my apologies on the sloppy coding, I really shouldn't wait until the program is completed to clean that up), the code's been edited above and works as intended.
Thanks again.