[英]How to synchronize child processes with each other using semaphores?
I have N
number of childs that needs to do some work in a loop while being synchronized with each other at the same time.我有
N
个孩子需要在一个循环中做一些工作,同时彼此同步。 Namely, if a child process is at its i'th iteration, all the other childs should be at i'th iteration.即,如果一个子进程处于其第 i 次迭代,则所有其他子进程都应处于第 i 次迭代。 I need to synchronize them with semaphores but I can't find how to do it.
我需要将它们与信号量同步,但我找不到如何做到这一点。 This is the code I wrote:
这是我写的代码:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/sem.h>
void sem_signal(int semid, int val) {
struct sembuf semaphore;
semaphore.sem_num = 0;
semaphore.sem_op = val;
semaphore.sem_flg = 0;
semop(semid, &semaphore, 1);
}
void sem_wait(int semid, int val) {
struct sembuf semaphore;
semaphore.sem_num = 0;
semaphore.sem_op = (-1 * val);
semaphore.sem_flg = 0;
semop(semid, &semaphore, 1);
}
int main() {
int sem_worker = semget(1, 1, 0700 | IPC_CREAT);
semctl(sem_worker, 0, SETVAL, 0);
int process_index = 0;
int N = 4, pid;
for (process_index = 0; process_index < N; process_index++) {
pid = fork();
if (pid == -1) {
printf("ERROR: cannot fork!\n");
return EXIT_FAILURE;
}
if (pid == 0)
break;
}
if (pid!=0) // parent
pause();
else {
int i = 0;
while (i < 3) {
printf("process %d: i: %d\n", process_index, i);
sem_signal(sem_worker, 1); // increase the semaphore by one
sem_wait(sem_worker, N); // wait for all the other childs
i += 1;
}
}
}
But when I run it, it can't continue after the first iteration.但是当我运行它时,它在第一次迭代后无法继续。
process 0: i: 0
process 1: i: 0
process 3: i: 0
process 2: i: 0
process 0: i: 1
I understand why this happens.我明白为什么会这样。 It's because one of the processes makes the semaphore 0 and continue to next iteration but all the other ones still waits.
这是因为其中一个进程使信号量为 0 并继续下一次迭代,但所有其他进程仍在等待。 So how should I write my code to solve this problem?
那么我应该如何编写代码来解决这个问题呢?
PS: I have taken sem_signal
and sem_wait
functions from somewhere else so I'm not sure how it works but I'm sure that they are working correctly. PS:我从其他地方获取了
sem_signal
和sem_wait
函数,所以我不确定它是如何工作的,但我确信它们工作正常。 For example, if I write sem_wait(my_sem, num_of_children)
in parent to wait all the child processes and increase my_sem
by 1 in childs when they finish, it works.例如,如果我在父进程中写入
sem_wait(my_sem, num_of_children)
以等待所有子进程,并在子进程完成时将my_sem
增加 1,则它可以工作。
As it is mentioned in the comments, you can create a barrier using semaphores and use it to synchronize your processes.正如评论中提到的,您可以使用信号量创建屏障并使用它来同步您的流程。 You need to create your barrier in a shared memory and set a non-zero value for your semaphores'
pshared
parameter to share it among processes:您需要在共享 memory 中创建屏障,并为信号量的
pshared
参数设置一个非零值以在进程之间共享它:
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <signal.h>
#include <unistd.h>
typedef struct {
int n;
int count;
sem_t mutex;
sem_t turnstile;
sem_t turnstile2;
} barrier_t;
void init_barrier(barrier_t *barrier, int n)
{
barrier->n = n;
barrier->count = 0;
sem_init(&barrier->mutex, 1, 1); // second parameter is pshared
sem_init(&barrier->turnstile, 1, 0);
sem_init(&barrier->turnstile2, 1, 0);
}
void phase1(barrier_t *barrier)
{
sem_wait(&barrier->mutex);
if (++barrier->count == barrier->n) {
int i;
for (i = 0; i < barrier->n; i++) {
sem_post(&barrier->turnstile);
}
}
sem_post(&barrier->mutex);
sem_wait(&barrier->turnstile);
}
void phase2(barrier_t *barrier)
{
sem_wait(&barrier->mutex);
if (--barrier->count== 0) {
int i;
for (i = 0; i < barrier->n; i++) {
sem_post(&barrier->turnstile2);
}
}
sem_post(&barrier->mutex);
sem_wait(&barrier->turnstile2);
}
void wait_barrier(barrier_t *barrier)
{
phase1(barrier);
phase2(barrier);
}
int shmid, KEYSHM=123456;
int main(int argc, char const* argv[]) {
barrier_t* barrier;
shmid = shmget(KEYSHM, sizeof(barrier_t), 0700 | IPC_CREAT);
barrier = (barrier_t*) shmat(shmid, 0, 0);
int N = 4;
init_barrier(barrier, N);
shmdt(barrier);
int process_index, pid;
for (process_index = 0; process_index < N; process_index++) {
pid = fork();
if (pid == -1) {
printf("ERROR: cannot fork!\n");
return EXIT_FAILURE;
}
if (pid == 0)
break;
}
if (pid != 0) // parent
pause();
else {
int i = 0;
while (i < 3) {
barrier = (barrier_t*) shmat(shmid, 0, 0);
printf("process %d: i: %d\n", process_index, i);
i += 1;
wait_barrier(barrier);
shmdt(barrier);
}
if (process_index == 3){
kill(getppid(), SIGKILL);
}
}
}
process 0: i: 0 process 1: i: 0 process 2: i: 0 process 3: i: 0 process 2: i: 1 process 3: i: 1 process 0: i: 1 process 1: i: 1 process 3: i: 2 process 2: i: 2 process 0: i: 2 process 1: i: 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.