Cs140 - Operating Systems: Instructor: David Mazi'Eres Cas: David Goldblatt and Ali Yahya Stanford University
Cs140 - Operating Systems: Instructor: David Mazi'Eres Cas: David Goldblatt and Ali Yahya Stanford University
Stanford University
1/33
Administrivia
• Class web page: http://cs140.scs.stanford.edu/
- All assignments, handouts, lecture notes on-line
2/33
Administrivia 2
• Staff mailing list: [email protected]
- Please mail staff list rather than individuals for help
3/33
Lecture videos
• Lectures will be televised for SCPD students
- Can also watch if you miss a lecture, or to review
- But resist temptation to miss a bunch of lectures and watch
them all at once
4/33
Course topics
• Threads & Processes
• Concurrency & Synchronization
• Scheduling
• Virtual Memory
• I/O
• Disks, File systems, Network file systems
• Protection & Security
• Virtual machines, Cutting edge topics
• Note: Lectures will often take Unix as an example
- Most current and future OSes heavily influenced by Unix
- Windows is exception; this quarter we will mostly ignore
5/33
Course goals
• Introduce you to operating system concepts
- Hard to use a computer without interacting with OS
- Understanding the OS makes you a more effective programmer
• Cover important systems concepts in general
- Caching, concurrency, memory management, I/O, protection
• Teach you to deal with larger software systems
- Programming assignments much larger than many courses
- Warning: Many people will consider course very hard
- In past, majority of people report ≥15 hours/week
6/33
Programming Assignments
• Implement parts of Pintos operating system
- Built for x86 hardware, you will use hardware emulator
10/33
What is an operating system?
• Layer between applications and hardware
13/33
Multitasking
14/33
Multitasking
14/33
Multi-user OSes
16/33
Typical OS structure
P1 P2 P3 P4
user
kernel
VM file
IPC system
sockets scheduler
TCP/IP device device device
driver driver driver
18/33
System calls (continued)
• Goal: Do things app. can’t do in unprivileged mode
- Like a library call, but into more privileged kernel code
• Kernel supplies well-defined system call interface
- Applications set up syscall arguments and trap to kernel
- Kernel performs operation and returns result
• Higher-level functions built on syscall interface
- printf, scanf, gets, etc. all user-level code
• Example: POSIX/UNIX interface
- open, close, read, write, ...
19/33
System call example
21/33
Error returns
• What if open fails? Returns -1 (invalid fd)
• Most system calls return -1 on failure
- Specific kind of error in global int errno
• #include <sys/errno.h> for possible values
- 2 = ENOENT “No such file or directory”
- 13 = EACCES “Permission Denied”
• perror function prints human-readable message
- perror ("initfile");
→ “initfile: No such file or directory”
22/33
Operations on file descriptors
• int read (int fd, void *buf, int nbytes);
- Returns number of bytes read
- Returns 0 bytes at end of file, or -1 on error
• int write (int fd, void *buf, int nbytes);
- Returns number of bytes written, -1 on error
• off t lseek (int fd, off t pos, int whence);
- whence: 0 – start, 1 – current, 2 – end
⊲ Returns previous file offset, or -1 on error
23/33
File descriptor numbers
• File descriptors are inherited by processes
- When one process spawns another, same fds by default
• Descriptors 0, 1, and 2 have special meaning
- 0 – “standard input” (stdin in ANSI C)
- 1 – “standard output” (stdout, printf in ANSI C)
- 2 – “standard error” (stderr, perror in ANSI C)
- Normally all three attached to terminal
• Example: type.c
- Prints the contents of a file to stdout
24/33
type.c
void
typefile (char *filename)
{
int fd, nread;
char buf[1024];
close (fd);
}
25/33
Different system contexts
• A system can typically be in one of several contexts
• User-level – running an application
• Kernel process context
- Running kernel code on behalf of a particular process
- E.g., performing system call
- Also exception (mem. fault, numeric exception, etc.)
- Or executing a kernel-only process (e.g., network file server)
• Kernel code not associated w. a process
- Timer interrupt (hardclock)
- Device interrupt
- “Softirqs”, “Tasklets” (Linux-specific terms)
27/33
CPU preemption
• Protection mechanism to prevent monopolizing CPU
• E.g., kernel programs timer to interrupt every 10 ms
- Must be in supervisor mode to write appropriate I/O registers
- User code cannot re-program interval timer
• Kernel sets interrupt to vector back to kernel
- Regains control whenever interval timer fires
- Gives CPU to another process if someone else needs it
- Note: must be in supervisor mode to set interrupt entry points
- No way for user code to hijack interrupt handler
• Result: Cannot monopolize CPU with infinite loop
- At worst get 1/N of CPU with N CPU-hungry processes
28/33
Protection is not security
• How can you monopolize CPU?
29/33
Protection is not security
• How can you monopolize CPU?
• Use multiple processes
• For many years, could wedge most OSes with
int main() { while(1) fork(); }
- Keeps creating more processes until system out of proc. slots
29/33
Address translation
• Protect mem. of one program from actions of another
• Definitions
- Address space: all memory locations a program can name
- Virtual address: addresses in process’ address space
- Physical address: address of real memory
- Translation: map virtual to physical addresses
• Translation done on every load and store
- Modern CPUs do this in hardware for speed
• Idea: If you can’t name it, you can’t touch it
- Ensure one process’s translations don’t include any other
process’s memory
30/33
More memory protection
• CPU allows kernel-only virtual addresses
- Kernel typically part of all address spaces,
e.g., to handle system call in same address space
- But must ensure apps can’t touch kernel memory
• CPU lets OS disable virtual addresses
- Catch and halt buggy program that makes wild accesses
- Make virtual memory seem bigger than physical
(e.g., bring a page in from disk only when accessed)
• CPU enforced read-only virtual addresses useful
- E.g., allows sharing of code pages between processes
- Plus many other optimizations
• CPU enforced execute disable of VAs
- Makes certain code injection attacks harder
31/33
Resource allocation & performance
• Multitasking permits higher resource utilization
• Simple example:
- Process downloading large file mostly waits for network
- You play a game while downloading the file
- Higher CPU utilization than if just downloading
32/33
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
33/33