COMP 3430 Operating systems – chapter 4,5,6 reading notes
Winter 2022
About these reading notes
Chapter 4: The Abstraction: The Process
Copyright By PowCoder代写 加微信 powcoder
TheAbstraction:AProcess……………………………… 3 ProcessAPI…………………………………….. 3 ProcessCreation:ALittleMoreDetail ………………………… 4 Processstates……………………………………. 4 Datastructures…………………………………… 5
Chapter 5: Interlude: Process API 5 Thefork()systemcall………………………………. 5 Thewait()systemcall………………………………. 6 Theexec()systemcall………………………………. 6 Why?MotivatingtheAPI ………………………………. 7 ProcessControlandUsers ……………………………… 7 Usefultools…………………………………….. 7 Summary……………………………………… 7
Chapter 6: Mechanism: Limited Direct Execution 7 BasicTechnique:LimitedDirectExecution ……………………… 7 Problem#1:Restrictedoperations………………………….. 8 Problem#2:SwitchingBetweenProcesses ……………………… 9
ACooperativeApproach:Waitforsystemcalls …………………. 9 ANon-Cooperativeapproach:TheOSTakescontrol . . . . . . . . . . . . . . . . . . . 9 SavingandRestoringContext …………………………. 10
WorriedaboutConcurrency?…………………………….. 10
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
About these reading notes
These are my own personal reading notes that I took (me, Franklin) as I read the textbook. I’m pro- viding these to you as an additional resource for you to use while you’re reading chapters from the textbook. These notes do not stand alone on their own — you might be able to get the idea of a chap- ter while reading these, but you’re definitely not going to get the chapter by reading these alone.
These notes are inconsistently all of the following:
• Mesummarizingpartsofthetext.
• Mecommentingonpartsofthetext.
• Measkingquestionstomyselfaboutthetext.
– …andsometimesansweringthosequestions.
The way that I would expect you to read or use these notes is to effectively permit me to be your in- ner monologue while you’re reading the textbook. As you’re reading chapters and sections within chapters, you can take a look at what I’ve written here to get an idea of how I’m thinking about this content.
Chapter 4: The Abstraction: The Process
Process Arunningprogram.
“waiting to spring into action” — sproing.
It’s the operating system’s responsibility to take code that’s just sitting on the hard drive and then actually do something with it.
Most modern systems have many CPUs available, but, take a look at your task manager: how many processes are currently running? While I’m writing this, I have 238 running processes. As far as each of those processes is concerned, it has full, unfettered access to one (or more) CPUs. I definitely don’t have hundreds of CPUs on my system.
Open up your task manager (on Windows 10, hit Ctrl+Alt+Delete and choose “Task Manager”, then
Given what you know about writing programs, what kinds of things do you think an operating system has to do when it’s starting a program? I’m thinking about things like reading files and populating data structures.
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
switch to the “Performance” tab). How many processes you have running right now? Do you have that many CPUs on your system?
A basic technique for virtualizing the CPU (giving every process the illusion that it’s got a processor to itself) is time sharing, give 𝐴 a bit of time, then switch to 𝐵, then switch to 𝐶, then switch back to 𝐴, really quickly.
They use the term “context switch”, but don’t go into detail in this chapter.
They do, however, say that time sharing is a mechanism that’s used by all modern operating sys- tems.
The Abstraction: A Process
The entire idea of a process is an abstraction.
The operating system needs to keep track of the state of a process while it’s running (the machine
There’s a tip here that sums up to: separate the implementation from the interface. The tip is about
separating policy and mechanism.
Process API
This is listing some of the kinds of operations that an OS needs to expose in terms of how it deals with processes.
• Create(makessense,weneedtobeabletocreateanewprocess)
• Destroy (also makes sense, we need to be able to get rid of a process and everything that’s as-
sociated with it).
• Wait(sometimesit’susefultowaitforaprocesstofinish?Why???)
• Miscellaneous control (“most operating systems provide some kind of method to suspend a
process (stop it from running for a while) and then resume it (continue running it)”)
• Status(basically:“tellmeaboutyourself”)
When do you think an OS might need to suspend a process? What kinds of things could a process
In terms of destroying a process: what kinds of things do you think an OS has to do to destroy a process?
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
be doing where the OS might want to tell it to stop? When might you want to tell a process to “stop”, then continue again later?
You can spend some time spelunking in the /proc file system on a Linux machine, if you’re inter- ested.
Process Creation: A Little More Detail
An OS has to load a program into memory. The program itself is in some kind of executable format (e.g., ELF).
It’s more complicated than just reading the binary directly into memory (beyond “lazy loading”, the OS has to set up its own internal data structures), but we’re just hand-waving it here. The ultimate description of what they’re doing here is that they need to load code and “static” data, the data that’s encoded in the program itself.
The OS does allocate memory for the stack (but note that this is the entire stack, not individual stack frames). The one stack frame that the OS is responsible for setting up is the frame for main, with argv and argc.
The OS may also allocate memory for the heap.
The last thing the OS does is set up some I/O stuff, specifically giving the process access to things like I/O (standard input, standard output, and standard error).
Process states
Processes can be in a state, and in the simplified book version, processes can be in 3 different states:
• Running(theinstructionsforthisprocessarecurrentlyrunningonaprocessor).
• Ready(theprocessisreadytoberun,butit’snotactuallyrunningontheprocessor). • Blocked(theprocessshouldnotberun,it’swaitingforsomethingtohappen).
They’ve got some nice tables here showing how processes can move between ready, running, and blocked.
In terms of “allocating” memory, what do you think the OS is actually doing here? Note that the OS does not call functions like malloc, those functions live in standard libraries.
Looking back at the Process API section, do you think that this description of “blocked” as a status here is related to what was described in miscellaneous control?
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
There’s a reasonable question here under figure 4.4: “it is not clear if this is a good decision or not. What do you think?”
Data structures
I really like this section because it shows “real” data structures that an OS might use to represent a running process.
Take a look at Figure 4.5, there are more states than just running, ready, and blocked. What do you think each of these states represent? This is covered later, but still might be worth looking at.
How much do you think the OS has to know about the CPU it’s running on? This diagram shows a bunch of registers in this thing called a “register context”, is this going to be the same for every CPU?
They refer to “context switch”, but say that it’s for later, that’s OK. You should actually have the basic idea of what’s happening (we’re switching contexts).
Task list as an idea (a list!), and the term “PCB process control block” is used here to describe the data structures that represent a process.
Chapter 5: Interlude: Process API
We’re discussing specifically some system calls that are used to create new processes. Specifically, we’re looking at fork, exec, and wait (at least).
The fork() system call
Notice that this is explicitly referred to as a “system call” now.
Let’s imagine, for a second, that a machine has exactly 1 CPU in it. Once an OS starts running instructions for a process, it’s effectively given up control of the processor to that process (the OS can’t also be executing instructions, there’s only one CPU!). How do you think the OS might get control of the CPU again?
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
The book suggests typing in and running the program. Do it! Seriously!
Surprise! fork forks into two different processes. This is supremely weird.
The order of operations of the messages that are printed by the code isn’t deterministic, you can run it multiple times and possibly get different results each time.
There’s some foreshadowing to concurrency and scheduling.
The wait() system call
The wait system call lets a process wait for it’s children to complete.
What is a process doing when it’s waiting for children? What state might it be in? The exec() system call
Oh boy. This is an entire family of functions. They said that fork was weird, but I think this one is weirder, mainly because by calling it you’re asking to replace your code with something else.
So exec is basically loading up a different program (by name) and replacing the code that was running with the code for the specified program. Weird. fork is “normal” to me in that it’s a clone, exec is like switching out bodies.
Based on what you read about processes in chapter 4, what do you think the OS has to do when fork() is called?
Thinking back to the description of states of processes, what do you think that means about how the state of the calling process changes? What does that mean about the state of the newly created process?
What’s the difference between wait and waitpid? Check out the manual pages for each! Note that you might have to run man 2 wait to get the manual page for the wait system call and not a shell equivalent.
Based on this idea of fork and exec, what exactly do you think a shell is doing when you’re entering commands? A shell is the name for the program that’s printing out the prompt when you log in to a remote system.
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
Why? Motivating the API
The shell stuff is actually then discussed in this part of the chapter.
This is cool, because it’s giving an example of the redirect operator, and it’s kind of showing the idea that there’s code running behind the scenes to set up the file for standard output et al.
Figure 5.4 really drives home this idea! Process Control and Users
Creating and waiting are not the only system calls on the block related to processes, there’s also kill.
The main point here, though, I think is that the OS is restricting who can interact with processes based on user accounts.
Useful tools
Here we’re listing tools like top, ps, kill, etc Summary
This refers to a system call called spawn. The spawn system call is kind of like fork/exec, but for other operating systems like Windows.
Chapter 6: Mechanism: Limited Direct Execution
We’re going to talk about some of the nitty gritty details of time-sharing as a concept. Fun!
Two main concepts here: performance and control. Performance is specifically defined here as “with- out adding excessive overhead to the system”. Control is defined as “how does an OS keep control of a CPU?”
Basic Technique: Limited Direct Execution
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
“The OS will often use a judicious bit of hardware support in order to accomplish its work effec- tively.” — why do you think the OS can’t do this by itself?
CPU. Two concerns once a process is running:
1. Howdowestopaprocessfromdoingsomethingwe(theOS)don’twantittodo,and 2. Howdowestoptheprocesssothatwecandotimesharing?
Problem #1: Restricted operations
Here they note “How can the OS and hardware work together to do so?” This is going to be an ongoing theme — how does the OS (software) work together with the hardware to accomplish what it needs to do?
There’s an aside here describing how a system call is actually doing a trap instruction instead of just directly calling more code. It also approaches describing the distinction between a standard library and the OS.
They answer the question above right below here, I/O is a restricted operation because we want to build permissions into the FS.
System calls are described as both a responsibility of the OS and the hardware. This makes it a bit weird, but you have to remember that the OS is software itself, the same as user programs. So if the OS has the entire responsibility to handle system calls (which it may have at some point), then user programs could (in theory) just do the same things as the OS.
“Restricted operations” here are described as “issuing an I/O request to a disk, or gaining access to more system resources such as CPU or memory”. Thinking about the responsibilities of an OS, why do you think these operations are “restricted”?
This is a bit outside the scope of the course, but do you think that there might be more kinds of modes that actual CPUs have beyond user and kernel mode? Why do you think a CPU would need those additional modes?
“To execute a system call, a program must execute a special trap instruction. The instruction simultaneously jumps into the kernel and raises the privilege level to kernel mode;” — Who do you think is responsible for setting up these connections (e.g., where in the kernel the CPU jumps to for a specific system call), when those connections need to be set up, and what kind of a data structure might be used to store those connections?
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
The trap table is then described almost immediately below this. Figure 6.2 actually goes through and describes this entire setup, both timing, trap table, and trap handlers.
The x86 platform has a per-process kernel stack. Why do you think this is only on a specific pro- cessor architecture?
The tip on pg 6 says “Otherwise, it would be possible for a user to read all of kernel memory;
given that kernel (virtual) memory also usually includes all of the physical memory of the system, this small slip would enable a program to read the memory of any other process in the system.” Why does the OS have access to all physical memory? Does an OS care about what a process is doing within its own virtual memory space?
What kinds of things might a user program be able to do if it were able to install its own trap table?
Thinking about fork, exec, and wait, where in figure 6.2 would these calls fit? Problem #2: Switching Between Processes
The main problem with running user code directly on the processor is that you (the OS) needs to be able to regain control of the CPU so that you can let other programs run.
A Cooperative Approach: Wait for system calls
This seems like a reasonable approach: eventually a program is going to have to ask the OS for some- thing, so just wait around for the program to ask the OS to do something before deciding to switch something out.
A Non-Cooperative approach: The OS Takes control
So, uh, turns out we (the OS) can’t do much in the cooperative situation in terms of bailing out if someone’s stuck in a loop. That requires manual, physical intervention to reboot the machine.
The next approach is … falling back to letting the hardware help us out.
Here’s an example: if a program is computing digits of Pi, when might it issue a system call? Here’s a C program that computes pi: https://crypto.stanford.edu/pbc/notes/pi/code.html
COMP 3430 Operating systems – chapter 4,5,6 reading notes Winter 2022
Notice that we again have to let the hardware help us out here — timer interrupts are kind of like traps, but instead of being invoked by a user program, the hardware is set up to run it on a schedule.
Compare and contrast interrupts and interrupt handlers and traps and trap handlers. What are similar between the two ideas? What’s different?
Saving and Restoring Context
Oh boy context switches!
The scheduler is the part of an OS that’s responsible for deciding who’s next, but for now we’re go- ing to assume that who’s going next is decided for us. We just need to think about the mechanics of actually getting that program running.
Looking back at the previous chapters describing a process, where do you think all the informa- tion that an OS needs to save about a process gets stored?
Thinking about two parts of a context switch (the switching between kernel mode and user mode and switching to a new process), how much do you think is the same between different operating systems (e.g., Windows vs Linux vs macOS)? How much is different?
Worried about Concurrency?
What happens if multiple of the events happen at the same time? It’s a problem, yes, but let’s acknowl- edge it’s a problem, then ignore it for now.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com