PROGRAM CODING:-
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <time.h>
#include <unistd.h>
#include<sys/wait.h>
#define MUTEX 0
#define FULL 1
#define EMPTY 2
#define SHM_KEY 5688
#define SEM_KEY 1256
int N,SIZE;
union semun { int val; struct semid_ds *buf; unsigned short *array; };
void set_sem_value( int semid, int i, int val )
{
union semun initval;
initval.val=val;
semctl( semid,i, SETVAL,initval);
}
int generate_number( int, int );
void wait_and_lock( int semid, int semnum )
{
struct sembuf op_lock[2];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=0;
op_lock[0].sem_flg=0;
op_lock[1].sem_num=semnum;
op_lock[1].sem_op=1;
op_lock[1].sem_flg=SEM_UNDO;
semop( semid,op_lock,2);
}
void signal_and_unlock( int semid, int semnum )
{
struct sembuf op_unlock[1]=
{ {semnum,-1,SEM_UNDO}
};
semop( semid,op_unlock,1); }
int get_sem_value( int semid, int i )
{
union semun initval;
return semctl( semid,i, GETVAL,initval);
}
void simwait(int semid,int semnum)
{
struct sembuf op_lock[1];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=-1;
op_lock[0].sem_flg=SEM_UNDO;
semop(semid,op_lock,1);
}
void simsignal(int semid,int semnum)
{
struct sembuf op_lock[1];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=1;
op_lock[0].sem_flg=SEM_UNDO;
semop(semid,op_lock,1);
}
int *buffer,in=0,out=0;
int main()
{
int produced, i;
int shmid,semid, pid;
time_t t;
printf("\nEnter the buffer size:");
scanf("%d",&SIZE);
printf("\nEnter the number of operations to be performed:");
scanf("%d",&N);
shmid= shmget( SHM_KEY, (sizeof(int))*SIZE, IPC_CREAT | 0666);
buffer=(int *)shmat( shmid, 0,0);
srand(time(&t));
semid= semget( SEM_KEY,3,IPC_CREAT|0666 );
set_sem_value(semid,MUTEX,1);
set_sem_value(semid,FULL,0);
set_sem_value(semid,EMPTY,SIZE);
signal_and_unlock(semid,MUTEX);
pid=fork();
if(pid!=0)
{
for( i=0; i<N; i++)
{
int f1=0;
while(get_sem_value(semid,FULL)==SIZE)
{if(f1==0){printf("\nBuffer full!Producer waiting!");f1=1;}}
simwait(semid,EMPTY);
wait_and_lock( semid,MUTEX );
produced = generate_number(1,50);
printf("\nProduced: %d",produced);
buffer[in]=produced;
in=(in+1)%SIZE;
signal_and_unlock( semid, MUTEX );
simsignal(semid,FULL);
sleep(rand()%2+1);
}
printf("\nProducer Exited\n");
wait(NULL);
shmdt( buffer );
shmctl(shmid, IPC_RMID,0);
semctl(semid, IPC_RMID,0);
}
else
{ printf("\nBuffer empty!Consumer waiting ");
for( i=0; i<N; i++)
{ int f1=0;
while(get_sem_value(semid,EMPTY)==SIZE)
{if(f1==0){printf("\nBuffer empty!Consumer waiting!");f1=1;}
};
simwait( semid, FULL);
wait_and_lock( semid, MUTEX );
printf("\nConsumed: %d", buffer[out]);
out=(out+1)%SIZE;
signal_and_unlock( semid, MUTEX );
simsignal(semid,EMPTY);
sleep(rand()%5+1);
}
printf("\nConsumer Exited\n");
}
return 0;
}
int generate_number( int lower, int upper)
{
return (rand()% ( upper-lower)) + lower;
}
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <time.h>
#include <unistd.h>
#include<sys/wait.h>
#define MUTEX 0
#define FULL 1
#define EMPTY 2
#define SHM_KEY 5688
#define SEM_KEY 1256
int N,SIZE;
union semun { int val; struct semid_ds *buf; unsigned short *array; };
void set_sem_value( int semid, int i, int val )
{
union semun initval;
initval.val=val;
semctl( semid,i, SETVAL,initval);
}
int generate_number( int, int );
void wait_and_lock( int semid, int semnum )
{
struct sembuf op_lock[2];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=0;
op_lock[0].sem_flg=0;
op_lock[1].sem_num=semnum;
op_lock[1].sem_op=1;
op_lock[1].sem_flg=SEM_UNDO;
semop( semid,op_lock,2);
}
void signal_and_unlock( int semid, int semnum )
{
struct sembuf op_unlock[1]=
{ {semnum,-1,SEM_UNDO}
};
semop( semid,op_unlock,1); }
int get_sem_value( int semid, int i )
{
union semun initval;
return semctl( semid,i, GETVAL,initval);
}
void simwait(int semid,int semnum)
{
struct sembuf op_lock[1];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=-1;
op_lock[0].sem_flg=SEM_UNDO;
semop(semid,op_lock,1);
}
void simsignal(int semid,int semnum)
{
struct sembuf op_lock[1];
op_lock[0].sem_num=semnum;
op_lock[0].sem_op=1;
op_lock[0].sem_flg=SEM_UNDO;
semop(semid,op_lock,1);
}
int *buffer,in=0,out=0;
int main()
{
int produced, i;
int shmid,semid, pid;
time_t t;
printf("\nEnter the buffer size:");
scanf("%d",&SIZE);
printf("\nEnter the number of operations to be performed:");
scanf("%d",&N);
shmid= shmget( SHM_KEY, (sizeof(int))*SIZE, IPC_CREAT | 0666);
buffer=(int *)shmat( shmid, 0,0);
srand(time(&t));
semid= semget( SEM_KEY,3,IPC_CREAT|0666 );
set_sem_value(semid,MUTEX,1);
set_sem_value(semid,FULL,0);
set_sem_value(semid,EMPTY,SIZE);
signal_and_unlock(semid,MUTEX);
pid=fork();
if(pid!=0)
{
for( i=0; i<N; i++)
{
int f1=0;
while(get_sem_value(semid,FULL)==SIZE)
{if(f1==0){printf("\nBuffer full!Producer waiting!");f1=1;}}
simwait(semid,EMPTY);
wait_and_lock( semid,MUTEX );
produced = generate_number(1,50);
printf("\nProduced: %d",produced);
buffer[in]=produced;
in=(in+1)%SIZE;
signal_and_unlock( semid, MUTEX );
simsignal(semid,FULL);
sleep(rand()%2+1);
}
printf("\nProducer Exited\n");
wait(NULL);
shmdt( buffer );
shmctl(shmid, IPC_RMID,0);
semctl(semid, IPC_RMID,0);
}
else
{ printf("\nBuffer empty!Consumer waiting ");
for( i=0; i<N; i++)
{ int f1=0;
while(get_sem_value(semid,EMPTY)==SIZE)
{if(f1==0){printf("\nBuffer empty!Consumer waiting!");f1=1;}
};
simwait( semid, FULL);
wait_and_lock( semid, MUTEX );
printf("\nConsumed: %d", buffer[out]);
out=(out+1)%SIZE;
signal_and_unlock( semid, MUTEX );
simsignal(semid,EMPTY);
sleep(rand()%5+1);
}
printf("\nConsumer Exited\n");
}
return 0;
}
int generate_number( int lower, int upper)
{
return (rand()% ( upper-lower)) + lower;
}
No comments:
Post a Comment