3 1up
3 1up
• Why processes?
Multi-Threaded Process
– Protected from each other!
– OS Protected from them
• In modern OS, anything that runs outside of the kernel runs in
a process
heap
Addresses translated Static Data
on-the-fly heap
stack
0100… stack
Base Address
1000…
0010… 1000… code
Bound <
stack
1100…
0100…
OS heap
stack
sysmode 0 1000…
code
Base 1000 … 0000… Static Data
uPC xxxx…
stack 1100…
PC 0000 1234
code 3000…
regs
Static Data
00FF…
• How to … heap
OS heap
stack
sysmode 1 1000…
code
Base 1000 … 0000… Static Data
registers and
set up system stack 3080…
stack? FFFF…
1/23/24 Kubiatowicz CS162 © UCB Spring 2024 Lec 3.8
Simple B&B: Switch User Process
0000…
Proc Proc Proc code RTU
1 2 … n
Static Data
OS heap
stack
sysmode 1 1000…
1000 … code
Base 3000 … 0000… Static Data
0100 …
Bound 0080 … FFFF… heap
0000 1234
uPC 0000 0248
regs stack 1100…
PC 0001 0124
00FF…
code 3000…
regs
Static Data
00D0…
• How to save … heap
registers and
set up system stack 3080…
stack? FFFF…
1/23/24 Kubiatowicz CS162 © UCB Spring 2024 Lec 3.9
Simple B&B: “resume”
0000…
Proc Proc Proc code RTU
1 2 … n
Static Data
OS heap
stack
sysmode 0 1000…
1000 … code
Base 3000 … 0000… Static Data
0100 …
Bound 0080 … FFFF… heap
0000 1234
uPC xxxx xxxx
regs stack 1100…
PC 000 0248
00FF…
code 3000…
regs
Static Data
00D0…
• How to save … heap
registers and
set up system stack 3080…
stack? FFFF…
1/23/24 Kubiatowicz CS162 © UCB Spring 2024 Lec 3.10
Is Simple Base and Bound Enough for General Systems?
• NO: Too simplistic for real systems
• Inflexible/Wasteful:
– Must dedicate physical memory for potential future use
– (Think stack and heap!)
• Fragmentation:
– Kernel has to somehow fit whole processes into contiguous
block of memory
– After a while, memory becomes fragmented!
• Sharing:
– Very hard to share any data between Processes or between
Process and Kernel
– Need to communicate indirectly through the kernel…
if ( readyProcesses(PCBs) ) {
nextPCB = selectProcess(PCBs);
run( nextPCB );
} else {
run_idle_process();
}
Sharing code or test cases with another group or individual (including HW!)
Copying OR reading another group’s code or test cases
Copying OR reading online code or test cases from prior years
Helping someone in another group to debug their code
• We compare all project and HW submissions against prior year submissions and
online solutions and will take actions (described on the course overview page)
against offenders
• Don’t put a friend in a bad position by asking for help that they shouldn’t give!
1/23/24 Kubiatowicz CS162 © UCB Spring 2024 Lec 3.22
More About Threads: What are they?
• Definition from before: A single unique execution context
– Describes its representation
A B C
Multiprogramming A B C A B C B
• Now, you would actually see the CPU1 CPU2 CPU1 CPU2 CPU1 CPU2
class list
Time
Handle I/O in
separate thread,
avoid blocking
other progress
Time
Time
Time
• Initially, this new process has one thread in its own address space
– With code, globals, etc. as specified in the executable
OS
OS
create
exit
join
• Execution Stack
– Parameters, temporary variables
– Return PCs are kept while called procedures are executing
Address Space
– How do we position stacks relative to Stack 2
each other?
– What maximum size should we choose
for the stacks?
– What happens if threads violate this? Heap
– How might you catch violations?
Global Data
Code
0x000…
• Initially x == 0 and y == 0
Thread A Thread B
x = 1; y = 2;
• What are the possible values of x below after all threads finish?
• Must be 1. Thread B does not interfere
Thread A Thread B
Insert(3) Insert(4)
Get(6)
• Mutual Exclusion: Ensuring only one thread does a particular thing at a time
(one thread excludes the others)
– Type of synchronization
Critical section
Applications
User Mode
Standard Libs
Kernel Mode
Hardware
User Mode
interrupt
exception
syscall
rtn rfi
exit
exec Kernel Mode
Word Processing
Compilers Web Browsers
Email
Databases Web Servers
Application / Service
Portable OS Library OS
User Mode
System Call
Interface
System Mode
Portable OS Kernel
Software Platform support, Device Drivers
Interrupt Mask
Priority Encoder
IntID
CPU
Timer
Interrupt Int Disable
Software Control
Interrupt NMI
Network
• Interrupts invoked with interrupt lines from devices
• Interrupt controller chooses interrupt request to honor
– Interrupt identity specified with ID line
– Mask enables/disables interrupts
– Priority encoder picks highest enabled interrupt
– Software Interrupt Set/Cleared by Software
• CPU can disable all interrupts with internal flag
• Non-Maskable Interrupt line (NMI) can’t be disabled
1/23/24 Kubiatowicz CS162 © UCB Spring 2024 Lec 3.80
How do we take interrupts safely?
• Interrupt vector
– Limited number of entry points into kernel
• Kernel interrupt stack
– Handler works regardless of state of user code
• Interrupt masking
– Handler is non-blocking
• Atomic transfer of control
– “Single instruction”-like to change:
» Program counter
» Stack pointer
» Memory protection
» Kernel/user mode
• Transparent restartable execution
– User program does not know interrupt occurred
intrpHandler_i () {
….
}