使用信号灯集完成三个进程的同步,A进程输出字符A,B进程输出字符B,C进程输出字符C,要求输出结果为ABCABCABCABCABC...
#include<myhead.h>
#include"sem.h"
int main(int argc, const char *argv[])
{
pid_t pid1,pid2;
int semid=create_sem(3);
pid1=fork();
if(pid1 ==0)
{
while(1)
{
P(semid,0);
printf("A");
fflush(stdout);
V(semid,1);
}
}
else if(pid1>0)
{
pid2=fork();
if(pid2 ==0)
{
while(1)
{
P(semid,1);
printf("B");
fflush(stdout);
V(semid,2);
}
}
else if(pid2>0)
{
while(1)
{
P(semid,2);
printf("C");
fflush(stdout);
V(semid,0);
}
}
else
{
perror("gg");
return -1;
}
}
else
{
perror("g");
return -1;
}
delete_sem(semid);
return 0;
}