【发布时间】:2017-12-05 22:15:47
【问题描述】:
Linux,C。
我必须同步从一个父级派生的 N 个进程。
每个进程都需要等到所有其他进程都完成了一项任务才能同时移动到第二个任务。
我正在考虑实现 信号量,但我真的不知道哪种类型以及如何以有效的方式实现它。
任何人都可以告诉我这样做的方法吗?
有人有解决这个问题的办法吗?
这是我想要实现的一些伪代码:
for (int i = 0; i < init_people; ++i) {
switch (pids[i] = fork()) {
case -1:
exit(1);
break;
case 0:
switch (i % 2) {
case 0:
/*Here is where one of the processes starts and
does his task*/
execve("./A",args,NULL);
/*Here is where it stops and waits for all the
other processes to complete the task*/
break;
case 1:
/*Here is where one of the processes starts and
does his task*/
execve("./B",args,NULL);
break;
/*Here is where it stops and waits for all the
other processes to complete the task*/
default:
break;
}
exit(0);
break;
default:
waitpid(pids[i], &returnStatus, 0);
break;
}
}
【问题讨论】:
-
... Linux 已经以
pthread_barrier_t及其关联的pthread_barrier_init()、*_destroy()和*_wait()函数的形式实现了屏障。还是需要您自己实现? -
您不需要信号量,您需要在共享内存中分配
pthread_barrier_t,并将其pshared属性设置为PTHREAD_PROCESS_SHARED。 -
@John Bollinger 我不需要实现任何东西
-
我该如何实现@EOF
-
@BryanShtjefni 你的两个 cmets 似乎是矛盾的。你能澄清一下吗?