Title: Gas Station exercise (process synchronization with semaphores)
Tags: process;operating-system;synchronization;semaphore;pseudocode
Question: I'm trying to write a program in pseudo code for my Operating Systems class: we have a gas station with N pumps and 1 M litre fuel tank. Each car requires to refuel with a given amount of petrol. The fuel tank is supplied by a tank truck that refills it up to maximum capacity, only if no car is refueling.


Cars can get gas only if there's an available pump, if there is enough petrol and if the tank truck is not refilling the fuel tank.
Write a solution in pseudo code which optimises access to resources using semaphores and processes.


And this is what I've done:

```#define M 15;
#define N 3;
sem pumps=N;
sem fuel_tank=3;
int max_capacity=M;
int request;
bool fill=false;

----
CAR:
----

wait(pumps);        

request=rand()%3+1;

if(request<max_capacity &amp;&amp; fill==false)
{
wait(fuel_tank);
refuel();
max_capacity=max_capacity-request;
signal(fuel_tank);
}

signal(pumps);

-----------
TANK TRUCK:
-----------


wait(pumps);

fill=true;
while(fuel_tank!=3)
sleep(1);

refill();
max_capacity=M-max_capacity;
fill=false;

signal(pumps);
```

Since this is my very first exercise with semaphores and processes, I'm not sure if the above code is correct and could work well.

What do you think about it? Is there something wrong?

Comment: @MartinJames, why does it fail? Can you explain me which kind of problem does it bring to the entire code?

Comment: 'sleep(1);' fail:(

Comment: It would be nice to add comments in the code to explain what you intend to do at line of code and what each variable is used for.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:04/07/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<process><operating-system><synchronization><semaphore><pseudocode>', 'Title': 'Gas Station exercise (process synchronization with semaphores)', 'ViewCount': 327, 'id': 49706265, 'user_AboutMe': '', 'user_AccountId': 10485493.0, 'user_DisplayName': 'Alopex', 'user_Reputation': 95.0, 'user_Views': 32.0, 'user_id': 7729189.0}}