0

一度に最大 5 つの子のみを実行できるようにしようとしている次のコードがありますが、子が終了したときに子の数を減らす方法がわかりません。

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;
}

改訂版はコメントに基づいて動作するようです

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;
}
4

3 に答える 3

3

関数ファミリーはwait()、子プロセスが終了するまで親を一時停止します (スリープする代わりに終了します)。


いいえ、クリティカル セクションはまったく必要ありません。子と親はメモリを共有しません。デフォルトの場合、必要なのは次のようなものだけです。

    default:
    {   
        children++;  // Last fork() was successful

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

        pid = fork();
    }

(1 に初期化することについて前に言ったことは忘れてください。それが while ループの前にあるはずだったことにchildren気づきませんでした)。children++

于 2010-04-28T12:36:28.570 に答える
2

最初の 5 つの子プロセスを実行したら、子プロセスが終了するのを待ちたいと思うでしょうwait()。その時点で新しい子プロセスを開始できます。

于 2010-04-28T12:36:43.187 に答える
1

wait()親に電話。これは、子の 1 つが終了するまでブロックされます。

于 2010-04-28T12:36:46.680 に答える