Producer Consumer problem using shared memory


/* Producer-Consumer program to accept data into shared memory and perform operations using semaphores */
// Producer part.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>

int main()
{
int key1 = 1000,shmid1,key2 = 2000,shmid2,key3 = 3000,shmid3;
int sem_key = 4000,semid;
int *roll,*m,i,count = 0;
char *name;

shmid1 = shmget(key1,10,IPC_CREAT);
if(shmid1 == -1)
{
printf(“\n\t 1st ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
roll = shmat(shmid1,NULL,0);

shmid2 = shmget(key2,100,IPC_CREAT);
if(shmid2 == -1)
{
printf(“\n\t 2nd ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
name = shmat(shmid2,NULL,0);

shmid3 = shmget(key3,100,IPC_CREAT);
if(shmid3 == -1)
{
printf(“\n\t 3rd ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
m = shmat(shmid3,NULL,0);

semid = semget(sem_key,1,IPC_CREAT);
if(semid == -1)
{
printf(“\n\t semaphore ERROR : SEMAPHORE CREATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}

struct sembuf lock_it;

lock_it.sem_flg = IPC_NOWAIT;
lock_it.sem_op = -1; // lock.
lock_it.sem_num = 0;
semop(semid,&lock_it,1);

do
{
count = count + 1;
printf(“\n\t Enter the details of student%d \n”,count);
printf(“\n\t ROLL NO. : “);
scanf(“%d”,roll);
if(*roll <= 0)
{
printf(“\n\n\t\t ERROR : INVALID ENTRY >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
printf(“\n\t NAME : “);
scanf(“%s”,name);
printf(“\n\t Enter MARKS achieved for the subjects : “);
for(i = 0;i < 5;i++)
{
printf(“\n\t SUBJECT %d : “,i+1);
scanf(“%d”,m+i);
}
printf(“\n\n”);

lock_it.sem_flg = 0;
lock_it.sem_op = +1; // unlock.
lock_it.sem_num = 0;
semop(semid,&lock_it,1);

}while(*roll != 0);

shmctl(shmid1,IPC_RMID,NULL);
shmctl(shmid2,IPC_RMID,NULL);
shmctl(shmid3,IPC_RMID,NULL);

return 0;
}

….

// Consumer part.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>

int main()
{
int key1 = 1000,shmid1,key2 = 2000,shmid2,key3 = 3000,shmid3;
int sem_key = 4000,semid;
int *roll,*m,i,count = 0,total,avg;
char *name;

shmid1 = shmget(key1,100,IPC_CREAT);
if(shmid1 == -1)
{
printf(“\n\t 1st ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
roll = shmat(shmid1,NULL,0);

shmid2 = shmget(key2,100,IPC_CREAT);
if(shmid2 == -1)
{
printf(“\n\t 2nd ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
name = shmat(shmid2,NULL,0);

shmid3 = shmget(key3,100,IPC_CREAT);
if(shmid3 == -1)
{
printf(“\n\t 3rd ERROR : ALLOCATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}
m = shmat(shmid3,NULL,0);

semid = semget(sem_key,1,IPC_CREAT);
if(semid == -1)
{
printf(“\n\t ERROR : SEMAPHORE CREATION FAILED >>> \n\n\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}

struct sembuf lock_it;

do
{
lock_it.sem_flg = 0;
lock_it.sem_op = -1; //lock
lock_it.sem_num = 0;
semop(semid,&lock_it,1);

total = 0;
count = count + 1;

printf(“\n\n\t DETAILED RECORD FOR STUDENT%d “,count);
printf(“\n\t NAME : %s”,name);
printf(“\n\t ROLL : %d”,*roll);
printf(“\n\t MARKS ACHIEVED “);
for(i = 0;i < 5;i++)
{
total = total + *(m+i);
printf(“\n\t SUBJECT%d : %d”,i+1,*(m+i));
}
avg = total / 5;
printf(“\n\t TOTAL MARKS : %d”,total);
printf(“\n\t AVERAGE PERCENT : %d”,avg);
printf(“\n\n”);
sleep(1);

}while(*roll != 0);

shmctl(shmid1,IPC_RMID,NULL);
shmctl(shmid2,IPC_RMID,NULL);
shmctl(shmid3,IPC_RMID,NULL);

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s