首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何同时派生父进程的最多5个子进程?

如何同时派生父进程的最多5个子进程?
EN

Stack Overflow用户
提问于 2010-04-28 20:29:38
回答 3查看 4.3K关注 0票数 0

我有下面的代码,我试图一次只允许最多5个孩子运行,但我不知道当孩子退出时如何减少孩子的数量。

代码语言:javascript
复制
struct {
   char *s1;
   char *s2;
} s[] = {
  {"one", "oneB"},
  {"two", "twoB"},
  {"three", "thr4eeB"},
  {"asdf", "3th43reeB"},
  {"asdfasdf", "thr33eeB"},
  {"asdfasdfasdf", "thdfdreeB"},
  {"af3c3", "thrasdfeeB"},
  {"fec33", "threfdeB"},
  {NULL, NULL}
};

int main(int argc, char *argv[])
{
int i, im5, children = 0;
int pid = fork();
for (i = 0; s[i].s2; i++)
{
    im5 = 0;
    switch (pid)
    {
        case -1:
        {
            printf("Error\n");
            exit(255);
        }
       case 0:
       {
            printf("%s -> %s\n", s[i].s1, s[i].s2);
            if (i==5) im5 = 1;
            printf("%d\n", im5);
            sleep(i);
            exit(1);
        }
        default:
        {   // Here is where I need to sleep the parent until chilren < 5 
            // so where do i decrement children so that it gets modified in the parent process?
            while(children > 5)
                sleep(1);
            children++;
            pid = fork();
        }
    }
}
return 1;
}

修订的版本似乎是基于评论而工作的

代码语言:javascript
复制
struct {
   char *s1;
   char *s2;
} s[] = {
  {"one", "oneB"},
  {"two", "twoB"},
  {"three", "thr4eeB"},
  {"asdf", "3th43reeB"},
  {"asdfasdf", "thr33eeB"},
  {"asdfasdfasdf", "thdfdreeB"},
  {"af3c3", "thrasdfeeB"},
  {"fec33", "threfdeB"},
  {NULL, NULL}
};

pthread_mutex_t children_count_lock;
int children = 0;

int main(int argc, char *argv[])
{
int i, im5;
int pid = fork();
for (i = 0; s[i].s2; i++)
{
    im5 = 0;
    switch (pid)
    {
        case -1:
        {
            printf("Error\n");
            exit(255);
        }
       case 0:
       {
            printf("%s -> %s\n", s[i].s1, s[i].s2);
            if (i==5) im5 = 1;
            printf("%d\n", im5);
            sleep(i);

            pthread_mutex_lock(&children_count_lock);
            children = children - 1;
            if (children < 0) children = 0;
            pthread_mutex_unlock(&children_count_lock);

            exit(1);
        }
        default:
        {   
            if (children > 4)
                wait();

            pthread_mutex_lock(&children_count_lock);
            children++;
            pthread_mutex_unlock(&children_count_lock);

            pid = fork();
        }
    }
}
return 1;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-04-28 20:36:29

wait()系列函数将挂起父进程,直到子进程退出(这样做而不是休眠)。

不,你根本不需要临界区--子节点和父节点不共享内存。在默认情况下,您所需要的全部内容如下所示:

代码语言:javascript
复制
    default:
    {   
        children++;  // Last fork() was successful

        while (children >= 5)
        {
            int status;
            // Wait for one child to exit
            if (wait(&status) == 0)
            {
                children--;
            }
        }

        pid = fork();
    }

(忘记我之前说的关于将children初始化为1的内容,我没有注意到children++应该在while循环之前)。

票数 3
EN

Stack Overflow用户

发布于 2010-04-28 20:36:43

一旦您运行了最初的5个子进程,您可能希望使用wait()来等待子进程完成,然后您就可以启动一个新的子进程。

票数 2
EN

Stack Overflow用户

发布于 2010-04-28 20:36:47

在父级中调用wait()。这将一直阻塞,直到其中一个子进程退出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2729367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档