意外的输出信号量运行 [英] Unexpected Output Running Semaphore
问题描述
我想编写一个程序,派生另一个进程,并保持同步,这样,直到第二个进程已经完成它(I-1)次迭代的第一个进程无法启动其第i个迭代。只用两个信号灯这可能吗?这是我到目前为止有:
的#include<&stdio.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&semaphore.h GT;sem_t SEMA,SEMB,SEM,米;INT主要(无效)
{
INT I; 将为pid_t child_a,child_b,PID2,PID3;
sem_init(安培; SEMA,0,1);
sem_init(安培; SEMB,0,0);
sem_init(安培峰; m,0,0); child_a =叉();
//等待(); 如果(child_a == 0){
//诠释J; PID2 = GETPID();
对于(I = 0; I&小于5)
{
sem_wait(安培; SEMA);
// sem_wait(安培;米);
的printf(child1数:%d \\ n,I);
我++;
//输出(PID1数:%d \\ n,PID2);
//输出(-------------------------- \\ n);
睡眠(3);
// sem_post(安培;米);sem_post(安培;森布);
}
}
其他{ child_b =叉();
//等待();
如果(child_b == 0){ PID3 = GETPID();
对于(I = 0; I&小于5)
{
sem_wait(安培;森布);
// sem_wait(安培;米);
的printf(的child2数:%d \\ n,I);
我++;
//输出(PID2数:%d \\ n,PID3);
//输出(-------------------------- \\ n);
睡眠(5);
// sem_post(安培;米);
sem_post(安培; SEMA); } }
} 出口(0);
返回0;
}
我期望的输出是:
child1:0
的child2:0
child1:1
的child2:1
child1:2
的child2:2
child1:3
的child2:3
child1:4
的child2:4
child1:5
的child2:5
child1:6
的child2:6
child1:7
的child2:7
child1:8
的child2:8
child1:9
的child2:9
但我得到的只是一个孩子:
child1:0
您可能需要三信号灯
。
SEM1
sem2
sem3
处理1:
如果(FirstRun){
初始化信号灯
等待sem2
}后SEM1
等待sem3
过程2:
如果(FirstRun){
初始化信号灯
后sem2
}等待SEM1
后sem3
注意:在初始化信号量
您必须将信号量映射到共享内存
以便它是由两个进程访问。也不要他们初始化一些默认值说 0,0,0
。
I'm trying to write a program that forks another process and stays in sync so that the first process doesn't start its (i)th iteration until the second process has finished its (i-1)th iteration. Is this possible using only two semaphores? This is what I have so far:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <semaphore.h>
sem_t semA, semB,sem,m;
int main(void)
{
int i;
pid_t child_a, child_b,pid2,pid3;
sem_init(&semA, 0, 1);
sem_init(&semB, 0, 0);
sem_init(&m, 0, 0);
child_a = fork();
//wait();
if (child_a == 0) {
// int j;
pid2 =getpid();
for (i = 0; i < 5; )
{
sem_wait(&semA);
//sem_wait(&m);
printf("child1: %d\n", i);
i++;
//printf("pid1: %d\n", pid2);
//printf("--------------------------\n");
sleep(3);
//sem_post(&m);
sem_post(&semB);
}
}
else {
child_b = fork();
//wait();
if (child_b == 0) {
pid3 =getpid();
for (i = 0; i < 5;)
{
sem_wait(&semB);
//sem_wait(&m);
printf("child2: %d\n", i);
i++;
//printf("pid2: %d\n", pid3);
//printf("--------------------------\n");
sleep(5);
//sem_post(&m);
sem_post(&semA);
}
}
}
exit(0);
return 0;
}
The output I expect is:
child1: 0
child2: 0
child1: 1
child2: 1
child1: 2
child2: 2
child1: 3
child2: 3
child1: 4
child2: 4
child1: 5
child2: 5
child1: 6
child2: 6
child1: 7
child2: 7
child1: 8
child2: 8
child1: 9
child2: 9
but I get just one child:
child1: 0
You may need three semaphores
.
sem1 sem2 sem3
Process1:
if(FirstRun){
Initialize the semaphores
wait for sem2
}
post sem1
wait for sem3
Process2:
if(FirstRun){
Initialize the semaphores
post sem2
}
wait for sem1
post sem3
Note: In Initialize the semaphores
you have to map the semaphores to a shared memory
so that it is accessible by both the processes. Also Do Initialize them to some default value say 0, 0, 0
.
这篇关于意外的输出信号量运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!