32 flights must land at, be serviced by, and take off from, an airport. All flights that approach the airport go to a 20,000 holding pattern, which can accomodate all of the flights simultaneously. However, as they approach to land, they go thru 4 different flight areas. Each flight area can hold its own maximum number of flights, and must be visited in the given order:
Flight Area:Number of
simultaneous flights:10000 ft holding pattern3Landing Runway1Terminal4Take off Runway1
Note: some flight areas can hold multiple flights. Therefore, we do not want to lock the flight area. Rather, we want to lock the enter()-ing and leave()-ing of the flight area.
You must:
Make main() create NUM_FLIGHTS pthreads. Each pthread will run fly() given the address of one of the NUM_FLIGHTS instances of Flight in array flights[]. Afterwards, main()should wait for all NUM_FLIGHTS pthreads. Because fly() does not return any meaningful value, the second argument to pthread_join() can just be NULL.
Write void* fly (void* vPtr). fly() must cast the incoming vPtr to a pointer of type Flight, then run method doFlight() on that Flight instance. (You can say something like oHare.doFlight(*flightPtr)) Finally, it should return NULL
Finish the enter() method of class FlightArea. Do not disturb the code that is already there, but make it thread safe by locking the method as you begin, unlocking it as you leave, and waiting if the FlightArea is full.
Finish the leave() method of class FlightArea. Again, do not disturb the code that is already there, but make it thread safe by locking the method as you begin, unlocking it as you leave, and signalling that another Flight may enter.
Of course you may have add member variables to class FlightArea, and initialize and kill them in the constructor and destructor respectively.
The program:
/*————————————————————————-*
*— —*
*— airport.cpp —*
*— —*
*— This program simulates the approach, landing, servicing, —*
*— and taking off of several flights. Each flight is in its own —*
*— thread, and is to be handled in a thread-safe fashion. —*
*— —*
*— —- —- —- —- —- —- —- —- —*
*— —*
*— Version 1.0 2018 August 1 Joseph Phillips —*
*— —*
*————————————————————————-*/
//
// Compile with: g++ airport.cpp -o airport -lpthread
//
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
#include <pthread.h>
#include <queue>
using namespace std;
// PURPOSE: To hold the names of the various airlines
const char* AIRLINE_NAME_ARRAY[]
= {“Air No Cares”,
“Fly-By-Nite”,
“Pterodactyl”,
“SwineAir”,
“Flying Toaster”,
“Do You Dare Air”,
“Air Montgolfier”,
“Luft Zeppelin”
};
// PURPOSE: To tell how many elements are in array ‘AIRLINE_NAME_ARRAY[]’.
const int NUM_AIRLINES = sizeof(AIRLINE_NAME_ARRAY)
/ sizeof(const char*);
// PURPOSE: To tell how many Flight instances need to be serviced.
const int NUM_FLIGHTS = 32;
// PURPOSE: To represent instances of a flight.
class Flight
{
// I. Member vars:
// PURPOSE: To tell the name of flight.
string name_;
// II. Disallowed auto-generated methods:
protected :
// III. Protected methods:
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To randomly generate a flight. No parameters. No return value.
Flight ()
{
char text[64];
snprintf(text,64,”%s %d”,
AIRLINE_NAME_ARRAY[rand() % NUM_AIRLINES],
(rand() % 9999)+1
);
name_ = text;
}
// PURPOSE: To make ‘*this’ a copy of ‘source’. No return value.
Flight (const Flight& source
) :
name_(source.getName())
{ }
// PURPOSE: To release the resources of ‘*this’, make ‘*this’ a copy of
// ‘source’, and return a reference to ‘*this’. No return value.
Flight& operator= (const Flight& source
)
{
// I. Application validity check:
if (this == &source)
return(*this);
// II. Release resources:
// III. Copy ‘source’:
name_ = source.getName();
// IV. Finished:
return(*this);
}
// PURPOSE: To release the resources of ‘*this’. No parameters. No return
// value.
~Flight ()
{
}
// V. Accessors:
// PURPOSE: To return the name of the Flight.
const string& getName ()
const
{
return(name_);
}
};
// PURPOSE: To output Flight instance ‘flight’ to ‘os’. Returns reference to
// ‘os’.
ostream& operator<< (ostream& os,
const Flight& flight
)
{
os << flight.getName();
return(os);
}
// PURPOSE: To represent the various places that a limited number of Flight
// instances could be.
typedef enum
{
HOLDING_PATTERN_FLIGHT_AREA,
LANDING_RUNWAY_FLIGHT_AREA,
TERMINAL_FLIGHT_AREA,
TAKE_OFF_RUNWAY_FLIGHT_AREA
}
flightArea_t;
// PURPOSE: To represent the various places that a limited number of Flight
// instances could be.
class FlightArea
{
// I. Member vars:
// PURPOSE: To tell what type of flight area this is.
flightArea_t type_;
// PURPOSE: To tell the capacity.
int capacity_;
// PURPOSE: To hold pointers to the Flight instances.
queue<Flight> flightQueue_;
// YOUR CODE HERE:
// II. Disallowed auto-generated methods:
// No default constructor:
FlightArea ();
// No copy constructor:
FlightArea (const FlightArea&
);
// No copy assignment op:
FlightArea& operator= (const FlightArea&
);
protected :
// III. Protected methods:
// PURPOSE: To return the name of ‘*this’ FlightArea. No parameters.
string getName ()
const
{
const char* cPtr;
switch (type_)
{
case HOLDING_PATTERN_FLIGHT_AREA :
cPtr = “holding pattern”;
break;
case LANDING_RUNWAY_FLIGHT_AREA :
cPtr = “landing runway”;
break;
case TERMINAL_FLIGHT_AREA :
cPtr = “terminal”;
break;
case TAKE_OFF_RUNWAY_FLIGHT_AREA :
cPtr = “take-off runway”;
break;
}
return(string(cPtr));
}
// PURPOSE: To return the text when a Flight should stay out while it is
// waiting to enter ‘*this’ FlightArea. No parameters.
string getHoldText ()
const
{
const char* cPtr;
switch (type_)
{
case HOLDING_PATTERN_FLIGHT_AREA :
cPtr = “stay in the 20,000 foot holding pattern”;
break;
case LANDING_RUNWAY_FLIGHT_AREA :
cPtr = “stay in the 10,000 foot holding pattern”;
break;
case TERMINAL_FLIGHT_AREA :
cPtr = “stay in one the landing runway”;
break;
case TAKE_OFF_RUNWAY_FLIGHT_AREA :
cPtr = “stay at the terminal”;
break;
}
return(string(cPtr));
}
// PURPOSE: To return the text when a Flight may enter ‘*this’ FlightArea.
// No parameters.
string getEnterText ()
const
{
const char* cPtr;
switch (type_)
{
case HOLDING_PATTERN_FLIGHT_AREA :
cPtr = “enter the holding pattern”;
break;
case LANDING_RUNWAY_FLIGHT_AREA :
cPtr = “land”;
break;
case TERMINAL_FLIGHT_AREA :
cPtr = “taxi to the terminal”;
break;
case TAKE_OFF_RUNWAY_FLIGHT_AREA :
cPtr = “take off”;
break;
}
return(string(cPtr));
}
// PURPOSE: To return the text when a Flight should leave ‘*this’
// FlightArea. No parameters.
string getLeaveText ()
const
{
const char* cPtr;
switch (type_)
{
case HOLDING_PATTERN_FLIGHT_AREA :
cPtr = “make your final approach”;
break;
case LANDING_RUNWAY_FLIGHT_AREA :
cPtr = “clear the runway for other flights”;
break;
case TERMINAL_FLIGHT_AREA :
cPtr = “refuel safely”;
break;
case TAKE_OFF_RUNWAY_FLIGHT_AREA :
cPtr = “have a safe flight”;
break;
}
return(string(cPtr));
}
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To initialize ‘*this’ FlightArea to have type ‘newType’ and
// to be allowed to hold ‘newCapacity’ simultaneous Flight instances. No
// return value.
FlightArea (flightArea_t newType,
int newCapacity
) :
type_(newType),
capacity_(newCapacity),
flightQueue_()
{
// YOUR CODE HERE:
}
// PURPOSE: To release the resources of ‘*this’. No parameters. No return
// value.
~FlightArea ()
{
// YOUR CODE HERE:
}
// V. Accessors:
// PURPOSE: To return ‘true’ if ‘*this’ FlightArea is full with Flight
// instances, or ‘false’ otherwise.
bool isFull ()
const
{
return(flightQueue_.size() == capacity_);
}
// VI. Mutators:
// VII. Methods that do main and misc. work of class:
// PURPOSE: To let Flight ‘flight’ begin to enter ‘*this’ FlightArea. No
// return value.
void enter (const Flight& flight
)
{
// YOUR CODE IN HERE, SOMEWHERE:
while ( isFull() )
{
cout << “Air Traffic Control: \”Flight ” << flight
<< “, the ” << getName() << ” is full. ”
<< “Please ” << getHoldText() << “\”.” << endl;
}
flightQueue_.push(flight);
if (flightQueue_.size() > capacity_)
{
cout << “CRASH! Too many flights in/on/at the ” << getName() << endl;
exit(EXIT_FAILURE);
}
cout << “Air Traffic Control: \”Flight ” << flight << “, please ”
<< getEnterText() << “\”.” << endl;
}
// PURPOSE: To let Flight ‘flight’ leave ‘*this’ FlightArea. No return
// value.
void leave (const Flight& flight
)
{
// YOUR CODE IN HERE, SOMEWHERE:
flightQueue_.pop();
cout << “Air Traffic Control: \”Flight ” << flight << “, please ”
<< getLeaveText() << “\”.” << endl;
}
};
// PURPOSE: To represent an Airport with multiple ‘FlightArea’ instances and
// with the responsibility of servicing multiple ‘Flight’ instances.
class Airport
{
// I. Member vars:
// PURPOSE: To implement the near orbit that Flight instances enter before
// final approach.
FlightArea holdingPattern_;
// PURPOSE: To implement the landing strip.
FlightArea landingRunway_;
// PURPOSE: To implement the airport terminal.
FlightArea terminal_;
// PURPOSE: To implement the taking-off runway.
FlightArea takeOffRunway_;
// II. Disallowed auto-generated methods:
// No copy constructor:
Airport (const Airport&
);
// No copy assignment op:
Airport& operator= (const Airport&
);
protected :
// III. Protected :
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To initialize ‘*this’ Airport to have no Flight instances yet.
// No parameters. No return value.
Airport () :
holdingPattern_(HOLDING_PATTERN_FLIGHT_AREA,3),
landingRunway_(LANDING_RUNWAY_FLIGHT_AREA,1),
terminal_(TERMINAL_FLIGHT_AREA,4),
takeOffRunway_(TAKE_OFF_RUNWAY_FLIGHT_AREA,1)
{ }
// PURPOSE: To release the resources of ‘*this’. No parameters. No return
// value.
~Airport ()
{ }
// V. Accessors:
// VI. Mutators:
// VII. Methods that do main and misc. work of class:
// PURPOSE: To handle Flight ‘flight’ as it approaches, landis, gets
// serviced, and taked off. No return value.
void* doFlight (const Flight* flightPtr
)
{
cout << “Flight ” << *flightPtr << ” approaches the airport.” << endl;
holdingPattern_.enter(*flightPtr);
sleep( (rand() % 6) + 1);
holdingPattern_.leave(*flightPtr);
landingRunway_.enter(*flightPtr);
sleep( (rand() % 2) + 1);
landingRunway_.leave(*flightPtr);
terminal_.enter(*flightPtr);
sleep( (rand() % 8) + 1);
terminal_.leave(*flightPtr);
takeOffRunway_.enter(*flightPtr);
sleep( (rand() % 3) + 1);
takeOffRunway_.leave(*flightPtr);
}
};
// PURPOSE: To be the global variable that implements the airport at which
// all Flight instances must land.
Airport oHare;
// PURPOSE: To make Flight ‘*(Flight*)vPtr’ fly to, land at, and take off
// from, ‘oHare’. Returns ‘NULL’.
void* fly (void* vPtr
)
{
// YOUR CODE HERE:
return(NULL);
}
// PURPOSE: To simulate the ‘NUM_FLIGHTS’ Flight instances being serviced
// by Airport instance ‘oHare’. Ignores command line arguments. Returns
// ‘EXIT_SUCCESS’ to OS.
int main ()
{
// I. Application validity check:
// II. Simulate the approach, landing, servicing, and taking off of several
// flights:
// II.A. Create Flight instances:
Flight flights[NUM_FLIGHTS];
// II.B. Launch threads for Flight instances:
// YOUR CODE HERE:
// II.C. Collect threads for Flight instances:
// YOUR CODE HERE:
// III. Finished:
return(EXIT_SUCCESS);
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more