Modify the following C code to get the given output: #include #include #include   /* This program provides a possible solution using mutex and semaphore. use 5 Fsrmers and 5 ShopOwner to demonstrate the solution. */ #define MaxCrops 5 // Maximum crops a Farmer can produce or a Shpoowner can take #define warehouseSize 5 // Size of the warehouse   sem_t empty; sem_t full; int in = 0; int out = 0; char crops[warehouseSize]={'R','W','P','S','M'};//indicatig room for different crops char warehouse[warehouseSize]={'N','N','N','N','N'};//initially all the room is empty pthread_mutex_t mutex;   void *Farmer(void *far) {       /*    1.Farmer harvest crops and put them in perticular room. For example, room 0 for Rice(R).    2.use mutex and semaphore for critical section.    3.print which farmer is keeping which crops in which room inside the critical section.    4.print the whole warehouse buffer outside of the critical section    */      } void *ShopOwner(void *sho) {    /*    1.Shop owner take crops and make that perticular room empty.    2.use mutex and semaphore for critical section.    3.print which shop owner is taking which crops from which room inside the critical section.    4.print the whole warehouse buffer outside of the critical section    */   int main() {        /*intializing thread,mutex,semaphore     */     pthread_t Far[5],Sho[5];     pthread_mutex_init(&mutex, NULL);     sem_init(&empty,0,warehouseSize);//when the wearehouse is full thread will wait     sem_init(&full,0,0);//when the wearehouse is empty thread will wait       int a[5] = {1,2,3,4,5}; //Just used for numbering the Farmer and ShopOwner       /*creat 5 thread for Farmer 5 thread for ShopOwner     -------------------------------------------------     -------------------------------------------------     */               //  Closing/distroying mutex and semaphore     pthread_mutex_destroy(&mutex);     sem_destroy(&empty);     sem_destroy(&full);          return 0;     }   Example Output : Farmer 3: Insert crops R at 0 Farmer 4: Insert crops W at 1 Farmer 5: Insert crops P at 2 Shop owner 1: Remove crops R from 0 Shop owner 1: Remove crops W from 1 Shop owner 1: Remove crops P from 2 Farmer 4: Insert crops S at 3 Farmer 4: Insert crops M at 4 Farmer 4: Insert crops R at 0 Shop owner 1: Remove crops S from 3 Shop owner 1: Remove crops M from 4 ShopOwner1: RNNNN …………………………………… ………………………………….. ………………………………….. …………………………………. Shop owner 5: Remove crops W from 1 Farmer 5: Insert crops P at 2 Farmer 5: Insert crops S at 3 Farmer 5: Insert crops M at 4 Farmer5: NNPSM Shop owner 5: Remove crops P from 2 Shop owner 5: Remove crops S from 3 Shop owner 5: Remove crops M from 4 ShopOwner5: NNNNN

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Modify the following C code to get the given output:

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>

 

/*

This program provides a possible solution using mutex and semaphore.

use 5 Fsrmers and 5 ShopOwner to demonstrate the solution.

*/

#define MaxCrops 5 // Maximum crops a Farmer can produce or a Shpoowner can take

#define warehouseSize 5 // Size of the warehouse

 

sem_t empty;

sem_t full;

int in = 0;

int out = 0;

char crops[warehouseSize]={'R','W','P','S','M'};//indicatig room for different crops

char warehouse[warehouseSize]={'N','N','N','N','N'};//initially all the room is empty

pthread_mutex_t mutex;

 

void *Farmer(void *far)

{   

   /*

   1.Farmer harvest crops and put them in perticular room. For example, room 0 for Rice(R).

   2.use mutex and semaphore for critical section.

   3.print which farmer is keeping which crops in which room inside the critical section.

   4.print the whole warehouse buffer outside of the critical section

   */

    

}

void *ShopOwner(void *sho)

{   

/*

   1.Shop owner take crops and make that perticular room empty.

   2.use mutex and semaphore for critical section.

   3.print which shop owner is taking which crops from which room inside the critical section.

   4.print the whole warehouse buffer outside of the critical section

   */

 

int main()

{   

    /*intializing thread,mutex,semaphore

    */

    pthread_t Far[5],Sho[5];

    pthread_mutex_init(&mutex, NULL);

    sem_init(&empty,0,warehouseSize);//when the wearehouse is full thread will wait

    sem_init(&full,0,0);//when the wearehouse is empty thread will wait

 

    int a[5] = {1,2,3,4,5}; //Just used for numbering the Farmer and ShopOwner

 

    /*creat 5 thread for Farmer 5 thread for ShopOwner

    -------------------------------------------------

    -------------------------------------------------

    */

    

    

    //  Closing/distroying mutex and semaphore

    pthread_mutex_destroy(&mutex);

    sem_destroy(&empty);

    sem_destroy(&full);

    

    return 0;    

}

 

Example Output :
Farmer 3: Insert crops R at 0
Farmer 4: Insert crops W at 1
Farmer 5: Insert crops P at 2

Shop owner 1: Remove crops R from 0
Shop owner 1: Remove crops W from 1
Shop owner 1: Remove crops P from 2
Farmer 4: Insert crops S at 3
Farmer 4: Insert crops M at 4
Farmer 4: Insert crops R at 0
Shop owner 1: Remove crops S from 3
Shop owner 1: Remove crops M from 4
ShopOwner1: RNNNN
……………………………………
…………………………………..
…………………………………..
………………………………….
Shop owner 5: Remove crops W from 1
Farmer 5: Insert crops P at 2
Farmer 5: Insert crops S at 3
Farmer 5: Insert crops M at 4
Farmer5: NNPSM
Shop owner 5: Remove crops P from 2
Shop owner 5: Remove crops S from 3
Shop owner 5: Remove crops M from 4
ShopOwner5: NNNNN

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY