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

Control two exclusively shared resources. The two resources are two files. The p

ID: 3658144 • Letter: C

Question

Control two exclusively shared resources. The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers Producer: #include #include #include #include #include #include #include #include #include #define PUB7645 "./PubFile" union semun { int val; struct semid_ds *buf; ushort *array; }; int main() { int i=0, intValue, semID; key_t key; FILE *fptr; union semun arg; static ushort startVal[2] = {1, 0}; static struct sembuf acquire = {0, -1, SEM_UNDO}, release = {1, 1, SEM_UNDO}; key = ftok(".", 's'); if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) { perror("Sem Creation:"); exit(2); } arg.array = startVal; if (semctl(semID, 2, SETALL, arg) == -1 ) { perror("semctl: SETALL"); exit(3); } for( ; ; ) { srand(getpid()+i); intValue =rand()*49/RAND_MAX + 1; if (semop(semID, &acquire, 1) == -1) { perror("Producer waiting"); exit(4); } if ((fptr = fopen(PUB7645, "w")) == NULL) { perror(PUB7645); exit(1); }fprintf(fptr, "%d ", intValue); fclose(fptr); if (semop(semID, &release, 1) == -1) { perror("new"); exit(5); } i++; } } Consumer #include #include #include #include #include #include #include #include #include #define PUB7645 "./PubFile" union semun { int val; struct semid_ds *buf; ushort *array; }; int main() { int i=0, intValue, semID; key_t key; FILE *fptr; union semun arg; static struct sembuf acquire = {1, -1, SEM_UNDO}, release = {0, 1, SEM_UNDO}; key = ftok(".", 's'); if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) { perror("Sem Creation:"); exit(2); } for(i=1; i<=5 ; i++) { if (semop(semID, &acquire, 1) == -1) { perror(

Explanation / Answer

it is very tough job to understand the ocde please rearrange the post

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