1. CS225
    1. Introduction
    2. Lesson 1
    3. Lesson 2
    4. Lesson 3
    5. Lesson 4
  2. CS251
    1. The Beginning
  3. CS180
    1. Introduction
    2. Midterm Revision
    3. Finals Revision
    4. CS251
    5. Memory
    6. ELF Format
    7. History of Computers
    8. Stuff to Research
    9. Quiz To-Do
    10. OS Architectures
    11. Week 3
    12. Week 4
    13. Week 5
    14. Week 6 (Threads)
    15. Week 7 (Scheduling)
    16. Week 8 (Thread Scheduling)
    17. Week 9 (Memory Management)


    1. OOP
      1. Inheritance
      2. R-Value References
    2. Misc Notes
    3. Size/Offsets of Structs/Classes
    4. Week 4
    1. Introduction
    2. Scan Conversion
    3. Quiz 01
    1. Notes

Week 3

User Mode vs Kernel Mode

User Mode cannot access privileged instructions.

1. Kernel Mode
In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC.

2. User Mode
In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode must delegate to system APIs to access hardware or memory. Due to the protection afforded by this sort of isolation, crashes in user mode are always recoverable. Most of the code running on your computer will execute in user mode.


Writing to a file - System callls required

Write, Read, Open, Close


If you want to port from one platform to another, for example, Windows to Linux, and you're using windows library functions, then the porting is not so simple, obviously.

There are two types of portability:
- Binary level
→ Harder to port, obviously
- Source code level


Different kinds of kernel architecture:

Monolithic: All code in OS runs in kernel mode (More efficient, requires no kernel change, but less stability) (Less stability because everything is talking to each other with kernel mode commands, if one is corrupted then the system's screwed)
Micro Kernel:
- Small part of OS as kernel
- Important services running as processes outside in user mode
- In order for the processes to talk to each other they require system calls (kernel change)
- More stability
Combination of the above two.
Exo-Kernel: Don't need to know, but has been talked about by MIT for years.
→ Commands can control hardware - risky.

Windows Win95, 98 and ME are monolithic
Windows NT 4.0, 2000, XP and beyond are micro kernel


Hard modularity:
- Slower speed
- Code is contained within specific individual programs
- If something crashes, other programs can still continue running
Soft modularity:
- Better speed
- Code is dependent on other code
- If something crashes, the entire program crashes


Snapshot view

Assume a process using the CPU at a point in time.
- What are the “resources” that the running program would be using
→ Memory
→ Registers
→ P.C.
→ Instruction Decoder
→ I/O
→ Files


In Linux there are 2 different stacks: user stack and kernel stack.
User stack is where the PC and other variables are stored.
Kernel stack is used to resolve ISRs.


Context saving and restoring
When a program get interrupted, it needs to save its current process and then switch to the ISR, and restore it once the ISR is resolved.
This is call context saving and restoring
Automatic (no instructions involved)

HW saving then SW saving (sometimes HW context saving doesn't save all the registers, for example the FPR)

What happen if CPU don't suppose HW saving?
HW MUST support some kind of saving
It must save at least the PC and SP
It must save them before ISR runs

Where does it save to:
HW reads the SP
Save the registers to the stack

So if the question asks where the hardware saves the context of the running process when an interrupt happens, just say “stack”.

When doing context switching, the values in the Register must be saved before switching, and restore after restoring.
The important registers to save are: PC and Stack Pointer
Other Regs are General Purpose regs, Floating pointer Stack, Status Flag

Context of a process:
The minimum data that must be saved before another code is executed.
The context is saved before switching control to the ISR

In Linux?, there are 2 different stacks that is assigned to a process: user stack and kernel stack.
User stack is where the PC and other variables are stored.
Kernel stack is used to resolved ISRs.


Process state transitions

P1 is running in the CPU.
Interrupt comes in, P1 is suspended.
Serve the interrupt.
Needs to know what the process is doing at that stage, and thus we need states to describe where they are.


return and exit are different because return will not return the status code to the parent.

return exits from the function while exit exits from the program.
In main function executing return 0; statement or calling exit(0) function will call the registered atexit handlers and will cause program termination.