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
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
Step by step
Solved in 5 steps with 2 images