SE350: Operating Systems Lecture 2: OS Concepts Outline Brief - - PowerPoint PPT Presentation

se350 operating systems
SMART_READER_LITE
LIVE PREVIEW

SE350: Operating Systems Lecture 2: OS Concepts Outline Brief - - PowerPoint PPT Presentation

SE350: Operating Systems Lecture 2: OS Concepts Outline Brief history of OSs Four fundamental OS concepts Thread Address space Process Dual-mode operation/protection Very Brief History of OS Several distinct phases:


slide-1
SLIDE 1

SE350: Operating Systems

Lecture 2: OS Concepts

slide-2
SLIDE 2

Outline

  • Brief history of OS’s
  • Four fundamental OS concepts
  • Thread
  • Address space
  • Process
  • Dual-mode operation/protection
slide-3
SLIDE 3

Very Brief History of OS

  • Several distinct phases:
  • Hardware expensive, humans cheap
  • Eniac, … Multics

Thomas Watson was often called “the worlds greatest salesman” by the time

  • f his death in 1956

“I think there is a world market for maybe five computers.” – Thomas Watson, chairman of IBM, 1943

slide-4
SLIDE 4

Very Brief History of OS

  • Several distinct phases:
  • Hardware expensive, humans cheap
  • Eniac, … Multics
  • Hardware cheaper, humans expensive
  • PCs, workstations, rise of GUIs
  • Hardware very cheap, humans very expensive
  • Ubiquitous devices, widespread networking
slide-5
SLIDE 5

Very Brief History of OS

  • Several distinct phases:
  • Hardware expensive, humans cheap
  • Eniac, … Multics
  • Hardware cheaper, humans expensive
  • PCs, workstations, rise of GUIs
  • Hardware very cheap, humans very expensive
  • Ubiquitous devices, widespread networking
  • Rapid change in hardware leads to changing OS
  • Batch Þ multiprogramming Þ timesharing Þ GUI Þ ubiquitous devices
  • Gradual migration of features into smaller machines
  • Today
  • Small OS: 100K lines / Large: 20M lines (10M browser!)
  • 100-1000 people-years
slide-6
SLIDE 6

OS Archaeology

  • Due to high cost of building OS from scratch, most modern OS’s have long lineage
  • Multics Þ AT&T Unix Þ BSD Unix Þ Ultrix, SunOS, NetBSD,…
  • Mach (micro-kernel) + BSD Þ NextStep Þ XNU Þ Apple OS X, iPhone iOS
  • MINIX Þ Linux Þ Android, Chrome OS, RedHat, Ubuntu, Fedora, Debian, Suse,…
  • CP/M Þ QDOS Þ MS-DOS Þ Windows 3.1 Þ NT Þ 95 Þ 98 Þ 2000 Þ

XP ÞVista Þ 7 Þ 8 Þ 10 Þ …

slide-7
SLIDE 7

Today: Four Fundamental OS Concepts

  • Th

Threa ead

  • Single unique execution context which fully describes program state
  • Program counter, registers, execution flags, stack
  • Ad

Address ess sp space ce (with transl slation

  • n)
  • Address space which is distinct from machine’s physical memory addresses
  • Pr

Process ess

  • Instance of executing program consisting of address space and 1+ threads
  • Dua

Dual-mo mode operati ation/prote tecti tion

  • Only “system” can access certain resources
  • OS and hardware are protected from user programs
  • User programs are isolated from one another by controlling translation from

program virtual addresses to machine physical addresses

slide-8
SLIDE 8

OS Bottom Line: Run Programs

  • Load instruction and data segments of

executable file into memory

  • Create stack and heap
  • “Transfer control to program”
  • Provide services to program
  • While protecting OS and program

Heap Stack O S L

  • a

d s Memory 0x000… 0xFFF… Instructions Data OS

Executable Image

Data Instructions

Compiler

Source Code

Edits Processor Registers

PC

OS Starts Execution

learnworthy.net

slide-9
SLIDE 9

Instruction Cycle: Fetch, Decode, Execute

PC

Memory Instructions Data Decode Instruction fetch ALU Registers

Execute

Next

slide-10
SLIDE 10

What Happens During Program Execution?

  • Execution sequence:
  • Fetch instruction at PC
  • Decode
  • Execute (possibly using registers)
  • Write results to registers/memory
  • PC ← Next(PC)
  • Repeat

PC

Memory Instructions Data Decode ALU Registers Next

Next instruction or jump to new address …

slide-11
SLIDE 11

Thread (1st OS Concept)

  • Thread is single unique execution context
  • Program counter (PC), registers, execution flags, stack
  • Thread is executing on processor when it resides in processor’s registers
  • Registers hold root state of thread (the rest is “in memory”)
  • Registers are defined by instruction set architecture (ISA) or by compiler
  • Stack pointer (SP) holds address of top of stack
  • Other conventions: frame pointer, heap pointer, data
  • PC register holds the address of executing instruction in the thread
slide-12
SLIDE 12

