Provide the correct solution.    The task is similar to the producer–consumer problem. The farmer and Shopowner share a fixed-size buffer named warehouse  used as a queue. The farmer’s job is to harvest crops(Rice=R, Wheat=W, Potato=P, Sugarcane=S, Maize=M) and put this in the warehouse. Imagine that warehouses have different rooms for different crops.  The Shopowner’s job is to take the crops from this wearhouse and make that  crops room empty(=N).You have 5 Farmers and 5 Shop Owners    You need to modify the following C code: #include #include #include /* This program provides a possible solution using mutex and semaphore. use 5 Farmers and 5 ShopOwners 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'}; //indicating 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 particular 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 takes crops and makes that particular 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() {        /*initializing thread,mutex,semaphore     */     pthread_t Far[5],Sho[5];     pthread_mutex_init(&mutex, NULL);     sem_init(&empty,0,warehouseSize);//when the warehouse is full thread will wait     sem_init(&full,0,0);//when the warehouse is empty thread will wait       int a[5] = {1,2,3,4,5}; //Just used for numbering the Farmer and ShopOwner       /*create 5 thread for Farmer 5 thread for ShopOwner     -------------------------------------------------     -------------------------------------------------     */               //  Closing or destroying 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

Provide the correct solution. 

 

The task is similar to the producer–consumer problem. The farmer and Shopowner share a fixed-size buffer named warehouse  used as a queue. The farmer’s job is to harvest crops(Rice=R, Wheat=W, Potato=P, Sugarcane=S, Maize=M) and put this in the warehouse.

Imagine that warehouses have different rooms for different crops. 

The Shopowner’s job is to take the crops from this wearhouse and make that 

crops room empty(=N).You have 5 Farmers and 5 Shop Owners 

 

You need to modify the following C code:

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>

/*

This program provides a possible solution using mutex and semaphore.

use 5 Farmers and 5 ShopOwners 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'}; //indicating 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 particular 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 takes crops and makes that particular 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()

{   

    /*initializing thread,mutex,semaphore

    */

    pthread_t Far[5],Sho[5];

    pthread_mutex_init(&mutex, NULL);

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

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

 

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

 

    /*create 5 thread for Farmer 5 thread for ShopOwner

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

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

    */

    

    

    //  Closing or destroying 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 5 steps with 2 images

Blurred answer
Knowledge Booster
Properties of Different Architectures
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
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