C 子进程终止后父进程未完成

发布于 2024-09-24 11:18:31 字数 1098 浏览 7 评论 0原文

我在进行进程分叉练习时遇到问题。我想分叉一个子进程,并在宣布它已分叉后将其挂起,并等待信号终止,之后父进程必须宣布它正在终止,然后退出。

我可以分叉进程并让父进程等待挂起的子进程被信号杀死,但它似乎也会杀死父进程。我尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-10-01 11:18:31

即使假设您修复了代码以便它可以编译(您没有定义 tempPID),也存在问题:

  • 您将子进程设置为进入睡眠状态,直到信号到达。
  • 您将父级设置为等待子级死亡。

因此,您将处于一种状态,两个进程都不会再执行任何操作。

您可能需要父级向子级发送信号:

kill(pid, SIGINT);
  • 尚不清楚您是否需要父级设置信号处理程序。
  • 您可能希望孩子设置一个信号处理程序。
  • 您可能不希望子级中出现无限循环。
  • 哦,void main() 不正确 - int main()int main(void)int main(int argc , char **argv)main() 的批准声明。
  • 如果从 main() 返回值 (0),则更加整洁。 C99 标准确实允许您删除 main() 的末尾,并将其视为返回零,但前提是该函数被正确声明为返回 int
  • POSIX 中 wait() 及其相关项的标头为

而且,因为我是个傻瓜,所以这里的代码可以编译,甚至可以做你想做的事情:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>            /* getpid() */ 
#include <stdlib.h>
#include <sys/wait.h>

void catchInt(int signum)
{
    printf("Child's PID is %d\n", (int)getpid());
    printf("My sincerest apologies, master\n");
    exit(1);
}

int main()
{
    pid_t  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);
        pause();
    }    
    else /* parent */
    {
        sleep(1);
        kill(pid, SIGINT);
        wait(NULL);
        printf("You're welcome\n");
    }
    return(0);
}

Even assuming you fix the code so it compiles (you've not defined tempPID), there are problems:

  • You set the child to go to sleep until a signal arrives.
  • You set the parent to wait until the child dies.

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:

kill(pid, SIGINT);
  • It is not clear that you need the parent to set a signal handler.
  • You probably want the child to set a signal handler.
  • You probably don't want the infinite loop in the child.
  • Oh, and void main() is incorrect - int main() or int main(void) or int main(int argc, char **argv) are the approved declarations for main().
  • And it is tidier if you return a value (0) from main(). The C99 standard does permit you to drop off the end of main() and will treat that as returning zero, but only if the function is properly declared as returning an int.
  • The header for 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:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>            /* getpid() */ 
#include <stdlib.h>
#include <sys/wait.h>

void catchInt(int signum)
{
    printf("Child's PID is %d\n", (int)getpid());
    printf("My sincerest apologies, master\n");
    exit(1);
}

int main()
{
    pid_t  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);
        pause();
    }    
    else /* parent */
    {
        sleep(1);
        kill(pid, SIGINT);
        wait(NULL);
        printf("You're welcome\n");
    }
    return(0);
}
恬淡成诗 2024-10-01 11:18:31

刚刚弄清楚我做错了什么,我应该意识到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文