Question: Please help, im not sure how to fix my code so that it runs properly Here is the instructions: TODO:: Find a free pump and
Please help, im not sure how to fix my code so that it runs properly
Here is the instructions:
TODO:: Find a free pump and fill up using that pump, otherwise wait until a pump becomes
available.
You will be using a single mutex and a bit value to utilize up to different pumps.
Each bit in the bit 'freemask' will represent the status of a single pump in use
or not in use. Refer to the documentation if you need a refresher on how to set, check
and clear individual bits.
All accesses to shared memory must be thread safe and no two cars should even attempt to
access the same pump at the same time.
You will be using the 'fillTankUp' method of the 'Pump' class to simulate a car filling
up Filling up must be implemented in such a way that allows for other cars to
concurrently use the other pumps while a car fills up
If a car fails to find an available pump it must wait until one becomes available.
After a car successfully fills up it either must wait for all other cars to fill up OR
wait until enough time has gone by for all other cars to fill up once. If you choose
to use the time based algorithm then you'll need to look into the function sleepfor,
which is found in the std::thisthread namespace. You will need to sleep for a value
based on the information available to you number of cars, number of pumps, how long
a single car takes to fill up
NOTE: You may NOT modify any of the code outside of the TODOs or add any global or static
variables UNLESS it's required by your algorithm for # above.
Here is my code:
std::uniquelock lockgetstationMutex;
while true
iterate through the number of pumps in the station
for int i ; i pumpsInStation; i
check if pump is available
if freeMask & i
mark pump as in use
freeMask & ~ i;
unlock mutex while filling up
lock.unlock;
fill up tank
pumpsifillTankUp;
lock mutex again
lock.lock;
mark pump as free
freeMask i;
notify waiting cars
stationConditionnotifyall;
return ;
getStationConditionwaitlock;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
