如何模拟生产者消费者程序?
如何模拟生产者消费者程序?
#include "unistd.h"#include "sys/types.h"
#include "sys/wait.h"
#include "sys/shm.h"
#include "sys/ipc.h"
#include "sys/sem.h"
#include "stdio.h"
#include "stdlib.h"
#define PERM IPC_CREAT|0777
int main()
{
int semfull;
int semempty;
int semmutexp;
int semmutexc;
int shmid;
struct sembuf semwait;
struct sembuf semsignal;
semwait.sem_num=0;
semwait.sem_op=-1;
semwait.sem_flg=0;
semsignal.sem_num=0;
semsignal.sem_op=1;
semsignal.sem_flg=0;
if((shmid=shmget(IPC_PRIVATE,sizeof(int)*13,PERM))==-1)
{
printf("Create Share Memory Error.");
exit(1);
}
semfull=semget(IPC_PRIVATE,1,PERM);
int j;
for(j=0;j<10;j++)
{
semop(semfull,&semsignal,1);
}
semempty=semget(IPC_PRIVATE,1,PERM);
semmutexp=semget(IPC_PRIVATE,1,PERM);
semop(semmutexp,&semsignal,1);
semmutexc=semget(IPC_PRIVATE,1,PERM);
semop(semmutexc,&semsignal,1);
int child1,child2,child3,child4;
int *shm_addrp1,
*shm_addrp2,
*shm_addrc1,
*shm_addrc2,
*shm_main;
shm_main=(int *)shmat(shmid,0,0);
shm_main[12]=0;
if((child1=fork())==0)
{
shm_addrp1=(int*)shmat(shmid,0,0);
shm_addrp1[10]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d]./n",getpid(),i,shm_addrp1[10]);
shm_addrp1[shm_addrp1[10]]=i;
shm_addrp1[10]=(shm_addrp1[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}
shmdt(shm_addrp1);
exit(0);
}
else
{
if((child2=fork())==0)
{
shm_addrc1=(int*)shmat(shmid,0,0);
shm_addrc1[11]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc1[12] += shm_addrc1[shm_addrc1[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d/n",getpid(),shm_addrc1[shm_addrc1[11]],shm_addrc1[11],shm_addrc1[12]);
shm_addrc1[11]=(shm_addrc1[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
shmdt(shm_addrc1);
exit(0);
}
else
{
if((child3=fork())==0)
{
shm_addrc2=(int*)shmat(shmid,0,0);
shm_addrc2[11]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc2[12]+=shm_addrc2[shm_addrc2[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d/n",getpid(),shm_addrc2[shm_addrc2[11]],shm_addrc2[11],shm_addrc2[12]);
shm_addrc2[11]=(shm_addrc2[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
shmdt(shm_addrc2);
exit(0);
}
else
{
if((child4=fork())==0)
{
shm_addrp2=(int*)shmat(shmid,0,0);
shm_addrp2[10]=0;
int i;
for( i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d]./n",getpid(),i,shm_addrp2[10]);
shm_addrp2[shm_addrp2[10]]=i;
shm_addrp2[10]=(shm_addrp2[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}
shmdt(shm_addrp2);
exit(0);
}
else
{
waitpid(child1,NULL,0);
waitpid(child2,NULL,0);
waitpid(child3,NULL,0);
waitpid(child4,NULL,0);
semctl(semfull,2,IPC_RMID,0);
semctl(semempty,2,IPC_RMID,0);
semctl(semmutexp,2,IPC_RMID,0);
semctl(semmutexc,2,IPC_RMID,0);
shmdt(shm_main);
shmctl(shmid,IPC_RMID,0);
return 0;
}
}
}
}
}