Lecture 4 - Processes and Privilege
Last time we talked about how we have user level and root/admin level privilege levels. But how can we have user-level processes run admin-level processes? The idea is:
- You send an interrupt, after putting your arguments in the right registers.
- The OS does stuff with that interrupt and the arguments in the registers.
- The OS puts the return of that process in a certain register.
How does this whole thing get going?
- When you turn on your computer, the memory and CPU (all the flip flops) get loaded to an unknown state.
- The flip flop reset line
get set so that we can have a known state. - Have a small amount of read only memory to load the right
PC
value. - Setup a bunch of needed data structures.
- Choose a user process. Load it's resources, ...
Old floppy disks with these bootloaders were only 512 bytes. That's pretty small! But it only needs to know either about other bootloaders, or about the OS.
The big question is how do you get the control back as the OS? The user process is running (and you are not). They could also write a while(true);
loop and keep control.
While the OS is in supervisor mode, it creates a timer interrupt. It uses this to start the timer once it yields control to the user. Eventually either:
- The process makes a system call. (Possibly) Reset the timer and is caused by the interrupt to get back to the OS.
- The timer goes, which triggers the interrupt to give control back to the OS.
The user can't do anything because the user can't stop the interrupt, nor change the ISR or anything. Also, keep in mind that if the timer is too long, then you may experience crashes (freezes) but if it's too short then the timer overhead is too overwhelming. The industry standard is 0.1 seconds, or 100 ms.
Most processes spend most of their time in the blocked state.
Sharing CPU Resources
How do we share cores on our CPU? We create a process which saves the process state like:
- Registers
- File Descriptors
- ...
That way one process can swap the data with another process and then give it back. We can do this by pushing onto the stack, and popping when we leave the stack.
You, as a payment, cannot rely on timing. Because at any moment you may be sent to the shadow realm via an interrupt. But, you do get both parallelism and concurrency, which is pretty awesome.