You are to design and write a program which simulates the following travel agent
ID: 3620840 • Letter: Y
Question
You are to design and write a program which simulates the following travel agent booking problem:Air SN has just instituted flights, on a Saab XJK turbo-prop seating 36 passengers, from Boston to Halifax and return. Consider Air SN's flight 888 which departs Halifax non-stop for Boston at 8:08 am on October 18. Passengers may book (and cancel) seats on this flight by contacting any recognized travel agent.
The basic requirements for each program are:
a) create 6 concurrent threads, with each thread representing a travel agent.
b) create a (shared) integer variable seats_available - which will represent the number of seats (still) available on the flight (and thus hence initialize it to 36).
c) each thread should read seats_available, then print both the current value of seats_available and the thread's ID. Each thread should then either increment (indicating that a passenger is cancelling his/her seat) or decrement (indicating that a seat is being booked) seats_available by 1. For this simulation, it is assumed that the ratio of seat bookings to cancellations is 3:1. i.e., 75% of the time a thread (i.e. travel agent acting on a passenger's instructions) will book a seat and 25% of the time will cancel a seat.
d) each thread repeats c) until seats_available reaches 0 (i.e. the flight is sold out - Air SN is determined to make a mint on this routing!!), at which point it terminates.
e) the program terminates when all threads come to an end.
For the program Use synchronization facilities from the pthreads package. You are NOT allowed to use any synchronization mechanism not in pthreads.
Program:
is to illustrate uncontrolled concurrent access to the shared resource, seats_available.
Compile & run your program (several times) & see if more than 1 thread prints the same value of seats_available, or if other 'strange' happenings occur. If not, make a thread relinquish the CPU just
2
before decrementing / incrementing seats_available, and introduce a random delay when a thread regains the CPU. Adjust the delays until some value is printed more than once.
Hand-in: Your assignment report should include: commented source code; at least 2 sample runs annotated with explanations as to why you got those 'strange' results which you did. Also include a discussion of why the 2 runs are different in output. **NOTE:** If you can answer the question but it is about to expire..leave a comment and I will repost the question and give you the 350 points for your answer
Hand-in: Your assignment report should include: commented source code; at least 2 sample runs annotated with explanations as to why you got those 'strange' results which you did. Also include a discussion of why the 2 runs are different in output. **NOTE:** If you can answer the question but it is about to expire..leave a comment and I will repost the question and give you the 350 points for your answer
Explanation / Answer
a) You need to call pthread_create six times to create each of these 6 threads. Create a function called myThread and then pass that function into pthread_create. See the example at http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html b) Declare the integer outside of all the functions in your C source file. This will make it shared. Make sure you use a mutex to "lock" the integer when it is accessed by one of your threads. The same URL as above has an example of using mutexes. c) Make sure you use a mutex before printing, because calls to printf are NOT thread-safe. d) You should put a while loop in your thread function to repeatedly test for this condition. Again, use a mutex to lock the variable when accessing it. e) Use pthread_join in your main function, after your calls to pthread_create, to wait for your threads to finish. If you have any further questions please feel free to follow up.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.