Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Semaphores: Producer Consumer problem We are going to use 1 producer thread and

ID: 3643217 • Letter: S

Question

Semaphores: Producer Consumer problem We are going to use 1 producer thread and 5 consume threads. They are going to run forever. There are two versions of the algorithm: 1. without using semaphores (proj7v1.cpp) 2. with semaphores (proj7v2.cpp) Submit both programs with sample outputs for both. Try to highlight the differences between both outputs. Feel free to insert sleep() in appropriate places to enable strange behavior. Try to keep those sleep() calls in both programs too. Use the following diagrams and code to guide you through the project.


Array b[] can be an array of 20 integers. produce() can be as simple as return the next integer in the sequence starting with integer value 1. consume(w) can simply print w out.

Once the assignment is completed, submit all the source files and 2 sample output files.

proj7v1.cpp proj7v2.cpp buffer.h buffer.cpp output1.txt output2.txt


any help with this would be great thanks in advance

Explanation / Answer

#include #include #include #include #include #include #include #include #define BUFFERSIZE 20 #define P(s) semop(s, &pop, 1) #define V(s) semop(s, &vop, 1) struct sembuf pop, vop; void consumer(int *full, int *empty, int *mutex, int *index, int *buffer, int *sum, int temp, int n) { do { if(*index == 50) break; P(*full); P(*mutex); if(*index == 50) break; temp = buffer[(*index)%BUFFERSIZE]; printf(" Consumer %d reads : %d at index : %d ", n, temp, *index); (*index)++; *sum = *sum +temp; V(*mutex); V(*empty); }while(1); V(*mutex); V(*full); shmdt(buffer); shmdt(sum); shmdt(index); return; } void producer(int *full, int *empty, int *mutex, int *buffer) { int cnt; for(cnt = 0; cnt < 50; cnt++) { P(*empty); P(*mutex); buffer[cnt%BUFFERSIZE] = cnt+1; printf("Producer writes : %d ", buffer[cnt%BUFFERSIZE]); V(*mutex); V(*full); } shmdt(buffer); return; } int main() { int status, temp = 0; int full, empty, mutex; int shmid1, shmid2, shmid3; int *buffer, *sum, *index; int cpid1, cpid2, cpid3; shmid1 = shmget(IPC_PRIVATE, BUFFERSIZE*sizeof(int), 0777|IPC_CREAT); shmid2 = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT); shmid3 = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT); full = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); empty = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); mutex = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT); semctl(full, 0, SETVAL, 0); semctl(mutex, 0, SETVAL, 1); semctl(empty, 0, SETVAL, BUFFERSIZE); pop.sem_num = vop.sem_num = 0; pop.sem_flg = vop.sem_flg = 0; pop.sem_op = -1; vop.sem_op = 1; if((cpid1 = fork()) == 0) { buffer = (int *) shmat(shmid1, 0, 0); sum = (int *) shmat(shmid2, 0, 0); index = (int *) shmat(shmid3, 0, 0); consumer(&full, &empty, &mutex, index, buffer, sum, temp, 1); exit(0); } else if((cpid2 = fork()) == 0) { buffer = (int *) shmat(shmid1, 0, 0); sum = (int *) shmat(shmid2, 0, 0); index = (int *) shmat(shmid3, 0, 0); consumer(&full, &empty, &mutex, index, buffer, sum, temp, 2); exit(0); } else if((cpid3 = fork()) == 0) { buffer = (int *) shmat(shmid1, 0, 0); producer(&full, &empty, &mutex, buffer); exit(0); } else { sum = (int *) shmat(shmid2, 0, 0); waitpid(cpid1, &status, 0); waitpid(cpid2, &status, 0); waitpid(cpid3, &status, 0); printf("sum : %d ", *sum); semctl(full, 0, IPC_RMID, 0); semctl(empty, 0, IPC_RMID, 0); semctl(mutex, 0, IPC_RMID, 0); shmdt(sum); shmctl(shmid1, IPC_RMID, 0); shmctl(shmid2, IPC_RMID, 0); shmctl(shmid3, IPC_RMID, 0); } return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote