Question: Objective: Implement a program that uses multiple threads Instructions: A theater has a total of 1 0 0 seats. People are trying to book tickets

Objective:
Implement a program that uses multiple threads
Instructions:
A theater has a total of 100 seats. People are trying to book tickets simultaneously from different counters (threads).
Each thread will attempt to book a ticket for a person.
Your task is to simulate the ticket booking process using threads, ensuring that no two threads can book the same seat at the same time (i.e., avoiding race conditions).
You will implement the following:
Booking Counter Thread: Each thread represents a booking counter, where a person tries to book a ticket.
Synchronization: Use locks to ensure that each seat is booked by only one person.
Shared Data: The total number of seats is shared among the threads, and this shared data should be accessed in a thread-safe way.
Result: Print the seat number each person booked. If no seats are available, print that the booking failed.
Tasks:
Step 1: (2 marks)
Import Required Modules You need to import the necessary modules for multithreading. Use the threading module to create threads and time for simulating delays.
Step 2(5 marks):
Define a TicketBooking Class Create a TicketBooking class to represent the ticket booking system. This class will have:
A shared variable for the number of available seats (self.available_seats).A Lock object to control access to the shared data.A method book_seat(self, counter_id) that each thread will call to attempt booking a seat.
Step 3: (8 marks)
Implement the book_seat Method The book_seat method should:
Acquire the lock to ensure thread-safe access to shared data.Check if there are any available seats left.If seats are available, decrement the seat count and print the seat number.Release the lock after updating shared data.
Step 4: (3 marks)
Create and Start Threads Write a function to create multiple threads (representing booking counters) and start them. Each thread should call the book_seat method.
Step 5: (2 marks)
Wait for Threads to Complete Ensure that the main program waits for all threads to finish before exiting.
Example Output:
Counter 1 booked seat 1
Counter 2 booked seat 2
Counter 3 booked seat 3
...
Counter 99 booked seat 99
Counter 100 booked seat 100
No seats available for Counter 101
No seats available for Counter 102
Use the attached .py file as your starter code. Complete the code as outlined above.
class TicketBooking:
pass
def main():
# define max seats
# instantiate ticket booking instance
# create a list to hold all the threads
threads =[]
# Create 110 threads simulating 110 counters
# Wait for all threads to finish
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!