主要过程 - > pthread->叉 - >让分叉的过程运行并杀死Main

发布于 2025-01-30 03:39:44 字数 1216 浏览 3 评论 0原文

过程A应启动过程B然后终止。过程B应该继续作为一个独立过程

请对我有一点怜悯 - 我正在尝试教育自己,我想了解更多信息,

int parallel_thread1;
pthread_t thread1;

static void *performer(void) {
    pid_t pid;
    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)            // if success then let the parent terminate
        exit(EXIT_SUCCESS);

    if (setsid() < 0)
        exit(EXIT_FAILURE);

    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)
        exit(EXIT_SUCCESS);

    switch(pid)
    {
    case -1: // Fork() has failed
        perror ("fork");
        break;
    case 0: // This is processed by the child
        system("/home/performerprocess/bin/performer");
        exit(EXIT_FAILURE);
        break;

    default: // This is processed by the parrent
        break;
    }

    return(0);

}

int main(int argc, char *argv[])
{
 parallel_thread1 = pthread_create(&thread1, NULL, &performer, NULL);
 if(parallel_thread1 == 0)
   {
     pthread_detach(updateThread);
     usleep(500000);
    }

 ...

puts("Process should already be terminated :(   I don't want to see this msg");
return 0;
}

请提出一个“干净”的解决方案 - 非常感谢您;)

Process A should start process B and then terminate. Process B should continue as an independent process

please have a little mercy on me - i am trying to educate myself and i like to learn more

int parallel_thread1;
pthread_t thread1;

static void *performer(void) {
    pid_t pid;
    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)            // if success then let the parent terminate
        exit(EXIT_SUCCESS);

    if (setsid() < 0)
        exit(EXIT_FAILURE);

    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)
        exit(EXIT_SUCCESS);

    switch(pid)
    {
    case -1: // Fork() has failed
        perror ("fork");
        break;
    case 0: // This is processed by the child
        system("/home/performerprocess/bin/performer");
        exit(EXIT_FAILURE);
        break;

    default: // This is processed by the parrent
        break;
    }

    return(0);

}

int main(int argc, char *argv[])
{
 parallel_thread1 = pthread_create(&thread1, NULL, &performer, NULL);
 if(parallel_thread1 == 0)
   {
     pthread_detach(updateThread);
     usleep(500000);
    }

 ...

puts("Process should already be terminated :(   I don't want to see this msg");
return 0;
}

please suggest a "clean" solution - thank you very much ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文