【问题标题】:Fork and exec several children in linux在 linux 中 fork 和 exec 几个孩子
【发布时间】:2018-11-28 19:42:50
【问题描述】:

我想从另一个进程中分叉和执行多个进程。 我的父代码是

/*Daddy.c*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
    int main(void)
{
        int status;
        char *nChild;

        for (int i=0; i<3;i++){
            int pid = fork();
            if (pid == 0)
            {
                sprintf(nChild, "%d", i);
                printf("%d\n", i);
                char *const arguments[]={nChild, NULL};
                fflush(NULL);
                execv("child",arguments);
                printf("\nNo , you can't print!\n");
            }else if (pid == -1){
                    printf("%d\n", getpid());
                    exit(0);
            }
        }
        wait(&status);
        printf("Dad %d went out!\n", getpid());
        exit(0);
}

我的子进程是

    /*child.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int args, char **argv){
        if( args !=2){
                printf("Child going away!\n");
                exit(1);
        }
        printf("Child %s: %d going away stylishly!\n", argv[1], getpid());

        exit(0);
}

当我不创建三个叉子,而是一个时,我知道如何创建孩子,做一些工作并从孩子和父母中退出。但是,在这种情况下,有几个孩子似乎孩子永远不会执行。 由于wait(&amp;status) 这行,我确实希望当第一个孩子退出时,父母也退出,但是,任何孩子都会打印任何消息。 Some relevant previous questions 没有帮助。

【问题讨论】:

  • 您能更具体地解决您的问题吗?您的代码有几个问题,但更糟糕的可能是 char *nChild;这是在没有分配的情况下编写并由孩子们共享的
  • 使用waitpid 代替等待
  • 顺便说一句,我看到你的一些其他帖子有很好的答案。 如果回答有帮助,请点击左侧的勾号图标接受
  • 否 @snr ,其他帖子没有解决问题,因为它们没有在父进程中使用 for 循环来创建子进程。当我在循环中使用循环时,我的代码问题就出现了。
  • 问题是没有分配*nChild。谢谢@OznOg

标签: c linux gcc


【解决方案1】:

您需要让父进程等待所有子进程完成。如果不是,则假设等待的 1 个孩子已完成,然后父母退出。其他2个孩子呢?他们成为孤儿,因为他们的父母不等他们。

pid_t wpid;
int status = 0;
.
.
while ((wpid = wait(&status)) > 0); // the parent waits for all the child processes 

【讨论】:

    【解决方案2】:

    这段代码完成了这项工作

    /* daddy.c */
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <string.h>
    int main(void)
    {
            int status=0;
            char nChild[16];
            pid_t wpid;
            for (int i=0; i<3;i++){
                sprintf(nChild, "%d", i);
                int pid = fork();
                if (pid == 0)
                {
                    printf("%s\n", nChild);
                    char *const arguments[]={"child", nChild, NULL};
                    fflush(NULL);
                    execv("child",arguments);
                    printf("\nNo , you can't print!\n");
                }else if (pid == -1){
                        printf("%d\n", getpid());
                        exit(0);
                }
            }
            while ((wpid=wait(&status)) >0);
            printf("Dad %d went out!\n", getpid());
            exit(0);
    }
    

    正如@OnzOg 在问题的cmets 中所说,nChild 的分配是主要问题。同样execv 需要两次传递子名称,一次作为参数。 最后,为了改进代码,父进程需要等待所有进程完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      相关资源
      最近更新 更多