Intro
Intro
Stanford University
1 / 36
Outline
1 Administrivia
2 Substance
2 / 36
Hybrid teaching
3 / 36
CS212 vs. CS112
• CS212 (previously CS140) is a standalone OS class
- Lectures introduce OS topics, similar to CS111
- Exams test you on material from lecture
- Programming projects make ideas concrete in an instructional OS
• CS112 is just the projects from CS212
- Only makes sense if you’ve previously taken CS111
- Idea: projects in separate quarter from lectures allows more time
- Feel free to attend any lectures if you want to review a topic (but
most will be similar to CS111)
- A few recommended lectures/sections marked in syllabus
• There could be bugs in program sheets
- CS111 or CS212 should fulfill any OS breadth requirement
- CS112 or CS212 should satisfy significant implementation
- Ask for exception is something doesn’t make sense
4 / 36
Administrivia
5 / 36
Administrivia 2
• Edstem is the main discussion forum
• Staff mailing list: [email protected]
- Please use edstem for any questions others could conceivably have
- Otherwise, please mail staff list, not individual staff members
• CA split office hours, first round-robin, then individual group
- Please ask non-private questions in RR portion
- Priority for individual group will go to people who attended RR
• Key dates:
- Lectures: MW 1:30pm–3:00pm
- Section: 10:30am on 6 Fridays, starting this Friday (Skilling)
- Midterm: Monday, February 13, in class (1:30pm-2:50pm)
- Final: Wednesday, March 22, 3:30pm-6:30pm
- We’ll accommodate exam conflicts in the 24 hours after exam
• Exams open note, but not open book
- Bring notes, slides, any printed materials except textbook
6 / 36
Course topics
7 / 36
Course goals
8 / 36
Programming Assignments
1 Administrivia
2 Substance
13 / 36
What is an operating system?
14 / 36
Why study operating systems?
OS
Hardware
16 / 36
Multitasking
emacs firefox
OS
Hardware
• Idea: More than one process can be running at once
- When one process blocks (waiting for disk, network, user input,
etc.) run another process
• Problem: What can ill-behaved process do?
17 / 36
Multitasking
emacs firefox
OS
Hardware
• Idea: More than one process can be running at once
- When one process blocks (waiting for disk, network, user input,
etc.) run another process
• Problem: What can ill-behaved process do?
- Go into infinite loop and never relinquish CPU
- Scribble over other processes’ memory to make them fail
• OS provides mechanisms to address these problems
- Preemption – take CPU away from looping process
- Memory protection – protect processes’ memory from one another
17 / 36
Multi-user OSes
emacs firefox
OS
Hardware
• Many OSes use protection to serve distrustful users/apps
• Idea: With N users, system not N times slower
- Users’ demands for CPU, memory, etc. are bursty
- Win by giving resources to users who actually need them
• What can go wrong?
18 / 36
Multi-user OSes
emacs firefox
OS
Hardware
• Many OSes use protection to serve distrustful users/apps
• Idea: With N users, system not N times slower
- Users’ demands for CPU, memory, etc. are bursty
- Win by giving resources to users who actually need them
• What can go wrong?
- Users are gluttons, use too much CPU, etc. (need policies)
- Total memory usage greater than machine’s RAM (must virtualize)
- Super-linear slowdown with increasing demand (thrashing) 18 / 36
Protection
19 / 36
Typical OS structure
user P1 P2 P3 P4
kernel VM IPC
sockets file
TCP/IP scheduler system
driver driver driver
NIC console
disk
• Most software runs as user-level processes (P[1-4])
- process ≈ instance of a program
• OS kernel runs in privileged mode (orange)
- Creates/deletes processes
- Provides access to hardware
20 / 36
System calls
22 / 36
System call example
24 / 36
Error returns
25 / 36
Operations on file descriptors
26 / 36
File descriptor numbers
27 / 36
type.c
void
typefile (char *filename)
{
int fd, nread;
char buf[1024];
close (fd);
}
• Can see system calls using strace utility (ktrace on BSD)
28 / 36
Protection example: CPU preemption
29 / 36
Protection is not security
30 / 36
Protection is not security
30 / 36
Address translation
31 / 36
More memory protection
34 / 36
Resource allocation & performance
35 / 36
Useful properties to exploit
• Skew
- 80% of time taken by 20% of code
- 10% of memory absorbs 90% of references
- Basis behind cache: place 10% in fast memory, 90% in slow,
usually looks like one big fast memory
• Past predicts future (a.k.a. temporal locality)
- What’s the best cache entry to replace?
- If past ≈ future, then least-recently-used entry
• Note conflict between fairness & throughput
- Higher throughput (fewer cache misses, etc.) to keep running
same process
- But fairness says should periodically preempt CPU and give it to
next process
36 / 36