# A question I have to solve til tomorrow

1. May 11, 2007

### FrostScYthe

Hi I have to turn in pseudocode for an algorithm that will work the multiple producer-consumer problem in this way. I want to write the problem because you might understand it in a better way. There is an airline producer where each airline has one flight, and each flight can have a random number of seats between 60 and 200. There's an intermediary in which the airline puts the flights, and there are consumer agencies that will "consume" the seats available in the flight. The problem states that the flight maybe put available for all agencies to consume seats as long as it is not "empty on seats", once it's out of seats it's taken out and a new airline can enter and put its flight. I hope I defined it clearly. The solution to the problem has to be THE BARE FUNCIONALITY so we don't have to worry about objects and stuff it's more on how to use the semaphores to sinchronize this process. Also what changes would need to be made in order for the algorithm to allow for the airlines to offer more than one flight,... I was thinking hmm threads?

#define MAX SEATS 200

Intermediary(int capacity)
CREATE SHARED MEMORY (MAX SEATS);
create semaphore available = capacity;
create semaphore occupied = 0;
create semaphore mutexcl = 1;
}
Producer()
{
int capacity = rand(60,200);
Intermediary(capacity);
int seat;
while (TRUE) {
produce seat(seat);
wait( available);
wait( mutexcl);
enter seat(seat);
signal( mutexcl);
if(signal( occupied))
exit();
}
}
Consumer()
{
int seat;
while (TRUE) {
wait( occupied);
wait( mutexcl);
remove seat( seat);
signal( mutexcl);
signal( available);
consume seat(item);
}
}

2. May 15, 2007

### FrostScYthe

well, they gave us til later, lol.. turns out most of us were doing it wrong. What was explained anyway was that, we would have to have 3 separate processes. one being the airline, the intermediary, and the agency.

The airline-flight would have its own shared memory and semaphores created in there to control access to the flight.

The intermediary would act as a mapping table to match airlines with agencies, so that the agencies could chose where they want a seat, possibly controlled by semaphores to prevent more than one airline from editing one spot.

The agency would be a simple consumer consuming seats, the semaphores would be opened here and they would be mutual exclusion semaphores asking whether a sit is available or not, just to not leave indefinitely blocked agencies when the flight would be full.

When I get the pseudo code, I'll post it here. I will have to implement it in C eventually. :yuck: