I have twenty minutes between computer science classes for lunch. I am a CPU looking to make a turkey and cheese sandwich as fast as possible, so that I have enough time to eat.

Sequential

I could use the following procedure to make a sandwich:

  1. Get out bread and put two pieces in the toaster
  2. Wait for the toaster to finish
  3. Remove bread from toaster
  4. Get out turkey, cheese, mustard, and olive tapenade
  5. Spread olive tapenade one piece of bread
  6. Spread mustard on the other
  7. Remove two pieces of turkey from the package
  8. Put them on a slice of bread
  9. Remove a slice of muenster cheese from the package
  10. Put it on top of the turkey
  11. Close sandwich

This procedure is fine, and it definitely results in a tasty sandwich. But can I finish this task in less time?

Timeline of sequential execution

Timeline of sequential execution

Concurrent

As a CPU, I can only do one thing at a time, but the hardware I have access to can do some things without my oversight or engagement. Specifically, my toaster will count down on its own and send me an interrupt when it finishes. In my sequential procedure, I wasted time by sitting around waiting for the toaster to finish. Instead of doing nothing during that time, I could use that time to my advantage.

My new process looks like this:

Sub-task A

  1. Get out bread and put two pieces in the toaster
  2. Switch to Sub-task B
  3. When toaster is done, come back to Sub-task A (this one) and remove the bread from the toaster
  4. Spread olive tapenade one piece of bread
  5. Spread mustard on the other
  6. Finish Sub-task B, if it isn’t already finished.
  7. Put turkey and cheese one piece of bread
  8. Close sandwich

Sub-task B

  1. Get out turkey, cheese, mustard, and olive tapenade
  2. Remove two pieces of turkey from the package
  3. Remove a slice of muenster cheese from the package
  4. Put it on top of the turkey

Sub-task B takes a little bit of time, because the turkey and cheese packages are fiddly, and the turkey especially requires attention to make sure I’m not ripping the slices into pieces.

Because Sub-task A voluntarily lets me (the CPU) take a break from it to switch to my other task, I don’t waste any time sitting around. And because Sub-task B can be interrupted by the toaster finishing, I’ll be able to respond to the toaster as soon as it finishes, even if I haven’t finished with Sub-task B.

Timeline of concurrent execution of two processes

Timeline of concurrent execution of two processes

Parallel?

I’d like to be able to parallelize this task, but I think the metaphor breaks down here. I’d need a clone or twin of myself to work alongside me at the same time, and I’d need to be able to divide the task into parts that can be worked on simultaneously. I suppose my twin and I could each handle spreading one of the spreads onto one piece of bread, but other than that it seems there is very little that can be done in parallel.

Maybe since there would be two of us, we would want to make two sandwiches…


Shoutout to my Operating Systems professor :)