Latest / CyberSecurity Summary / Operating Systems: Internals and Design Principles
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. You know, I was
- 0:02Speaker 1: looking at the stack of notes for today we are
- 0:05Speaker 1: tackling William Stallings's massive text Operating Systems, internals and design principles,
- 0:13Speaker 1: and it really hit me. We spend all this money
- 0:16Speaker 1: on faster processors, better ram but the thing that actually
- 0:20Speaker 1: decides if we have a good experience is this invisible
- 0:24Speaker 1: layer we almost never talk about.
- 0:26Speaker 2: It's the ghost in the machine, literally.
- 0:28Speaker 1: And Stallings makes this comparison right out of the gate
- 0:30Speaker 1: that to stop me. He doesn't compare the operating system
- 0:33Speaker 1: to a brain or you know, an engine. He compares
- 0:36Speaker 1: it to a.
- 0:36Speaker 2: Government, which, depending on your political views, might make you
- 0:40Speaker 2: a little nervous about turning on your laptop.
- 0:41Speaker 1: It definitely gives you pause. But when you dig into
- 0:44Speaker 1: the book the analogy it fits surprisingly well. The OS
- 0:48Speaker 1: doesn't actually produce anything, does it.
- 0:50Speaker 2: No, it doesn't write your email or edit your photo.
- 0:52Speaker 2: It just it creates the environment where those things can happen.
- 0:56Speaker 2: That's the core function. It paves the roads, polices the intersectctions,
- 1:00Speaker 2: and it collects taxes in the form of memory and
- 1:03Speaker 2: processing power.
- 1:04Speaker 1: And like a government, it's constantly fighting this sort of
- 1:08Speaker 1: three way war with itself. Stallings lays these out really clearly. Convenience, efficiency,
- 1:15Speaker 1: and the ability to evolve, and.
- 1:17Speaker 2: Those three things do not naturally go together.
- 1:20Speaker 1: Not at all. In fact, they seem like they would
- 1:22Speaker 1: actively sabotage each other.
- 1:23Speaker 2: They absolutely do. I mean, think about convenience. That's you,
- 1:27Speaker 2: the user. You want a beautiful desktop, you want transparent windows,
- 1:31Speaker 2: plug and plate devices. You do not want to see
- 1:33Speaker 2: the wiring.
- 1:34Speaker 1: I definitely do not want to see the wiring. I
- 1:35Speaker 1: just want to click the button and have it work.
- 1:37Speaker 2: But hiding the wiring is expensive. Yeah, that eats up
- 1:41Speaker 2: system resources, which fights against objective number two efficiency. The
- 1:46Speaker 2: hardware just wants to run raw code as fast as possible,
- 1:49Speaker 2: not paint pretty pictures for you. Every cycle spent drawing
- 1:53Speaker 2: a drop shadow on a window is a cycle not
- 1:56Speaker 2: spent calculating a spreadsheet.
- 1:58Speaker 1: So the OS is basically stuck in the middle trying
- 2:00Speaker 1: to make me happy without making the CPU quit in frustration, while.
- 2:03Speaker 2: Also handling objective three evolution. It has to be built
- 2:08Speaker 2: in a way that allows it to change completely next
- 2:11Speaker 2: year without breaking all the software you bought five years ago.
- 2:14Speaker 2: It's a juggling act.
- 2:15Speaker 1: An unbelievable juggling.
- 2:17Speaker 2: App billions of times a second.
- 2:19Speaker 1: So let's get into the mechanics of this. Stallings breaks
- 2:22Speaker 1: it down to the instruction cycle. Fetch, execute, fetch execute.
- 2:27Speaker 1: It sounds so simple.
- 2:28Speaker 2: It is. It's the heartbeat of the machine. But there's
- 2:30Speaker 2: a massive physiological problem with this computer body. Okay, the brain,
- 2:36Speaker 2: the CPU is moving at the speed of light, but
- 2:38Speaker 2: the hands and feet, the input output devices like your
- 2:41Speaker 2: hard drive or your Wi Fi card are moving through molasses.
- 2:45Speaker 1: Stalling's had a stat on this that just blew my mind.
- 2:47Speaker 1: The speed difference isn't a little bit, it's orders of magnitude.
- 2:50Speaker 2: It's astronomical. If the CPU's a Ferrari driving it at
- 2:53Speaker 2: two hundred miles an hour, the hard drive is a tractor. No,
- 2:56Speaker 2: that gives a hard drive too much credit.
- 2:57Speaker 1: It's a snail.
- 2:58Speaker 2: It's a snail exactly.
- 2:59Speaker 1: So you didn't have a smart operating system and you
- 3:02Speaker 1: told the computer to save file that Ferrari effectively has
- 3:06Speaker 1: to slam on the brakes and just idle for what
- 3:09Speaker 1: feels like one hundred years while the snail writes the data.
- 3:12Speaker 2: That's the polling method. CPU just sits there, asking the
- 3:15Speaker 2: drive Are you done yet? Are you done yet? Are
- 3:18Speaker 2: you done yet?
- 3:18Speaker 1: What a waste.
- 3:19Speaker 2: It's a massive waste, a billion dollar hardware. You have
- 3:22Speaker 2: the world's smartest mathematician sitting there waiting for a printer
- 3:25Speaker 2: to warm up.
- 3:26Speaker 1: So they invented interrupts. And I love this term because
- 3:30Speaker 1: it sounds rude, but it's actually the most polite thing
- 3:33Speaker 1: the hardware can do.
- 3:34Speaker 2: It's save computing with interrupts. The Ferrari sends the save
- 3:38Speaker 2: command to the snail and then immediately peels out and
- 3:41Speaker 2: goes to do something else.
- 3:42Speaker 1: Okay, so it goes off and runs a spreadsheet, plays music, whatever.
- 3:46Speaker 1: But wait, how does it know when the snail is done?
- 3:48Speaker 2: The snail has a direct line to the CPU. When
- 3:51Speaker 2: it's finished, it sends an electrical signal and interrupt. It
- 3:55Speaker 2: forces the CPU to pause whatever it's doing to acknowledge
- 3:59Speaker 2: that the task is complete.
- 4:00Speaker 1: See that's the part that stresses me out. If I'm
- 4:02Speaker 1: in the middle of a complex math problem and you
- 4:04Speaker 1: interrupt me, I lose my place. I have to start over.
- 4:08Speaker 2: And that is the magic trick. Yeah, the OS performs
- 4:11Speaker 2: a context switch. Okay, imagine you're reading a really dense
- 4:14Speaker 2: novel and the doorbell rings, You don't just throw the
- 4:17Speaker 2: book on the floor, right, you put a bookmark in.
- 4:19Speaker 2: You write down exactly which line you were on, maybe
- 4:22Speaker 2: even the thought you were having in that split second.
- 4:24Speaker 1: So that's the state of the process.
- 4:26Speaker 2: Correct, the program counter, the register values. Everything gets pushed
- 4:29Speaker 2: onto a control stack. It's a snapshot of reality frozen
- 4:33Speaker 2: in time. Wow, you go answer the door, handle the
- 4:37Speaker 2: big job, and when you come back, you pop that
- 4:40Speaker 2: snapshot back off the stack. The software doesn't even know
- 4:43Speaker 2: it was passed, so.
- 4:44Speaker 1: To me, it looks like everything happened at once exactly.
- 4:46Speaker 2: It completely decouples the user's perception of time from the
- 4:50Speaker 2: computer's reality.
- 4:51Speaker 1: But what happens if the doorbell rings while I'm already
- 4:53Speaker 1: on the phone?
- 4:54Speaker 2: Ah, the nightmare scenario multiple.
- 4:57Speaker 1: Interrupts, is the system just crash?
- 5:00Speaker 2: Depends on how it's designed. You can disable interrupts basically
- 5:03Speaker 2: take the phone off the hook while you answer the door,
- 5:06Speaker 2: or you can have nested interrupts.
- 5:08Speaker 1: Which implies a hierarchy. Some things are more important.
- 5:10Speaker 2: Precisely, if your network card says incoming data that's urgent.
- 5:15Speaker 2: If your printer says I'm out of paper, that can
- 5:18Speaker 2: wait The OS prioritizes the urgent interrupt handles it then
- 5:22Speaker 2: goes back to the less urgent one, and finally back
- 5:24Speaker 2: to your program.
- 5:25Speaker 1: And all of this happens so fast that I just
- 5:26Speaker 1: see a smooth mouse cursor.
- 5:28Speaker 2: Well underneath, it's just controlled chaos.
- 5:30Speaker 1: So we have the CPU maximizing its time with interrupts.
- 5:34Speaker 1: But there's another bottleneck, right memory. We all want terabytes
- 5:39Speaker 1: of storage that's instantly accessible. But Stallings points out the
- 5:43Speaker 1: iron triangle of memory capacity, speed, and cost.
- 5:46Speaker 2: And you can pick two because you cannot have all three.
- 5:49Speaker 2: Fast memory like the registers right inside the CPU, is
- 5:53Speaker 2: incredibly expensive and tiny. Big memory like your hard drive
- 5:57Speaker 2: is cheap but slow.
- 5:58Speaker 1: So the OS has to lie to us again. It
- 6:01Speaker 1: has to create the illusion that we have unlimited, super
- 6:03Speaker 1: fast memory.
- 6:04Speaker 2: And it does that through the memory hierarchy. It's a pyramid.
- 6:08Speaker 2: Data is constantly moving up and down. But the secret sauce,
- 6:12Speaker 2: the thing that makes the lie convincing is this concept
- 6:15Speaker 2: of locality of reference.
- 6:17Speaker 1: This is essentially the OS gambling right. It's betting on
- 6:20Speaker 1: what I'm going to do next.
- 6:21Speaker 2: It's a rigorous statistical gamble. They are two types. First,
- 6:25Speaker 2: temporal locality Okay, if you looked at a photo five
- 6:29Speaker 2: seconds ago, the odds are really high you'll look at
- 6:32Speaker 2: it again in the next five seconds. Think about a
- 6:33Speaker 2: loop and a piece of code.
- 6:35Speaker 1: It's the same instructions over and over, so you keep
- 6:37Speaker 1: that data close exactly.
- 6:39Speaker 2: And then there's spatial locality. If you're reading a document
- 6:41Speaker 2: at line ten, there is a ninety nine percent chance
- 6:44Speaker 2: you're going to need line eleven next.
- 6:46Speaker 1: You almost never jump from line ten to line five thousand.
- 6:49Speaker 2: Right, So the OSCs you're reading line ten and quietly
- 6:53Speaker 2: in the background, it runs down to the slow warehouse,
- 6:56Speaker 2: grabs lines eleven through twenty and puts them on the
- 6:59Speaker 2: fast on the top, right next to the CPU that's cashing.
- 7:03Speaker 2: That's cashing, and it works surprisingly well. A well toned
- 7:06Speaker 2: o S can get a hit on that cash maybe
- 7:08Speaker 2: ninety percent of the.
- 7:09Speaker 1: Time, which explains why when I open a huge app
- 7:12Speaker 1: for the very first time, it sometimes chugs for a second.
- 7:15Speaker 2: That's a miss, right. The chef reached for the salt,
- 7:18Speaker 2: but it wasn't on the counter. He had to run
- 7:20Speaker 2: to the pantry. But once he brings it out, it
- 7:23Speaker 2: stays on the counter. It stays on the counter. The
- 7:25Speaker 2: next time you click. It's instant.
- 7:26Speaker 1: It's fascinating to think about this because it means my
- 7:29Speaker 1: computer is constantly trying to predict the future based on
- 7:32Speaker 1: the immediate past.
- 7:33Speaker 2: And when a guess is right, you feel like you
- 7:35Speaker 2: have a supercomputer. When a guess is wrong, you get
- 7:38Speaker 2: the spinning wheel of death.
- 7:40Speaker 1: It's so easy to take this for granted. Now we
- 7:42Speaker 1: have casing, we have interrupts. Everything feels instant. But Stallings
- 7:47Speaker 1: takes us back to the nineteen forties and fifties to
- 7:49Speaker 1: show us the dark Ages.
- 7:51Speaker 2: It really was the dark Ages. No operating system, no mouse,
- 7:55Speaker 2: no screen. It's a clipboard, a literal clipboard outside the
- 7:59Speaker 2: computer room. You signed up for a thirty minute block.
- 8:02Speaker 2: You walked in with your deck of punch cards. You
- 8:04Speaker 2: loaded them into the machine, and you prayed.
- 8:06Speaker 1: And if you had a typo, a light.
- 8:08Speaker 2: Flashed, the machine stopped, and your thirty minutes were up.
- 8:12Speaker 2: You had to go back to your desk and debug
- 8:14Speaker 2: by just staring at holes in paper.
- 8:17Speaker 1: The inefficiency is painful to even think about. While you
- 8:20Speaker 1: were walking around loading cards, checking the lights, this million
- 8:24Speaker 1: dollar machine was just sitting.
- 8:25Speaker 2: There burning electricity, doing nothing. That idle time is what
- 8:30Speaker 2: drove the invention of the OS. It wasn't about user comfort,
- 8:33Speaker 2: it was about money. They needed batch processing to keep
- 8:36Speaker 2: the machine.
- 8:37Speaker 1: Fed, and this is where we see the first monitor programs.
- 8:40Speaker 2: Yes, you'd hand your cards to an operator. The operator
- 8:42Speaker 2: fed them to the machine one after another. The monitor
- 8:45Speaker 2: was a simple program that just said Okay, job one
- 8:47Speaker 2: is done, load job two.
- 8:49Speaker 1: But even then, if a job had to wait for
- 8:51Speaker 1: a tape to rewind, the CPU starped again.
- 8:54Speaker 2: Which led to the biggest leap inefficiency multi programming, the
- 8:58Speaker 2: idea that you could hold multiple job and memory at once.
- 9:01Speaker 1: So if job A waits for a.
- 9:03Speaker 2: Tape, the OS switches to Joe B.
- 9:05Speaker 1: Stallings mentions that uniprogramming had something like what three percent
- 9:09Speaker 1: CPU utilization? Multiprogramming just blew that away.
- 9:12Speaker 2: It changed the economics of computing, but it was still impersonal.
- 9:16Speaker 2: You submitted a job and came back hours later for
- 9:19Speaker 2: the print.
- 9:19Speaker 1: Out, which brings us to the moment the computer became
- 9:22Speaker 1: personal time sharing.
- 9:24Speaker 2: This is the MIT system CTSS. Before this, the goal
- 9:28Speaker 2: was always maximized the CPU. With time sharing, the goal
- 9:32Speaker 2: shifted to maximize the user.
- 9:34Speaker 1: And this is where we get the concept of time
- 9:36Speaker 1: slicing right.
- 9:37Speaker 2: Yes. Imagine a chess master playing fifty games at once.
- 9:40Speaker 2: He walks to board one, makes a move, walks to
- 9:43Speaker 2: board two, makes a.
- 9:44Speaker 1: Move, and if he moves fast enough.
- 9:46Speaker 2: Every player feels like they have his full attention.
- 9:49Speaker 1: So the OS is the chess master. It runs my
- 9:51Speaker 1: browser for a fraction of a second, then switches to
- 9:54Speaker 1: your music player, then the system clock and back to
- 9:56Speaker 1: my browser.
- 9:57Speaker 2: Every point two seconds in those early systems. Now it's
- 10:00Speaker 2: much much faster. It creates the illusion of concurrency.
- 10:03Speaker 1: You think you're multitasking, but the computer is actually just
- 10:06Speaker 1: panic switching.
- 10:07Speaker 2: Panic switching between tasks fast enough to fool your eyes.
- 10:10Speaker 1: This required a huge leap in software architecture, though Stallings
- 10:14Speaker 1: really emphasizes the concept of the process.
- 10:17Speaker 2: It sounds abstract, but it's so vital. A program is
- 10:21Speaker 2: just a static set of instructions on a disc. It's dead.
- 10:25Speaker 2: A process is that program in execution. It's alive.
- 10:28Speaker 1: It has baggage, it has.
- 10:29Speaker 2: Context, it has ownership of memory, it has security clearance,
- 10:33Speaker 2: it has a priority level. By treating running programs as processes,
- 10:38Speaker 2: the OS could finally manage them safely.
- 10:40Speaker 1: It could isolate them.
- 10:41Speaker 2: Isolate them, so if one process crashes, it doesn't necessarily
- 10:44Speaker 2: kill the others.
- 10:45Speaker 1: And this ties into virtual memory too, because if you
- 10:48Speaker 1: have fifty processes running, they can't all fit in the
- 10:51Speaker 1: physical RAM.
- 10:52Speaker 2: Virtual memory is the ultimate illusion. The OS breaks a
- 10:55Speaker 2: process into these fixed sized blocks called pages. It can
- 10:59Speaker 2: scatter them those pages anywhere, some in RAM, some on
- 11:01Speaker 2: the desk.
- 11:02Speaker 1: With the program thinks it's all in one continuous piece.
- 11:04Speaker 2: The program sees a continuous, beautiful block of memory. It
- 11:07Speaker 2: uses virtual addresses. The hardware, specifically, the memory management unit
- 11:12Speaker 2: translates those virtual desks into physical addresses.
- 11:14Speaker 1: On the fly, so a developer can write a program
- 11:17Speaker 1: that uses four gigabytes of RAM even if the machine
- 11:20Speaker 1: only has two gigabytes free. It's basically gas lighting the software.
- 11:24Speaker 1: Oh yeah, you totally have all the space while frantically
- 11:26Speaker 1: shuffling data in and out of the back door.
- 11:29Speaker 2: Gas Lighting is a harsh word for it, but technically accurate.
- 11:32Speaker 2: It's abstraction. You abstract the harsh reality of the hardware
- 11:36Speaker 2: to create a better reality for the software.
- 11:39Speaker 1: Speaking of harsh reality, let's talk about the hardware we
- 11:41Speaker 1: use today. We aren't just using single processors anymore.
- 11:44Speaker 2: We have SMP and multi core Symmetric multiprocessing. SMP was
- 11:49Speaker 2: the first step. You plug two or four separate CPUs
- 11:52Speaker 2: into one motherboard.
- 11:53Speaker 1: They share everything, they.
- 11:54Speaker 2: Share memory IO. The US has to schedule tasks across
- 11:58Speaker 2: all of them.
- 11:59Speaker 1: Which is great for real life liability. If one burns out,
- 12:01Speaker 1: the other keeps going.
- 12:02Speaker 2: But multi core is where we are now putting multiple
- 12:05Speaker 2: brains on a single chip. It's faster, more efficient, but
- 12:09Speaker 2: it creates a huge headache for the OS designer called
- 12:11Speaker 2: cash coherence.
- 12:12Speaker 1: Because each core has its own little cash right, its
- 12:15Speaker 1: own little countertop of ingredients exactly.
- 12:18Speaker 2: So Core A changes the value, you say, updates your
- 12:21Speaker 2: bank balance in its private cache, but COREB tries to
- 12:24Speaker 2: read that balance from its own cash.
- 12:26Speaker 1: COREB has old information. You have a synchronization nightmare.
- 12:30Speaker 2: So the OS and the hardware have to constantly gossip
- 12:33Speaker 2: with each other. Hey, I changed this value, invalidate your copy.
- 12:36Speaker 2: It has a lot of complexity.
- 12:38Speaker 1: It's fascinating that all these tricks interrupts caching, time slicing.
- 12:42Speaker 1: They're universal, but how they're packaged differs. Stallings compares the
- 12:48Speaker 1: big three Windows, Linux, and Android, and they have very
- 12:52Speaker 1: different personalities.
- 12:53Speaker 2: They do take Windows. Windows is the ultimate corporate manager.
- 12:57Speaker 2: It's highly modular. It has this thing called the HL,
- 13:00Speaker 2: the Hardware abstraction.
- 13:01Speaker 1: Layer, which sounds like the villain from two thousand and
- 13:03Speaker 1: one A Space Odyssey.
- 13:04Speaker 2: Fortunately it's helpful. The HAL is a translator. It means
- 13:08Speaker 2: the core of Windows does need to know if you're
- 13:09Speaker 2: running an Intel chip or an AMD chip, or what
- 13:12Speaker 2: motherboard you have. AHL handles the specifics.
- 13:14Speaker 1: It isolates the kernel.
- 13:15Speaker 2: From the messiness of the real world. This is why
- 13:18Speaker 2: you can build a PC out of random parts and
- 13:20Speaker 2: Windows generally just boots up.
- 13:22Speaker 1: It prioritizes compatibility, compatibility, and portability.
- 13:26Speaker 2: It uses a client server model. Even internally, different parts
- 13:29Speaker 2: of Windows talk to each other by sending messages, almost
- 13:32Speaker 2: like they're on a network.
- 13:33Speaker 1: Now contrast that with Linux. Stallings calls it monolithic. That
- 13:37Speaker 1: sounds heavy.
- 13:38Speaker 2: It is, but in a good way. In Linux, the
- 13:41Speaker 2: kernel is one giant, highly optimized brain. All the drivers,
- 13:45Speaker 2: the file systems, the memory management, it's all living in
- 13:49Speaker 2: the same address space.
- 13:50Speaker 1: Isn't that dangerous If the audio driver crashes, doesn't it
- 13:53Speaker 1: take down the whole brain?
- 13:55Speaker 2: Historically, yes, that was the big criticism, but Linux got clever.
- 13:59Speaker 2: They added loadable.
- 14:01Speaker 1: Modules like Lego bricks sort of.
- 14:03Speaker 2: It means you can plug in a new device driver
- 14:05Speaker 2: or a file system while the kernel is running. You
- 14:07Speaker 2: don't have to shut down and rebuild the whole LOS
- 14:10Speaker 2: just to add support for a new webcam.
- 14:11Speaker 1: So you get the raw speed of a monolithic kernel with.
- 14:14Speaker 2: The flexibility of a modular one. It's the Borg approach,
- 14:17Speaker 2: adapt and assimilate without stopping.
- 14:19Speaker 1: And then Android is basically Linux putting on a hiking backpack.
- 14:23Speaker 2: It is Linux optimizing for survival power management on a server.
- 14:28Speaker 2: Linux assumes it has a wall socket. It runs tasks
- 14:31Speaker 2: whenever it wants. On a phone, Android has to be ruthless.
- 14:35Speaker 2: It uses wayclocks to prevent apps from keeping the CPU awake.
- 14:39Speaker 1: It kills background processes.
- 14:40Speaker 2: Aggressively because the dead battery makes the smartest OS completely useless.
- 14:46Speaker 1: True.
- 14:47Speaker 2: Android also uses a unique software stack for apps. It
- 14:50Speaker 2: doesn't run standard Linux programs. It runs apps inside a virtual.
- 14:54Speaker 1: Machine, which creates a sandbox.
- 14:56Speaker 2: Right, so if an angry bird crashes, it doesn't crash
- 14:58Speaker 2: your phone dialer.
- 14:59Speaker 1: It really highlights that the OS is the unsung hero.
- 15:02Speaker 1: We've talked about efficiency, convenience, evolution, but stallings leaves us
- 15:07Speaker 1: with one final, slightly terrifying concept, fault tolerance.
- 15:11Speaker 2: It's the realization that hardware will fail, not might will.
- 15:16Speaker 1: It's the law of large numbers. If you have billions
- 15:18Speaker 1: of transistors, one of them is going to flake out.
- 15:20Speaker 2: Eventually, a cosmic ray is going to hit your hard drive.
- 15:22Speaker 2: So the OS has to be paranoid. It relies on redundancy.
- 15:25Speaker 1: Redundancy in what sense.
- 15:26Speaker 2: Three senses actually spatial redundancy, having backup hardware like raid discs.
- 15:31Speaker 2: If one fails, the other takes over. Temporal redundancy. If
- 15:35Speaker 2: a calculation looks weird or fails, run it.
- 15:37Speaker 1: Again, just turn it off and on again at the
- 15:40Speaker 1: microchip level basically.
- 15:41Speaker 2: And information redundancy using air correcting codes to check if
- 15:45Speaker 2: a one accidentally flipped to a zero.
- 15:47Speaker 1: So the OS isn't just a manager. It is a
- 15:50Speaker 1: disaster prevention team.
- 15:51Speaker 2: Ideally, yes, it's managing a constant state of low level crisis.
- 15:56Speaker 2: It's masking the chaos of the physical world so that
- 15:59Speaker 2: you can exist in the pristine world of software. If
- 16:02Speaker 2: the OS does its job right, you never know how
- 16:05Speaker 2: close he came to a crash.
- 16:06Speaker 1: Well, on that comforting note. Next time your phone buzzes
- 16:09Speaker 1: with a notification, just remember that's an interrupt signal, saving
- 16:12Speaker 1: you from staring at a frozen screen. And that smooth scrolling,
- 16:16Speaker 1: that's a gamble that paid off. Thanks for listening to
- 16:18Speaker 1: the deep dive.
- 16:19Speaker 2: We'll catch on the next one.