Jonathan's thoughts about just about anything

Month: August 2014

So, I’m sure the thing everyone is wondering right now is (I mean, assuming you read the title): “What do operating systems and food trucks have to do with each other?”. My personal comprehensive list is one item long: “They are both in the title of this blog post”. I’m sure you are now wondering why I chose to put them in the title, and that’s because the rest of this post is about to cover applying the language of operating systems to working in a food truck (and by that, I mean the sausage trucks you see at fairs). Why am I choosing to talk about that? Earlier today, I was volunteering at a sausage truck with a Kiwanis club and I kept catching myself using operating system terminology or thinking about the work in terms of different concepts in the field. For those of you familiar with O/S terms, I do understand I am about to make a couple bad mappings, the goal of this post is more to talk about how the terms could be used in everyday speech (which, in my opinion, is one of the best parts of having O/S knowledge)

So, today, after a bit of shuffling around, I ended up working the pretzel station in the food truck. This entailed taking some pretzels, putting them in the microwave for a few minutes, taking them out of the microwave, covering them with salt, placing them in the display, and repeating. Here, I’ll draw a pretty ascii picture for you. My guestimates about times are included as well (some of these are very wrong by the way).

So, what you see here is a highly serial process. Each task is completed before any other task is started, and only one task is going on at any single time. What you see here is also an incredibly inefficient process and there are a lot of places to improve it. By the way, to start with, let’s say that we are making three pretzels at a time, and it’s taking about four minutes to do that (so ~1:20/pretzel).

Approaching the problem of making this more efficient from a systems perspective, the first thing we want to do is look for resource usage. Here, we can identify a couple major resources: the microwave and the salt. Here’s a nice picture of when these resources are being used at each step of the process.

Looking at this chart, it’s pretty clear that there is a big portion of the time that we are using the microwave, and a big portion of the time we are using the salt, and really not much overlap between those times. There are a couple ways we could approach this problem from an O/S perspective but I’m going to take us out of the picture, assume that everything is being controlled by a robot, and introduce a really awesome concept: pipelines!

So, the idea of a pipeline is that you have a number of resources and while each resource can only be doing on thing at a time, you can have multiple resources working at the same time. One way to think about this in the real world is doing laundry (with only one washer and dryer but with multiple loads): you can load the next load of clothing into the washer before the first load is done in the dryer. For us, this means that we can do the salting at the same time as the microwaving is happening for another batch of pretzels! A diagram of this is below, with all the microwave stuff having been condensed to one step for simplicity.

| microwave | salt |
| microwave | salt |
| microwave | salt |

In this graph, it takes us three minutes to finish one step of the pipeline once the pipeline is in its steady state (the period after warm-up (the beginning, where only microwaving is going on) and before cool-down (the end, where only the salting is going on)) which takes us down to only one minute per pretzel!

Now, the above metaphor of pipelining is actually kind of weak, and that’s because I’m leaving out a pretty significant detail: the person. As a person, one is required to be doing work during most of these steps as well. That graph above? It actually will not work because the first twenty seconds (and last ten seconds) of the microwave step require a person to actively be working as does the entire salting process (the timing ends up still working, but only because salting takes less time). To really get a better picture of this, I’m going to talk about another cool operating systems concept: threads.

In an operating system, a thread is a way to abstract the fact that a computer processor can really only be doing one thing at a time, but we expect our computer to be doing a lot of different things at once. We would all be pretty upset if our phone completely froze up as we tried to load a website and we could not go send a snapchat at the same time. It’s a way for the computer to deal with all the waiting it has to do while doing I/O (downloading that website is mostly just waiting for the data to arrive). Internally, the computer ends up constantly switching between threads constantly so that it is “always” working on something, the same way we multi-task (except it is WAY better at threading than we are at multi-tasking). To think about preparing pretzels in terms of threading, we look at ourselves as the process and consider what thread we are going to work on (i.e. what task we are going to do). For the below, I’m going to label items in ten-second interval for what we are working on at any given time, with each new set of pretzels being on a new line. “M” is the times we are using the microwave, “S” is anything where we are using the salt dish, and “-” is anytime the food is in the microwave and we are not doing anything with it.

I’m going to condense this down to one line, removing the “-“s since they do not make sense at a global level.

0 1 2 3 4 5 6 7 8
MMSSSSSS MMMSSSSSS MMMSSSSSS M...

Looking at this, we can see that there is a huge chunk of time that no work is being done (50% of the time actually (not on that graph, but looking at a steady state)), that’s because we are blocking on the microwave. Whenever a computer scientist says blocking (not sure about everyone else, but this is probably the term I use most often in everyday speech), it means that something is not able to get work done because some other resource is not available. In this case, we can’t start another set of pretzels because the microwave is in use. When that happens, all you really can do is wait for other work to come up. In the case of the sausage truck, I was right next to where they were grilling the sausages so I could use the time to move uncooked knackwursts into a pot of hot water. That entire process (getting a bag, opening it, separating the sausages, changing gloves since I just touched raw meat) ends up taking about a minute and a half, which gives us the following time graph:

As you can see, that shows 100% CPU (i.e. me) utilization, an awesome goal to reach in many cases (in computing, there are many times where you end up with very low CPU utilization because most of the time is spent waiting to read/write things to disks/the network). Thinking back to our pretzels, we still are still only turning them out at a rate of 1/minute, and I can assure you that people at a fair order at a higher rate than that. In computing, the normal way to deal with this is to throw more resources (i.e. money) at the problem, and the Kiwanians chose to do much the same thing adding another human, microwave, and salt tray at the other end of the sausage truck (in computing, we’d also refer to having multiple CPUs (people) doing the same task as adding parallelism). This brings the time per pretzel down to thirty seconds and gives you the following nice graph (yeah, I have no idea what the person at the other end of the truck was doing with the extra time).

There are a bunch of other ways you could look at this from an operating systems standpoint (many of them proving a much better mapping than what I chose to go with), as with many of the mudane tasks we do on a daily basis. Being a computer scientist gives you a lot of new ways to talk about doing work…the only downside to that is you sound like you are just speaking gibberish in many crowds.