In this program, you will write a program to solve the m-producer n-consumer pro
ID: 3640348 • Letter: I
Question
In this program, you will write a program to solve the m-producer n-consumer problem for thespecific values of m = 1 and n = 2. You have a shared circular buffer that can hold 20 integers. The
producer process stores the numbers 1 to 50 in the buffer one by one (in a for loop with 50
iterations) and then exits. Each of the consumer processes reads the numbers from the buffer and
adds them to a shared variable SUM (initialized to 0). Any consumer process can read any of the
numbers in the buffer. The only constrain is that every number written by some producer should be
read exactly once by exactly one of the consumers. Of course, a producer should not write when the
buffer is full, and a consumer should not read when the buffer is empty.
Write a program that first creates the shared circular buffer and the shared variable SUM using the
shm*() calls in Linux. You can create any other shared variable that you think you may need. The
program then forks the producers and the two consumers. The producer and consumer codes can be
written as functions that are called by the child processes. After the producer and both the
consumers have finished (the consumers exit after all the data produced by all the producers have
been read. How does a consumer know this?), the parent process prints the value of SUM. Note that
the value of SUM should be 25 × 51 = 1275 if your program is correct.
Explanation / Answer
#include #include #include #include /* Include this to use semaphores */ #define P(s) semop(s, &pop, 1) /* pop is the structure we pass for doing the P(s) operation */ #define V(s) semop(s, &vop, 1) /* vop is the structure we pass for doing the V(s) operation */ typedef struct clist { int count; int buffer[20]; } clist; main() { int *a, *b; int i,j, count = 50, status; clist* sdata; int* sum; int semid1, semid2 , semid3; int shmid1, shmid2 ; struct sembuf pop, vop ; semid1 = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); semid2 = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); semid3 = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); shmid1 = shmget(IPC_PRIVATE, sizeof(clist), 0777|IPC_CREAT); shmid2 = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT); semctl(semid1, 0, SETVAL, 0); semctl(semid2, 0, SETVAL, 20); semctl(semid3, 0, SETVAL, 1); pop.sem_num = vop.sem_num = 0; pop.sem_flg = vop.sem_flg = 0; pop.sem_op = -1 ; vop.sem_op = 1 ; sdata = (clist*) shmat(shmid1, 0, 0); sum = (int* ) shmat(shmid2, 0, 0); sdata->count = 0; *sum = 0; if (fork() == 0) { sdata = (clist*) shmat(shmid1, 0, 0); sum = (int* ) shmat(shmid2, 0, 0); do{ P(semid1); P(semid3); count = sdata->count; if(count < 50) { printf("Consumer 1 reads %d ", sdata->buffer[sdata->count%20]); *sum = *sum + sdata->buffer[sdata->count%20]; sdata->count++; } V(semid3); V(semid2); }while(countcount; if(count < 50) { printf("Consumer 1 reads %d ", sdata->buffer[sdata->count%20]); *sum = *sum + sdata->buffer[sdata->count%20]; sdata->count++; } V(semid3); V(semid2); }while(countRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.