Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to run this and have the following output: [Create a test file so I get the same as the picture output, and how to

How to run this and have the following output: [Create a test file so I get the same as the picture output, and how to compile it]

Q:

A runway of airplanes at an airport. The runway can have only one plane on it at one time. There are two types of planes: planes taking off and planes landing. For each plane that takes off, one must land. As well, planes must take off in the order in which they enter the takeoff queue. Planes must land in the order in which they enter the landing queue. Use semaphores to design a synchronization scheme with some properties.

Code:

//Include the needed header files #include #include #include #include

//Structure typedef struct { //Variables int planeID; pthread_t planeTD; } planes_thread;

//Semaphores sem_t runwaySemaphore; sem_t queueSemaphore; sem_t queueMutex;

//Method planeLand() static void planeLand(int planeID) { //Display printf("[PLANE_TO_LAND]: Airplane %d asks permission to land ", planeID); //Semaphore wait sem_wait(&runwaySemaphore); //Display printf("[PLANE_LANDED]: ===> Airplane %d has landed ", planeID); }

//Method planeQueue() static void planeQueue(int planeID) { //Variable int sValue; //Display printf("[PLANE_LANDED]: Airplane %d asks permission to go to gates ", planeID); //Semaphore wait sem_wait(&queueSemaphore); //Semaphore get value sem_getvalue(&queueSemaphore, &sValue); //Check condition if (sValue != 0) { //Semaphore wait sem_wait(&queueMutex); } //Otherwise else { //Semaphore sem_post(&queueMutex); //Semaphore sem_post(&queueMutex); } //Semaphore sem_post(&runwaySemaphore); //Display printf("[PLANE_TO_GATE]: ===> Airplane %d did proceed to gate: %d ", planeID, 3 - sValue); //Semaphore sem_post(&queueSemaphore); }

//Method planeFunction() static void* planeFunction(void* args) { //New planes_thread* p = (planes_thread*)args; //Function call planeLand(p->planeID); //Function call planeQueue(p->planeID); //Return return NULL; }

//Method usage static void usage(char** argv) { //Display fprintf(stderr, "Usage: %s number_of_planes number_of_runways ", argv[0]); }

//Driver int main(int argc, char* argv[]) { //Initialize needed variables int nVal = 0, mVal = 0, index, tStatus; planes_thread* planes; //Check condition if (argc != 3) { //Function call usage(argv); //Exit exit(EXIT_FAILURE); } //Assign nVal = atoi(argv[1]); //Assign mVal = atoi(argv[2]); //Check condition if (nVal

Output:

image text in transcribed

PLANE TO LAND]: Airplane 1 asks permission to land [PLANE_LANDED]: Airplane 1 has landed [PLANE LANDED]: Airplane 1 asks permission to go to gates [PLANE TO LAND]: Airplane 2 asks permission to land [PLANE_LANDED]:Airplane 2 has landed [PLANE LANDED]: Airplane 2 asks permission to go to gates [PLANE_TO LAND]:Airplane 5 asks permission to land [PLANE-LANDED]: ="> Airplane 5 has landed [PLANE LANDED]: Airplane 5 asks permission to go to gates [PLANE-TO-GATE): ===> Airplane I did proceed to gate: 1 [PLANE TO GATE]:Airplane 5 did proceed to gate: 3 [PLANE-TO-GATE]: ===> Airplane 2 did proceed to gate: 2 [PLANE TO LAND]: Airplane 4 asks permission to land [PLANE_LANDED]: Airplane 4 has landed [PLANE LANDED]: Airplane 4 asks permission to go to gates [PLANE_TO LAND]: Airplane 3 asks permission to land [PLANE LANDED]: Airplane 3 has landed [PLANE LANDED]: Airplane 3 asks permission to go to gates [PLANE TO LAND]: Airplane 6 asks permission to land [PLANE LANDED]: Airplane 6 has landed [PLANE LANDED]: Airplane 6 asks permission to go to gates [PLANE-TO-GATE): "=> Airplane 6 did proceed to gate: 3 [PLANE TO GATE]:Airplane 4 did proceed to gate: 1 [PLANE-TO-GATE): "=> Airplane 3 did proceed to gate: 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago