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

This was asked already but the answer isnt correct- text book is Modern Operatin

ID: 3834756 • Letter: T

Question

This was asked already but the answer isnt correct- text book is Modern Operating Systems fourth edition

Follow the outline given in Figure 2-28 of the textbook and implement producer-consumer problem using shared memory and semaphores. Your program should copy a file named file1 to another file named file2 byte by byte through a shared buffer. Your main process should fork two processes: one is producer and another is consumer. The main process then pauses waiting for the signals from its children. The producer’s job is to move the bytes in file1 to a shared buffer (implemented by shared memory), one byte at a time. Insert random delay between two movings. The consumer’s job is to move the bytes in the shared buffer to file2, one byte at a time. You should also insert random delay between two movings.

Whenever the producer moves an item into the shared buffer, it sends a signal to main process. Main process enters a “P” in the monitoring table. Similarly, whenever the consumer moves an item from the shared buffer, it sends a signal to main process. Main process then enters a “C” in the monitoring table. When producer and consumer have copied the entire file1, consumer sends a signal to main process. Main process outputs the monitoring table.

Implement the above in the UNIX environment using C or C++. You may need system calls: signal, kill, pause, sleep, semget, semctl, semop, shmget, shmat, shmctl, etc. When sending signals, you should use the two signals defined for users: SIGUSR1 and SIGUSR2 for the producer and consumer respectively

Part I am stuck on... sending and recieving signals and where to put them? in main or in each child process?

Explanation / Answer

ANSWER:::

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void* producer(void *arg);
void* consumer(void *arg);
char buff[20];
sem_t full,empty;
int main()
{
pthread_t pid,cid;
sem_init(&empty,0,1);
sem_init(&full,0,0);
pthread_create(&pid,NULL,producer,NULL);
pthread_create(&cid,NULL,consumer,NULL);
pthread_join(pid,NULL);
pthread_join(cid,NULL);
}
void* producer(void*arg)
{
int run=1;
while(run)
{
sem_wait(&empty);
printf(“ Enter Mes to be add into buffer:”);
scanf(“%s”,buff);
if(strncmp(buff,”end”,3)==0)
run=0;
sem_post(&full);
}
return NULL;
}
void* consumer(void *arg)
{
int run=1;
while(run)
{
sem_wait(&full);
printf(“ Consumed item is %s ”,buff);
if(strncmp(buff,”end”,3)==0)
run=0;
sem_post(&empty);
}
return NULL;
}

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