45
University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

Embed Size (px)

Citation preview

Page 1: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

Today Lab 4 due Wednesday! HW 4 out today!

What a process again?

Page 2: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

2

Fork-Exec fork-exec model:

fork() creates a copy of the current process execve() replaces the current process’ code & address space with

the code for a different program There is a whole family of exec calls – see exec(3) and execve(2)

// Example arguments: path="/usr/bin/ls”,// argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULLvoid fork_exec(char *path, char *argv[]){ pid_t pid = fork(); if (pid != 0) { printf("Parent: created a child %d\n”, pid); } else { printf("Child: exec-ing new program now\n"); execv(path, argv); } printf("This line printed by parent only!\n");}

Page 3: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

3

Exec-ing a new program

Stack

Code: /usr/bin/bashData

Heap

Stack

Code: /usr/bin/bashData

Heap

Stack

Code: /usr/bin/bashData

Heap

Stack

Code: /usr/bin/lsData

fork():

exec():

Very high-level diagram of what happens when you run the command ”ls” in a Linux shell:

parent child child

Page 4: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

4

execve: Loading and Running Programs int execve(

char *filename, char *argv[], char *envp[])

Loads and runs in current process: Executable filename With argument list argv And environment variable list envp

Env. vars: “name=value” strings(e.g. “PWD=/homes/iws/pjh”)

execve does not return (unless error) Overwrites code, data, and stack

Keeps pid, open files, a few other items

Null-terminatedenv var strings

unused

Null-terminatedcmd line arg strings

envp[n] == NULLenvp[n-1]

envp[0]…

Linker vars

argv[argc] == NULLargv[argc-1]

argv[0]…

envp

argcargv

Stack bottom

Stack frame for main Stack top

Page 5: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

5

exit: Ending a process void exit(int status)

Exits a process Status code: 0 is used for a normal exit, nonzero for abnormal exit

atexit() registers functions to be executed upon exit

void cleanup(void) { printf("cleaning up\n");}

void fork6() { atexit(cleanup); fork(); exit(0);}

Page 6: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

6

Zombies Idea

When process terminates, it still consumes system resources Various tables maintained by OS

Called a “zombie” A living corpse, half alive and half dead

Reaping Performed by parent on terminated child Parent is given exit status information Kernel discards process

What if parent doesn’t reap? If any parent terminates without reaping a child, then child will be

reaped by init process (pid == 1) But in long-running processes we need explicit reaping

e.g., shells and servers

Page 7: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

7

wait: Synchronizing with Children int wait(int *child_status)

Suspends current process (i.e. the parent) until one of its children terminates

Return value is the pid of the child process that terminated On successful return, the child process is reaped

If child_status != NULL, then the int that it points to will be set to a status indicating why the child process terminated

There are special macros for interpreting this status – see wait(2)

If parent process has multiple children, wait() will return when any of the children terminates waitpid() can be used to wait on a specific child process

Page 8: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

8

wait Examplevoid fork_wait() { int child_status; pid_t child_pid;

if (fork() == 0) { printf("HC: hello from child\n"); } else { child_pid = wait(&child_status); printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0);}

HC Bye

CT Bye

Page 9: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

9

Process management summary fork gets us two copies of the same process (but fork()

returns different values to the two processes) execve has a new process substitute itself for the one that

called it Two-process program:

