Operating Systems (CSE 120)
OS Basics
-
Lecture 2: Interactions with Apps and Hardware
What is an Operating System?
- Sits between applications and hardware
- Provides abstractions to applications
- Implements abstractions and manages resources
Hardware of a Typical Computer
- System bus
- CPUs
- Memory
- Network
- Storage
Software of a Typical (Unix) System
User level: Applications and libraries
Kernel level: Portable OS layer, machine-dependent layer
- System calls
- Bootstrap & Initialization
- Interrupts & exceptions
- Drivers, memory management
- Mode & processor management
Questions for Today
- How to separate OS from apps?
- How to cross layers safely?
- How does hardware support this?
Protection and OS Interaction
- Protection: Privileged instructions, memory protection
- Interaction: Faults, system calls, interrupts
Dual-Mode Operation
- Kernel mode: unrestricted
- User mode: limited
- Controlled by mode bit in CPU register
Privileged Instructions
- Allowed only in kernel mode
- Examples:
- I/O access
- Memory management state
- Protected register changes
Example of a Privileged Instruction
HLT
: halts CPU (kernel mode only)Memory Protection
- Protect OS from apps
- Protect apps from each other
- Uses page tables, segmentation, TLB
- Managed using privileged instructions
Events
An event = unnatural control flow change
- Handled by kernel
- Changes mode/context
- Event handlers are run in kernel mode
Types of Events
- Interrupts: external (e.g., I/O, timer)
- Exceptions (traps): internal (e.g., system call, fault)
Faults
- Examples: divide-by-zero, page fault
- CPU saves state, looks up handler
- Switches to kernel mode
Handling Faults
Recovery:
- OS fixes issue (e.g., load missing page)
- Returns to original instruction
Termination:
- Kills user process
- Fatal kernel faults crash the OS (panic, BSOD)
System Calls
API for user programs to request OS services
- Categories: process, memory, file, device, communication
System Call Mechanism
- Uses syscall instruction (e.g., INT, SYSCALL)
- Triggers exception into kernel
- Passes syscall number and saves state
System Call Example
User level → read() ↓ Library → INT $0x03 ↓ Kernel → syscall handler → return to user
Referencing Data
- OS and user space are separate
- Use handles (e.g., file descriptors) instead of pointers
Interrupts
- Generated by hardware (I/O, timers)
- Precise on modern CPUs (instruction boundaries)
Handling Interrupts
- Disable lower-priority interrupts
- Save state
- Execute interrupt handler
- Re-enable interrupts and resume user process
Timer Interrupt
- Ensures OS regains control
- Prevents infinite loops and enforces CPU sharing
- Basis for scheduling
I/O Interrupt
- Asynchronous I/O completion notifies OS via interrupt
- Context switch to handler, then resume process
x86 Interrupts and Exceptions
Examples:
- #DE: Divide Error
- #PF: Page Fault
- #UD: Invalid Opcode
- 32–255: User-defined interrupts
The OS as an Interrupt Handler
All kernel execution is triggered by:
- Interrupts (timer, I/O)
- System calls
- Faults
Practice Question
How many mode switches occur after
main()
is called?Summary
- OS uses privileged instructions & memory protection
- Interaction via faults, syscalls, interrupts
-
Lecture 3: Processes
Review Question: Events
- Similarities:
- Trap to the OS
- Run in kernel mode
- Hardware saves state (PC, registers)
- Differences:
- Interrupts: asynchronous (external)
- Exceptions: synchronous (caused by instructions)
Next Several Lectures
- Processes (today)
- Threads (next lecture)
- Synchronization
Today’s Outline
- What is a process?
- How to represent a running program?
- APIs to interact with processes
The Process
An OS abstraction for a running program used for execution, scheduling, and resource management.
Process Components
- Address space
- Code and data
- Execution stack
- Program counter (PC)
- Registers
- OS resources (files, sockets)
- Identified by PID
Process vs. Program
Program: static code
Process: program in execution with memory, registers, etc.Basic Process Address Space
0xFFFFFFFF Stack ↑ Heap Static Data Code 0x00000000
Process State
- Running: currently using CPU
- Ready: waiting to be scheduled
- Waiting: blocked, waiting for I/O
The Processing Illusion
- Each process thinks it owns the CPU
- Managed by:
- Timer interrupts
- Context saving
- Schedulers
Process Control Block (PCB)
Contains all process info: memory, execution state, scheduling, I/O
Process Creation
- Each process has a parent
- Linux root process:
init
orsystemd
- Parent can wait or run concurrently with child
Process Creation APIs
Windows:
CreateProcess()
BOOL CreateProcess(char *prog, char *args);
Unix:
fork()
int fork();
Returns 0 to child, child PID to parent
fork() Example
int main() { int child_pid = fork(); if (child_pid == 0) { printf("I am the child, PID: %d\n", getpid()); } else { printf("My child's PID: %d\n", child_pid); } }
exec()
int exec(char *prog, char *argv[]);
- Replaces process memory with new program
- Files remain open
- Returns only on error
Process Termination
exit(int status)
(Unix)ExitProcess(int status)
(Windows)- OS reclaims memory, closes files, removes PCB
wait()
Parent pauses until child exits
wait()
: any childwaitpid()
: specific child- Required to avoid zombie processes
Unix Shell Example
while (1) { char *cmd = read_command(); int pid = fork(); if (pid == 0) { exec(cmd); } else { waitpid(pid); } }
Poll Questions
Which does the OS provide?
✅ D: Physical memory allocationPrivileged Instruction?
✅ C: INVD (invalidate caches) - Similarities:
-
Lecture 4: Threads
Processes:
Process: abstraction for a running program
Includes: Address space, OS resources & accounting info, execution state
Process creation is slow:
• 833 lines intask_struct
in Linux
• Must create/initialize many data structures
Communication between processes is slow:
• Processes are isolated
• OS mediates communication via Inter-Process Communication (IPC)IPC Mechanisms:
- Message passing:
send()
/receive()
system calls - Files:
read()
/write()
- Shared memory (e.g.,
shm_open()
)
Concurrency:
Running multiple tasks at once. Useful for:
- Web servers
- Multicore utilization
- Overlapping I/O
Multiple processes can do it, but it's inefficient in space/time.
Rethinking Processes:
Separate execution state (PC, SP, registers) from address space and resources.
Execution state = threadThread vs Process:
- Process: address space + resources
- Thread: a single sequence of execution (PC, SP, registers)
- Threads are the unit of scheduling
Process Address Space with Threads:
- Shared: Code, data, heap
- Unique per-thread: Stack, PC, SP
Thread Control Block (TCB):
- Per-thread info: state, PC, registers, stack
- Stored in Thread Control Block
Thread Lifecycle:
- States: Ready → Running → Waiting
- Transitions triggered by events like yield(), blocking I/O, timer interrupts
Context Switching:
- OS saves the CPU state of one thread (into TCB), loads another
- Can happen every millisecond
Scheduling:
- Non-preemptive: threads yield voluntarily
- Preemptive: OS uses timer interrupts to switch threads
Web Server Example:
// Process-based while (1) { int sock = accept(); if (fork() == 0) { handle_request(sock); exit(); } } // Thread-based while (1) { int sock = accept(); thread_fork(handle_request, sock); }
Kernel vs User-Level Threads:
- Kernel threads: managed/scheduled by OS
- User threads: managed in user space (e.g., by a library)
- User threads are faster, but lack integration with OS
Threading Models:
- Many-to-One: many user threads to one kernel thread
- One-to-One: each user thread mapped to one kernel thread
- Many-to-Many: many user threads mapped to many kernel threads
- Message passing: