You are on page 1of 1

#include<stdio.

h>
#include<sys/types.h>
#include<sys/shm.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
main()
{
char *ptr,str[5];
struct sembuf wait={0,-1,SEM_UNDO},signal={0,1,SEM_UNDO};
int shmid,pid,semid,prod,cons,i;
prod=semget((key_t)10,1,IPC_CREAT|0666);
cons=semget((key_t)11,1,IPC_CREAT|0666);
semct1(prod,0,SETVAL,1);
semct1(cons,0,SETVAL,0);
shmid=shmget((key_t)100,20,IPC_CREAT|0666);
ptr=(char*)shmat(shmid,0,0);
pid=fork();
if(pid==0)
while(1)
{
semop(prod,&wait,1);
printf("\n Producer produces ");
scanf("%s",str);
strcpy(ptr,str);
semop(cons,&signal,1);
if(pid>0)
while(1)
{
print("\n Consumer waits for the producer to place an item \n");
semop(cons,&wait,1);
if(strcmp(ptr,"quit")==0)
{
printf("\n end of the process ");
kill(pid,SIGINT);
exit(0);
}
printf("\n Consumer consumes \t %s \n",ptr);
semop(prod,&signal,1);
}
}

You might also like