Address Space (2nd OS Concept)

  • Address space is set of accessible addresses and

state associated with them

  • For 32-bit processor: 232 = ~4 billion addresses
  • What happens when you read or write to address?
  • Perhaps nothing
  • Perhaps acts like regular memory
  • Perhaps ignores writes
  • Perhaps causes I/O operation
  • (Memory-mapped I/O)
  • Perhaps causes exception (fault)

Heap Stack 0x000… Instructions Data 0xFFF…

slide-13
SLIDE 13

Address Space Layout of C Programs

#include <stdio.h> #include <stdlib.h> int x; int y = 15; int main(int argc, char *argv[]) { int *values; int I; values = (int *)malloc(sizeof(int)*5); for (i = 0; i < 5; i++) values[i] = i; return 0; } Binary Code Initialized Data Uninitialized Data Heap Command line args and environment vars Stack

slide-14
SLIDE 14

Multiprogramming: Multiple Threads

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc n Proc 2

Code Data Heap Stack

slide-15
SLIDE 15

Time Sharing

  • How can we give illusion of multiple processors with single processor?
  • Multiplex in time!
  • Each virtual “CPU” needs structure to hold
  • PC, SP

, and rest of registers (integer, floating point, …)

  • How do we switch from one vCPU to next?
  • Save PC, SP

, and registers in current state block

  • Load PC, SP

, and registers from new state block

  • What triggers switch?
  • Timer, voluntary yield, I/O, …

vCPU3 vCPU2 vCPU1 Shared Memory vCPU1 vCPU2 vCPU3 vCPU1 vCPU2 Time

slide-16
SLIDE 16

The Basic Problem of Concurrency

  • The basic problem of concurrency involves resources
  • Hardware: single CPU, single DRAM, single I/O devices
  • Multiprogramming API: processes think they have exclusive access to

shared resources

  • OS should coordinate all activity
  • Multiple processes, I/O interrupts, …
  • How can it keep all these things straight?
  • Basic idea is to use virtual machine abstraction
  • Simple machine abstraction for processes
  • Multiplex these abstract machines
  • Dijkstra did this for the “THE system”
  • Few thousand lines vs 1 million lines in OS 360 (1K bugs)
slide-17
SLIDE 17

Properties of This Simple Multiprogramming Technique

  • All vCPUs share same non-CPU resources
  • I/O devices, memory, …
  • Consequence of sharing
  • Each thread can access data of every other thread

(good for sharing, bad for protection)

  • Threads can share instructions

(good for sharing, bad for protection)

  • Can threads overwrite OS functions?
  • This (unprotected) model is common in
  • Embedded applications
  • Windows 3.1/Early Macintosh (switch only with yield)
  • Windows 95-ME (switch with both yield and timer)
slide-18
SLIDE 18

Protection

  • OS must protect itself from user programs
  • Reliability: compromising OS generally causes it to crash
  • Security: limit scope of what processes can do
  • Privacy: limit each process to data it is permitted to access
  • Fairness: enforce appropriate share of resources (CPU time, memory, I/O, etc)
  • It must protect user programs from one another
  • Primary mechanism is to limit translation from program address space to

physical memory space

  • Can only touch what is mapped into process address space
  • There are additional mechanisms as well
  • Privileged instructions, in/out instructions, special registers
  • syscall processing, subsystem implementation
  • (e.g., file access rights, etc)
slide-19
SLIDE 19

Process (3rd OS Concept)

  • Process: execution environment with restricted rights
  • Address space with one or more threads
  • Owns memory (address space)
  • Owns file descriptors, file system context, …
  • Encapsulates one or more threads sharing process resources
  • Why processes?
  • Protected from each other!
  • OS Protected from them
  • Memory protection
  • Threads more efficient than processes (later)
  • Fundamental tradeoff between protection and efficiency
  • Communication easier within a process
  • Communication harder between processes
  • Application instance consists of one or more processes
slide-20
SLIDE 20

Single and Multithreaded Processes

  • Threads encapsulate concurrency and are active components
  • Address spaces encapsulate protection and are passive part
  • Keeps buggy program from trashing system
  • Why have multiple threads per address space?
  • Processes are expensive to start, switch between, and communicate between
slide-21
SLIDE 21

Dual-Mode Operation (4th OS Concept)

  • Hardware provides at least two modes
  • Kernel mode (or “supervisor” or “protected”)
  • User mode, which is how normal programs are executed
  • How can hardware support dual-mode operation?
  • A bit of state (user/system mode bit)
  • Certain operations/actions only permitted in system/kernel mode
  • In user mode they fail or trap
  • User to kernel transition sets system mode AND saves user PC
  • OS code carefully puts aside user state then performs necessary actions
  • Kernel to user transition clears system mode AND restores user PC
  • E.g., rfi: return-from-interrupt
slide-22
SLIDE 22

User/Kernel (Privileged) Mode

User Mode Kernel Mode

Hardware

Full HW access Limited HW access exec syscall exit rtn interrupt rfi exception

slide-23
SLIDE 23

