Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose we have a multithreaded system for handling a ticket vending service. When dealing with the last few remaining tickets, this service attempts to offer

image text in transcribedimage text in transcribedimage text in transcribed

Suppose we have a multithreaded system for handling a ticket vending service. When dealing with the last few remaining tickets, this service attempts to offer each ticket to a user and wait until they complete the purchase of the ticket or decline to purchase it. While this is happening, another user can be waiting for a ticket to become available or for all tickets to be sold out. Suppose, to implement this, the service provides the following functions: bool WaitForNextTicket( ) --- waits for a ticket to be available for purchase or for all tickets to be sold out. Returns true if a ticket is still available for purchase, in which case it is reserved until BuyTicket() or DeclineTicket() is called. Returns false is tickets are sold out. void BuyTicket() --- purchases a ticket void DeclineTicket() --- declines to purchase a ticket so the code that prompts a user for their decision looks something like: if (WaitForNextTicket()) { printf("Purchase ticket? "); if (ReadInputFromUser() == YES) { BuyTicket(); } else { DeclineTicket(); } else { printf("Tickets are sold out "); P Question 1 (1 points): (see above) Suppose these functions are implemented using POSIX semaphores. Of the choices listed below, it is most likely that the WaitForNextTicket() function would call sem_wait on a semaphore whose value represents A. O the total number of tickets already bought B. O the total number of tickets currently reserved for purchase (but not bought or declined) C. O the total number of tickets ever reserved for purchased, regardless of whether eventually bought or declined D. O the total number of tickets available for purchase and not reserved E. O the total number of tickets either available for purchase or currently reserved for purchase (but not bought or declined) Question 2 (1 points): (see above) Suppose these functions are implemented using mutexes and condition variables. The WaitForNextTicket() function would most likely call pthread_cond_wait A. in a loop for as long as there are unsold tickets B. O in a loop for as long as all unsold tickets are awaiting a user's decision to purchase or decline it C. O in a loop for as long as at least one ticket is awaiting a user's decision to purchase or decline it D. O once (without a loop), but only if all unsold tickets are awaiting a user's decision to purchase or decline it E. O once (without a loop), but only if at least one ticket is awaiting a user's decision to purchase or decline it F. O once (without a loop), but only if there are unsold tickets Suppose we have a multithreaded system for handling a ticket vending service. When dealing with the last few remaining tickets, this service attempts to offer each ticket to a user and wait until they complete the purchase of the ticket or decline to purchase it. While this is happening, another user can be waiting for a ticket to become available or for all tickets to be sold out. Suppose, to implement this, the service provides the following functions: bool WaitForNextTicket( ) --- waits for a ticket to be available for purchase or for all tickets to be sold out. Returns true if a ticket is still available for purchase, in which case it is reserved until BuyTicket() or DeclineTicket() is called. Returns false is tickets are sold out. void BuyTicket() --- purchases a ticket void DeclineTicket() --- declines to purchase a ticket so the code that prompts a user for their decision looks something like: if (WaitForNextTicket()) { printf("Purchase ticket? "); if (ReadInputFromUser() == YES) { BuyTicket(); } else { DeclineTicket(); } else { printf("Tickets are sold out "); P Question 1 (1 points): (see above) Suppose these functions are implemented using POSIX semaphores. Of the choices listed below, it is most likely that the WaitForNextTicket() function would call sem_wait on a semaphore whose value represents A. O the total number of tickets already bought B. O the total number of tickets currently reserved for purchase (but not bought or declined) C. O the total number of tickets ever reserved for purchased, regardless of whether eventually bought or declined D. O the total number of tickets available for purchase and not reserved E. O the total number of tickets either available for purchase or currently reserved for purchase (but not bought or declined) Question 2 (1 points): (see above) Suppose these functions are implemented using mutexes and condition variables. The WaitForNextTicket() function would most likely call pthread_cond_wait A. in a loop for as long as there are unsold tickets B. O in a loop for as long as all unsold tickets are awaiting a user's decision to purchase or decline it C. O in a loop for as long as at least one ticket is awaiting a user's decision to purchase or decline it D. O once (without a loop), but only if all unsold tickets are awaiting a user's decision to purchase or decline it E. O once (without a loop), but only if at least one ticket is awaiting a user's decision to purchase or decline it F. O once (without a loop), but only if there are unsold tickets

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

Students also viewed these Databases questions

Question

How do the two components of this theory work together?

Answered: 1 week ago