Operating Systems / Chapter 5: Parallel Processes /

Race conditions

Connecting computers together on a network gives us amazing new powers. For example, a person in Spain can have video calls with another person in Japan. We can buy groceries from home. Someone in Brazil can play a videogame together with someone in Mexico. The list goes on and on.

But networked computing brings a unique set of challenges as well. We incur new problems we never had to think about with 1 computer.

Let's look at a simple example. Suppose that John is trying to buy a phone at an e-commerce website called InterMart. John searches for the phone and, luckily, they have 1 phone left. So John clicks the buy button. He is taken to the "ordering" page, where he now needs to enter his address and credit card information to complete the order. Unluckily, Stacey, John's neighbor across the street, is also trying to buy the same phone at the same website. Stacey also sees that there is 1 phone left (John has not completed the ordering process yet). Stacey then also starts the ordering process.

John is in a race with Stacey to claim the last phone.

Suppose that Stacey finished ordering the phone before John did. Who gets the phone? Should John get the phone since he starting ordering first? Or should Stacey get it since she entered her information first?

The answer depends on the InterMart website policy. For example, if the policy is to send the phone to the first successful credit card charge, then Stacey would win. Or perhaps InterMart will give the phone to John as long as he completes his order in a reasonable amount of time. If InterMart is not careful, it may even charge both John and Stacey before realizing it can only complete 1 of the orders! Note that InterMart is not necessarily trying to be evil; there just aren't many good options when 2 people want 1 phone.

Suppose that InterMart decides to give preference to the customer that start the ordering process first. But we also don't want other customer to have a bad experience. In the example above, once John starts ordering, we don't want Stacey to waste time going through the ordering process only to find she isn't getting the phone.

There are many ways to do this, but one way os to reserve or hold the phone while John is ordering. This is common in ticketing websites: when we try to buy concert tickets, the website will usually give you some time, maybe 10 minutes, to complete the order. When your neighbor tries to view the ticket, the website will tell them there are no tickets available.

Now while John is ordering, InterMart guarantees not to sell the phone to someone else. But there is a time limit because they cannot hold the phone for John forever. For example, maybe John gets an emergency phone call and forgets about the phone order. Then InterMart should give someone else a chance to order the phone.

Quiz: Check All That Apply (1 point)

When both John and Stacey try to order the phone at InterMart, please check all the possible outcomes from below:

   
   
   
   
Become a subscriber to save your progress, see the correct answer, and more!
Previous Lesson Next Lesson

Comments

Please log in to add comments