First fork() if (pid == 0) { //child code } else { //parent code }

Two different programs: First fork() if (pid == 0) { execve() } else { //parent code } Now running two completely different programs

wait / waitpid used to synchronize parent/child execution and to reap child process

Page 10: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

10

Summary Processes

At any given time, system has multiple active processes Only one can execute at a time, but each process appears to have total

control of the processor OS periodically “context switches” between active processes

Implemented using exceptional control flow Process management

fork-exec model

Page 11: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

11

Roadmap

car *c = malloc(sizeof(car));c->miles = 100;c->gals = 17;float mpg = get_mpg(c);free(c);

Car c = new Car();c.setMiles(100);c.setGals(17);float mpg = c.getMPG();

get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret

Java:C:

Assembly language:

Machine code:

01110100000110001000110100000100000000101000100111000010110000011111101000011111

Computer system:

OS:

Data & addressingIntegers & floatsMachine code & Cx86 assembly programmingProcedures & stacksArrays & structsMemory & cachesProcessesVirtual memoryMemory allocationJava vs. C

Page 12: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

12

Processes Definition: A process is an instance of a running program

One of the most important ideas in computer science Not the same as “program” or “processor”

Process provides each program with two key abstractions:

Page 13: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

13

Processes Definition: A process is an instance of a running program

One of the most important ideas in computer science Not the same as “program” or “processor”

Process provides each program with two key abstractions: Logical control flow

Each process seems to have exclusive use of the CPU Private virtual address space

Each process seems to have exclusive use of main memory

How are these illusions maintained?

Page 14: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

14

Processes Definition: A process is an instance of a running program

One of the most important ideas in computer science Not the same as “program” or “processor”

Process provides each program with two key abstractions: Logical control flow

Each process seems to have exclusive use of the CPU Private virtual address space

Each process seems to have exclusive use of main memory

How are these illusions maintained? Process executions interleaved (multi-tasking) – last time Address spaces managed by virtual memory system – today!

Page 15: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

15

Virtual Memory (VM) Overview and motivation VM as tool for caching Address translation VM as tool for memory management VM as tool for memory protection

Page 16: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

16

Virtual Memory (Previous Lectures) Programs refer to virtual memory addresses

movl (%ecx),%eax Conceptually memory is just a very large array of bytes Each byte has its own address System provides address space private to particular “process”

Allocation: Compiler and run-time system Where different program objects should be stored All allocation within single virtual address space

What problems does virtual memory solve?

FF∙∙∙∙∙∙F

00∙∙∙∙∙∙0

Page 17: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

17

Problem 1: How Does Everything Fit?

64-bit addresses:16 Exabyte

Physical main memory:Few Gigabytes

?

And there are many processes ….

Page 18: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

18

Problem 2: Memory Management

Physical main memory

What goes where?

stackheap.text.data

Process 1Process 2Process 3…Process n

x

Page 19: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

19

Problem 3: How To ProtectPhysical main memory

Process i

Process j

Problem 4: How To Share?Physical main memory

Process i

Process j

Page 20: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

20

How would you solve those problems?

Page 21: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

21

Indirection “Any problem in computer science can be solved by adding another level

of indirection”

Without Indirection

With Indirection

Name Thing

Name Thing

Thing

Page 22: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

22

Indirection Indirection: the ability to reference something using a name, reference, or

container instead the value itself. A flexible mapping between a name and a thing allows changing the thing without notifying holders of the name.

Without Indirection

With Indirection

Examples: Domain Name Service (DNS) name->IP address, phone system (e.g., cell phone number portability), snail mail (e.g., mail forwarding), 911 (routed to local office), DHCP, call centers that route calls to available operators, etc.

Name Thing

Name Thing

Thing

Page 23: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

23

Solution: Level Of Indirection

Each process gets its own private virtual address space Solves the previous problems

Physical memory

Virtual memory

Virtual memory

Process 1

Process n

mapping

Page 24: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

24

Address Spaces Virtual address space: Set of N = 2n virtual addresses

{0, 1, 2, 3, …, N-1}

Physical address space: Set of M = 2m physical addresses (n > m){0, 1, 2, 3, …, M-1}

Every byte in main memory: one physical address; zero, one, or more virtual addresses

Page 25: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

Mapping

Vir

tual A

dd

ress Physical

Memory

Disk

A virtual address can be mapped to either physical memory or disk.

Think of memory as a cache for the whole address space.

25

Page 26: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

26

A System Using Physical Addressing

Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames

0:1:

M-1:

Main memory

CPU

2:3:4:5:6:7:

Physical address(PA)

Data word

8: ...

4

Page 27: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

27

A System Using Virtual Addressing

Used in all modern desktops, laptops, servers One of the great ideas in computer science

0:1:

M-1:

Main memory

MMU

2:3:4:5:6:7:

Physical address(PA)

Data word

8: ...

CPU

Virtual address(VA)

CPU Chip

44100

Page 28: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

28

VM and the Memory Hierarchy Think of virtual memory as an array of N = 2n contiguous

bytes stored on a disk Then physical main memory (DRAM) is used as a cache for

the virtual memory array The cache blocks are called pages (size is P = 2p bytes)

PP 2m-p-1

Physical memory

Empty

Empty

Uncached

VP 0VP 1

VP 2n-p-1

Virtual memory

UnallocatedCachedUncachedUnallocatedCachedUncached

PP 0PP 1

EmptyCached

0

N-1M-1

0

Virtual pages (VPs) stored on disk

Physical pages (PPs) cached in DRAM

Page 29: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

29

Memory Hierarchy: Core 2 Duo

Disk

Main Memory

L2 unified cache

L1 I-cache

L1 D-cache

CPU Reg

2 B/cycle8 B/cycle16 B/cycle 1 B/30 cyclesThroughput:Latency: 100 cycles14 cycles3 cycles millions

~4 MB

32 KB

~4 GB ~500 GB

Not drawn to scale

L1/L2 cache: 64 B blocks

Miss penalty (latency): 33x

Miss penalty (latency): 10,000x

Page 30: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

30

DRAM Cache Organization DRAM cache organization driven by the enormous miss

penalty DRAM is about 10x slower than SRAM Disk is about 10,000x slower than DRAM

(for first byte; faster for next byte)

Consequences? Block size? Associativity? Write-through or write-back?

Page 31: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

31

DRAM Cache Organization DRAM cache organization driven by the enormous miss

penalty DRAM is about 10x slower than SRAM Disk is about 10,000x slower than DRAM

(for first byte; faster for next byte)

Consequences Large page (block) size: typically 4-8 KB, sometimes 4 MB Fully associative

Any VP can be placed in any PP Requires a “large” mapping function – different from CPU caches

Highly sophisticated, expensive replacement algorithms Too complicated and open-ended to be implemented in hardware

Write-back rather than write-through

Page 32: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

32

Indexing into the “DRAM Cache”

How do we perform the VA -> PA translation?

0:1:

M-1:

Main memory

MMU

2:3:4:5:6:7:

Physical address(PA)

Data word

8: ...

CPU

Virtual address(VA)

CPU Chip

44100

Page 33: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

33

Address Translation: Page Tables A page table (PT) is an array of page table entries (PTEs) that

maps virtual pages to physical pages.

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory (disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

How many page tables are in the system?

Page 34: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

34

Address Translation: Page Tables A page table (PT) is an array of page table entries (PTEs) that

maps virtual pages to physical pages.

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory (disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

How many page tables are in the system?One per process

Page 35: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

35

Address Translation With a Page Table

Virtual page number (VPN) Virtual page offset (VPO)

Physical page number (PPN) Physical page offset (PPO)

Virtual address (VA)

Physical address (PA)

Valid Physical page number (PPN)

Page table base register

(PTBR)

Page table Page table address for process

Valid bit = 0:page not in memory

(page fault)

In most cases, the hardware (the MMU) can perform this translation on its own, without software assistance

Page 36: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

36

Page Hit Page hit: reference to VM byte that is in physical memory

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory(disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 37: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

37

Page Fault Page fault: reference to VM byte that is NOT in physical

memory

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory(disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

What happens when a page fault occurs?

Page 38: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

38

User writes to memory location That portion (page) of user’s memory

is currently on disk

Page handler must load page into physical memory Returns to faulting instruction: mov is executed again! Successful on second try

int a[1000];main (){ a[500] = 13;}

80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10

User Process OS

exception: page faultCreate page and load into memoryreturns

movl

Fault Example: Page Fault

Page 39: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

39

Handling Page Fault Page miss causes page fault (an exception)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory(disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 40: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

40

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 4

Virtual memory(disk)

Valid01

010

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 41: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

41

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 3

Virtual memory(disk)

Valid01

100

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 42: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

42

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Offending instruction is restarted: page hit!

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7VP 3

Virtual memory(disk)

Valid01

100

10

1

Physical pagenumber or

disk addressPTE 0

PTE 7

PP 0VP 2VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 43: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

43

Why does it work?

Page 44: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

44

Why does it work? Locality Virtual memory works well because of locality

Same reason that L1 / L2 / L3 caches work

The set of virtual pages that a program is “actively” accessing at any point in time is called its working set Programs with better temporal locality will have smaller working sets

If (working set size < main memory size): Good performance for one process after compulsory misses

If (SUM(working set sizes) > main memory size): Thrashing: Performance meltdown where pages are swapped (copied)

in and out continuously

Page 45: University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

University of Washington

45