Simple Memory Protections: Base and Bound (B&B)

Code Data Heap Stack

0x000… 0x010… 0x000… 0xFFF…

+

Base Bound

Code Data Heap Stack

0x100… 0x110…

Virtual Address

0x001… 0x101…

Physical Address

0x100… 0x110…

>

Raise Exception

slide-24
SLIDE 24

Towards Virtual Addresses

  • What are upsides of B&B?
  • OS protection and program isolation
  • Low overhead address translation
  • What are downsides of B&B?
  • Expandable heap?
  • Expandable stack?
  • Memory sharing between processes?
  • Non-relative addresses – hard to move memory around
  • Memory fragmentation
slide-25
SLIDE 25

Memory Processor

Address Space Translation

  • Program operates in address space that is distinct from

physical memory space of machine

0x000… 0xFFF… Translator Virtual Address Physical Address

slide-26
SLIDE 26

Virtual Address Example

int staticVar = 0; // a static variable int main() { staticVar += 1; usleep(5000000); // sleep for 5 seconds printf("static address: %x, value: %d\n", &staticVar, staticVar); }

  • What happens if we run two instances of this program at the

same time?

  • What if we took the address of a procedure local variable in

two copies of the same program running at the same time?

slide-27
SLIDE 27

Putting it All Together: OS Loads Process (with B&B)

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc 2 0x0000… 0xFFFF…

Base Bound uPC Regs 1 sysmode … PC

0x1000… 0x1100… 0x3000… 0x3080…

SP

slide-28
SLIDE 28

OS Gets Ready to Execute Process (with B&B)

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc 2 0x0000… 0xFFFF…

Base 0x1000… 0x1100… Bound 0x0001 uPC 0x00FF Regs 1 sysmode … PC

0x1000… 0x1100… 0x3000… 0x3080…

SP

  • Privileged Inst: set

special registers

slide-29
SLIDE 29

User Code Running (with B&B)

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc 2 0x0000… 0xFFFF…

Base 0x1000… 0x1100… Bound uPC 0x00FF Regs sysmode … 0x0001 PC

0x1000… 0x1100… 0x3000… 0x3080…

SP

  • How does OS

switch between processes?

  • First question: How

to return to OS?

slide-30
SLIDE 30

Three Types of Mode Transfer

  • Syscall
  • Process requests system service, e.g., exit
  • Like function call, but outside process
  • Process does not have address of system function to call
  • Like a Remote Procedure Call (RPC) – for later
  • OS marshalls syscall id and args in registers and exec syscall
  • Interrupt
  • External asynchronous event triggers context switch, e. g., Timer, I/O device
  • Independent of user process
  • Trap or exception
  • Internal synchronous event in process triggers context switch, e.g., protection

violation (segmentation fault), divide by zero, …

  • All 3 are UNPROGRAMMED CONTROL TRANSFER
  • How do we get address of unprogrammed control transfer?
slide-31
SLIDE 31

Interrupt Vector

  • Table set up by OS pointing to code to run on different events

Interrupt Vector Table Processor Register

h a n d l e Ti m e r I n t e r r u p t ( ) { . . . } h a n d l e D i v i d e B y Z e r o ( ) { . . . } h a n d l e S y s t e m C a l l ( ) { . . . }

slide-32
SLIDE 32

User to Kernel Switch (with B&B)

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc 2 0x0000… 0xFFFF…

Base 0x1000… 0x1100… Bound uPC 0x00FF… … … Regs sysmode … 0x00001234 PC

0x1000… 0x1100… 0x3000… 0x3080…

SP

slide-33
SLIDE 33

Interrupt (with B&B)

Code Data Heap Stack Code Data Heap Stack Code Data Heap Stack

OS Proc 1 Proc 2 0x0000… 0xFFFF…

Base 0x1000… 0x1100… Bound 0x00001234 uPC 0x00FF… … … Regs 1 sysmode … IntrpVector[i] PC

0x1000… 0x1100… 0x3000… 0x3080…

SP

  • How to save

registers and set up system stack?

0x1000… 0x1100… 0x00001234 0x00FF… … … …

OS stores copy

  • f registers in

its memory

slide-34
SLIDE 34

Summery: Four Fundamental OS Concepts

  • Th

Threa ead

  • Single unique execution context which fully describes program state
  • Program counter, registers, execution flags, stack
  • Ad

Address ess sp space ce (with transl slation

  • n)
  • Address space which is distinct from machine’s physical memory addresses
  • Pr

Process ess

  • Instance of executing program consisting of address space and 1+ threads
  • Dua

Dual-mo mode operati ation/prote tecti tion

  • Only “system” can access certain resources
  • OS and hardware are protected from user programs
  • User programs are isolated from one another by controlling translation from

program virtual addresses to machine physical addresses

slide-35
SLIDE 35

Questions?

globaldigitalcitizen.org

slide-36
SLIDE 36

Acknowledgment

  • Slides by courtesy of Anderson, Culler, Stoica,

Silberschatz, Joseph, and Canny