Question
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
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 producers 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 consumers 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
#define N 100 number of slots in the buffer semaphores are a special kind of int typedef int semaphore; Semaphore mutex 1; controls access to critical region Semaphore empty N, counts empty buffer slots Semaphore ful 0; counts full buffer slots void producer(void) int item; while TRUE) TRUE is the constant 1 item produce em() generate something to put in buffer down (&empty); decrement empty count down (&mutex); enter critical region insert item (item); put new item in buffer up(&mutex); leave critical region up(&full); increment count of full slots void consumer (void) int item; while TRUE) infinite loop down(&full); decrement full count down(&mutex); enter critical region take item from buffer item remove item up(&mutex: leave critical region up(&empty); increment count of empty slots consume item(item); do something with the item Figure 2.28. The producer-consumer problem using semaphores. #define N 100 number of slots in the buffer semaphores are a special kind of int typedef int semaphore; Semaphore mutex 1; controls access to critical region Semaphore empty N, counts empty buffer slots Semaphore ful 0; counts full buffer slots void producer(void) int item; while TRUE) TRUE is the constant 1 item produce em() generate something to put in buffer down (&empty); decrement empty count down (&mutex); enter critical region insert item (item); put new item in buffer up(&mutex); leave critical region up(&full); increment count of full slots void consumer (void) int item; while TRUE) infinite loop down(&full); decrement full count down(&mutex); enter critical region take item from buffer item remove item up(&mutex: leave critical region up(&empty); increment count of empty slots consume item(item); do something with the item Figure 2.28. The producer-consumer problem using semaphoresStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started