Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix:...

61
Advanced Operating Systems (CS 202) Process (continued) 1

Transcript of Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix:...

Page 1: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Advanced Operating Systems

(CS 202)

Process (continued)

1

Page 2: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Process system call API

Process creation: how to create a new process?

Process termination: how to terminate and clean up a

process

Coordination between processes

Wait, waitpid, signal, inter-process communication,

synchronization

Other

E.g., set quotas or priorities, examine usage, …

2

Page 3: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

3

Process Creation

A process is created by another process

Why is this the case?

Parent is creator, child is created (Unix: ps “PPID” field)

What creates the first process (Unix: init (PID 0 or 1))?

In some systems, the parent defines (or donates)

resources and privileges for its children

Unix: Process User ID is inherited – children of your shell

execute with your privileges

After creating a child, the parent may either wait for it to

finish its task or continue in parallel (or both)

Page 4: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

4

Process Creation: Windows

The system call on Windows for creating a process is

called, surprisingly enough, CreateProcess:BOOL CreateProcess(char *prog, char *args) (simplified)

CreateProcess

Creates and initializes a new PCB

Creates and initializes a new address space

Loads the program specified by “prog” into the address space

Copies “args” into memory allocated in address space

Initializes the saved hardware context to start execution at main

(or wherever specified in the file)

Places the PCB on the ready queue

Page 5: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

5

Process Creation: Unix

In Unix, processes are created using fork()int fork()

fork()Creates and initializes a new PCB

Creates a new address space

Initializes the address space with a copy of the entire contents of the address space of the parent

Initializes the kernel resources to point to the resources used by parent (e.g., open files)

Places the PCB on the ready queue

Fork returns twiceReturns the child’s PID to the parent, “0” to the child

Page 6: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

6

fork()

int main(int argc, char *argv[])

{

char *name = argv[0];

int child_pid = fork();

if (child_pid == 0) {

printf(“Child of %s is %d\n”, name, getpid());

return 0;

} else {

printf(“My child is %d\n”, child_pid);

return 0;

}

}

What does this program print?

Page 7: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

7

Example Output

[well ~]$ gcc t.c

[well ~]$ ./a.out

My child is 486

Child of a.out is 486

Page 8: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

8

Duplicating Address Spaces

child_pid = fork();

if (child_pid == 0) {

printf(“child”);

} else {

printf(“parent”);

}

Parent Child

child_pid = fork();

if (child_pid == 0) {

printf(“child”);

} else {

printf(“parent”);

}

PC

child_pid = 486 child_pid = 0

PC

Page 9: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

9

Divergence

child_pid = fork();

if (child_pid == 0) {

printf(“child”);

} else {

printf(“parent”);

}

Parent Child

child_pid = fork();

if (child_pid == 0) {

printf(“child”);

} else {

printf(“parent”);

}

PC

PC

child_pid = 486 child_pid = 0

Page 10: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

10

Example Continued

[well ~]$ gcc t.c

[well ~]$ ./a.out

My child is 486

Child of a.out is 486

[well ~]$ ./a.out

Child of a.out is 498

My child is 498

Why is the output in a different order?

Page 11: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

11

Why fork()?

Very useful when the child…

Is cooperating with the parent

Relies upon the parent’s data to accomplish its

task

Example: Web serverwhile (1) {

int sock = accept();

if ((child_pid = fork()) == 0) {

Handle client request

} else {

Close socket

}

}

Page 12: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

12

Process Creation: Unix (2)

Wait a second. How do we actually start a new

program?int exec(char *prog, char *argv[])

exec()

Stops the current process

Loads the program “prog” into the process’ address space

Initializes hardware context and args for the new program

Places the PCB onto the ready queue

Note: It does not create a new process

What does it mean for exec to return?

What does it mean for exec to return with an error?

Page 13: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

13

Process Termination

All good processes must come to an end. But

how?

Unix: exit(int status), NT: ExitProcess(int status)

Essentially, free resources and terminate

Terminate all threads (next lecture)

Close open files, network connections

Allocated memory (and VM pages out on disk)

Remove PCB from kernel data structures, delete

Note that a process does not need to clean up

itself

OS will handle this on its behalf

Page 14: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

14

wait() a second…

Often it is convenient to pause until a child process has

finished

Think of executing commands in a shell

Use wait() (WaitForSingleObject)

Suspends the current process until a child process ends

waitpid() suspends until the specified child process ends

Wait has a return value…what is it?

Unix: Every process must be reaped by a parent

What happens if a parent process exits before a child?

What do you think is a “zombie” process?

Page 15: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

15

Unix Shells

while (1) {

char *cmd = read_command();

int child_pid = fork();

if (child_pid == 0) {

Manipulate STDIN/OUT/ERR file descriptors for pipes,

redirection, etc.

exec(cmd);

panic(“exec failed”);

} else {

if (!(run_in_background))

waitpid(child_pid);

}

}

Page 16: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Some issues with processes

Creating a new process is costly because of new address space and data structures that must be allocated and initialized

Recall struct proc in xv6 or Solaris

Communicating between processes is costly because most communication goes through the OS

Inter Process Communication (IPC) – we will discuss later

Overhead of system calls and copying data

16

Page 17: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Parallel Programs

• Also recall our Web server example that forks off copies

of itself to handle multiple simultaneous requests

• To execute these programs we need to

• Create several processes that execute in parallel

• Cause each to map to the same address space to share data

• They are all part of the same computation

• Have the OS schedule these processes in parallel

• This situation is very inefficient (CoW helps)

• Space: PCB, page tables, etc.

• Time: create data structures, fork and copy addr space, etc.

17

Page 18: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

18

Rethinking ProcessesWhat is similar in these cooperating processes?

They all share the same code and data (address space)

They all share the same privileges

They all share the same resources (files, sockets, etc.)

What don’t they share?

Each has its own execution state: PC, SP, and registers

Key idea: Separate resources from execution state

Exec state also called thread of control, or thread

Page 19: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

19

Threads

Separate execution and resource container roles

u The thread defines a sequential execution stream within a

process (PC, SP, registers)

u The process defines the address space, resources, and general

process attributes (everything but threads)

Threads become the unit of scheduling

u Processes are now the containers in which threads execute

u Processes become static, threads are the dynamic entities

Page 20: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

20

Recap: Process Address Space

Stack

0x00000000

0xFFFFFFFF

Code

(Text Segment)

Static Data

(Data Segment)

Heap

(Dynamic Memory Alloc)Address

Space

SP

PC

Page 21: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

21

Threads in a Process

Stack (T1)

Code

Static Data

Heap

Stack (T2)

Stack (T3)

Thread 1

Thread 3

Thread 2

PC (T1)

PC (T3)PC (T2)

Page 22: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

22

Threads: Concurrent Servers

Using fork() to create new processes to handle

requests in parallel is overkill for such a simple

task

Recall our forking Web server:

while (1) {

int sock = accept();

if ((child_pid = fork()) == 0) {

Handle client request

Close socket and exit

} else {

Close socket

}

}

Page 23: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

23

Threads: Concurrent Servers

Instead, we can create a new thread for each

request

web_server() {

while (1) {

int sock = accept();

thread_fork(handle_request, sock);

}

}

handle_request(int sock) {

Process request

close(sock);

}

Page 24: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

24

Implementing threads

Kernel Level Threads

All thread operations are implemented in the kernel

The OS schedules all of the threads in the system

Don’t have to separate from processes

OS-managed threads are called kernel-level threads or

lightweight processes

Windows: threads

Solaris: lightweight processes (LWP)

POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM

Page 25: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

25

Kernel Thread (KLT) Limitations

KLTs make concurrency cheaper than processes

Much less state to allocate and initialize

However, there are a couple of issues

Issue 1: KLT overhead still high

Thread operations still require system calls

Ideally, want thread operations to be as fast as a procedure call

Issue 2: KLTs are general; unaware of application needs

Alternative: User-level threads (ULT)

Page 26: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

26

Alternative: User-Level Threads

Implement threads using user-level library

ULTs are small and fast

A thread is simply represented by a PC, registers, stack, and

small thread control block (TCB)

Creating a new thread, switching between threads, and

synchronizing threads are done via procedure call

No kernel involvement

User-level thread operations 100x faster than kernel threads

pthreads: PTHREAD_SCOPE_PROCESS

Page 27: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

27

Summary KLT vs. ULT

Kernel-level threads

Integrated with OS (informed scheduling)

Slow to create, manipulate, synchronize

User-level threads

Fast to create, manipulate, synchronize

Not integrated with OS (uninformed scheduling)

Understanding the differences between

kernel and user-level threads is important

For programming (correctness, performance)

For test-taking ☺

Page 28: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

28

Sample Thread Interface

thread_fork(procedure_t)

Create a new thread of control

Also thread_create(), thread_setstate()

thread_stop()

Stop the calling thread; also thread_block

thread_start(thread_t)

Start the given thread

thread_yield()

Voluntarily give up the processor

thread_exit()

Terminate the calling thread; also thread_destroy

Page 29: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Advanced Operating Systems

(CS 202)

Virtual Memory

29

Page 30: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

OS Abstractions

30

Operating System

Hardware

Applications

CPU Disk RAM

Process File system Virtual memory

Page 31: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

An Example Memory Hierarchy

Registers

L1 cache(SRAM)

Main memory(DRAM)

Local secondary storage(local disks)

Larger, slower, cheaper per byte

Remote secondary storage(tapes, distributed file systems, Web servers)

Local disks hold files retrieved from disks on remote network servers

Main memory holds disk blocks retrieved from local disks

L2 cache(SRAM)

L1 cache holds cache lines retrieved from L2 cache

CPU registers hold words retrieved from L1 cache

L2 cache holds cache lines retrieved from main memory

L0:

L1:

L2:

L3:

L4:

L5:

Smaller,faster,costlierper byte

31

Page 32: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Memory hierarchyCache: A smaller, faster storage device that acts as a staging area for

a subset of the data in a larger, slower device.

Fundamental idea of a memory hierarchy:

For each layer, faster, smaller device caches larger, slower device

.

Why do memory hierarchies work?

Because of locality!

Hit fast memory much more frequently even though its smaller

Thus, the storage at level k+1 can be slower (but larger and cheaper!)

Big Idea: The memory hierarchy creates a large pool of storage that

costs as much as the cheap storage near the bottom, but that serves

data to programs at the rate of the fast storage near the top.

32

Page 33: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

33

Virtual Addresses

Many ways to do this translation…Need hardware support and OS management algorithms

Requirements

Need protection – restrict which addresses jobs can use

Fast translation – lookups need to be fast

Fast change – updating memory hardware on context switch

vmapprocessorphysical

memory

virtual

addresses

physical

addresses

Page 34: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

34

Paging

Main Idea: split virtual address space into

multiple partitions

Each can go anywhere!

Virtual Memory

Page 1

Page 2

Page 3

Page N

Physical Memory

Paging solves the external fragmentation problem by

using fixed sized units in both physical and virtual memory But need to keep track

of where things are!

Page 35: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

35

Page Lookups

Page frame

Page number Offset

Virtual Address

Page TablePage frame Offset

Physical Address

Physical Memory

Page 36: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Why Virtual Memory (VM)?

Virtual memory is page with a new ingredientAllow pages to be on disk

In a special partition (or file) called swap

Motivation?Uses main memory efficiently

Use DRAM as a cache for the parts of a virtual address

space

Simplifies memory management Each process gets the same uniform linear address space

With VM, this can be big!

36

Page 37: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

VM as a Tool for Caching

Virtual memory is an array of N contiguous bytes

stored on disk.

The contents of the array on disk are cached in

physical memory (DRAM cache)

These cache blocks are called pages (size is P = 2p bytes)

PP 2m-p-1

Physical memory

Empty

Empty

Uncached

VP 0

VP 1

VP 2n-p-1

Virtual memory

Unallocated

Cached

Uncached

Unallocated

Cached

Uncached

PP 0

PP 1

Empty

Cached

0

N-1

M-1

0

Virtual pages (VPs) stored on disk

Physical pages (PPs) cached in DRAM

37

Page 38: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Page TablesA page table is an array of page table entries (PTEs) that maps

virtual pages to physical pages.

Per-process kernel data structure in DRAM

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7

VP 4

Virtual memory(disk)

Valid

0

1

0

1

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

38

Page 39: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Page Hit

Page hit: reference to VM word that is in physical memory (DRAM

cache hit)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7

VP 4

Virtual memory(disk)

Valid

0

1

0

1

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

39

Page 40: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Page Fault

Page fault: reference to VM word that is not in physical memory

(DRAM cache miss)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7

VP 4

Virtual memory(disk)

Valid

0

1

0

1

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

40

Page 41: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Handling Page FaultPage miss causes page fault (an exception)

null

null

Memory residentpage table

(DRAM)

Physical memory(DRAM)

VP 7

VP 4

Virtual memory(disk)

Valid

0

1

0

1

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

41

Page 42: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Handling Page FaultPage 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 7

VP 4

Virtual memory(disk)

Valid

0

1

0

1

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

42

Page 43: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Handling Page FaultPage 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 7

VP 3

Virtual memory(disk)

Valid

0

1

1

0

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0

VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

43

Page 44: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Handling Page FaultPage 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 7

VP 3

Virtual memory(disk)

Valid

0

1

1

0

0

1

0

1

Physical pagenumber or

disk address

PTE 0

PTE 7

PP 0

VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

44

Page 45: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

VM as a Tool for Mem ManagementKey idea: each process has its own virtual address space

It can view memory as a simple linear array

Mapping function scatters addresses through physical memory

Well chosen mappings simplify memory allocation and management

Virtual Address Space for Process 1:

Physical Address Space (DRAM)

0

N-1

(e.g., read-only library code)

Virtual Address Space for Process 2:

VP 1

VP 2...

0

N-1

VP 1

VP 2...

PP 2

PP 6

PP 8

...

0

M-1

Address translation

45

Page 46: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

VM as a Tool for Mem Management

Memory allocation

Each virtual page can be mapped to any physical page

A virtual page can be stored in different physical pages at different times

Sharing code and data among processes

Map virtual pages to the same physical page (here: PP 6)

Virtual Address Space for Process 1:

Physical Address Space (DRAM)

0

N-1

(e.g., read-only library code)

Virtual Address Space for Process 2:

VP 1

VP 2...

0

N-1

VP 1

VP 2...

PP 2

PP 6

PP 8

...

0

M-1

Address translation

46

Page 47: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Sharing

Can map shared memory at same or different

virtual addresses in each process’ address

space

Different:

10th virtual page in P1 and 7th virtual page in P2 correspond to

the 2nd physical page

Flexible (no address space conflicts), but pointers inside the

shared memory segment are invalid

Same:

2nd physical page corresponds to the 10th virtual page in both

P1 and P2

Less flexible, but shared pointers are valid

47

Page 48: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Copy on Write

OSes spend a lot of time copying data

System call arguments between user/kernel space

Entire address spaces to implement fork()

Use Copy on Write (CoW) to defer large copies as long

as possible, hoping to avoid them altogether

Instead of copying pages, create shared mappings of parent

pages in child virtual address space

Shared pages are protected as read-only in parent and child

Reads happen as usual

Writes generate a protection fault, trap to OS, copy page, change

page mapping in client page table, restart write instruction

How does this help fork()?

48

Page 49: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Execution of fork()

Page 1

Physical Memory

Page 2

Parent process’s

page table

Page 1

Child process’s

page table

Page 2

49

Page 50: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

fork() with Copy on Write

Page 1

Physical Memory

Page 2

Parent process’s

page table

Page 1

Child process’s

page table

Page 2

Protection bits set to prevent either

process from writing to any page

When either process modifies Page 1,

page fault handler allocates new page

and updates PTE in child process

50

Page 51: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Simplifying Linking and Loading

Linking

Each program has similar virtual

address space

Code, stack, and shared libraries

always start at the same address

Loading

execve() allocates virtual pages

for .text and .data sections = creates PTEs marked as invalid

The .text and .data sections

are copied, page by page, on

demand by the virtual memory

system

Kernel virtual memory

Memory-mapped region forshared libraries

Run-time heap(created by malloc)

User stack(created at runtime)

Unused0

%esp

(stack pointer)

Memoryinvisible touser code

brk

0xc0000000

0x08048000

0x40000000

Read/write segment(.data, .bss)

Read-only segment(.init, .text, .rodata)

Loaded from the executable file

51

Page 52: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

VM as a Tool for Mem Protection

Extend PTEs with permission bits

Page fault handler checks these before remappingIf violated, send process SIGSEGV (segmentation fault)

Process i: AddressREAD WRITE

PP 6Yes No

PP 4Yes Yes

PP 2Yes

VP 0:

VP 1:

VP 2:

•••

Process j:

Yes

SUP

No

No

Yes

AddressREAD WRITE

PP 9Yes No

PP 6Yes Yes

PP 11Yes Yes

SUP

No

Yes

No

VP 0:

VP 1:

VP 2:

Physical Address Space

PP 2

PP 4

PP 6

PP 8PP 9

PP 11

52

Page 53: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Address Translation: Page Hit

1) Processor sends virtual address to MMU

2-3) MMU fetches PTE from page table in memory

4) MMU sends physical address to cache/memory

5) Cache/memory sends data word to processor

MMU Cache/MemoryPA

Data

CPUVA

CPU ChipPTEA

PTE1

2

3

4

5

53

Page 54: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Address Translation: Page Fault

1) Processor sends virtual address to MMU

2-3) MMU fetches PTE from page table in memory

4) Valid bit is zero, so MMU triggers page fault exception

5) Handler identifies victim (and, if dirty, pages it out to disk)

6) Handler pages in new page and updates PTE in memory

7) Handler returns to original process, restarting faulting instruction

MMU Cache/Memory

CPUVA

CPU ChipPTEA

PTE

1

2

3

4

5

Disk

Page fault handler

Victim page

New page

Exception

6

7

54

Page 55: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Integrating VM and Cache

VACPU MMU

PTEA

PTE

PA

Data

MemoryPAPA

miss

PTEAPTEA

miss

PTEA

hit

PA

hit

Data

PTE

L1

cache

CPU Chip

VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address

55

Page 56: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Elephant(s) in the room

• Problem 1: Translation is slow!

• Many memory accesses for each memory access

• Caches are useless!

• Problem 2: Page

table can be

gigantic!

• We need one for

each process

• All your memory

belong to us!

56

Page 57: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Speeding up Translation with a TLB

Page table entries (PTEs) are cached in L1 like any other

memory word

PTEs may be evicted by other data references

PTE hit still requires a small L1 delay

Solution: Translation Lookaside Buffer (TLB)

Small hardware cache in MMU

Maps virtual page numbers to physical page numbers

Contains complete page table entries for small number of pages

57

Page 58: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

TLB Hit

MMUCache/Memory

PA

Data

CPUVA

CPU Chip

PTE

1

2

4

5

A TLB hit eliminates a memory access

TLB

VPN 3

58

Page 59: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

TLB Miss

MMUCache/MemoryPA

Data

CPUVA

CPU Chip

PTE

1

2

5

6

TLB

VPN

4

PTEA

3

A TLB miss incurs an additional memory access (the PTE)Fortunately, TLB misses are rare. Why?

59

Page 60: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

Multi-Level Page Tables

Suppose:

4KB (212) page size, 48-bit address space, 8-byte PTE

Problem:

Would need a 512 GB page table!

248 * 2-12 * 23 = 239 bytes

Common solution:

Multi-level page tables

Example: 2-level page table

Level 1 table: each PTE points to a page table (always memory

resident)

Level 2 table: each PTE points to a page

(paged in and out like any other data)

Level 1

Table

...

Level 2

Tables

...

60

Page 61: Advanced Operating Systems (CS 202) Process (continued)heng/teaching/cs202-fall19/lec3.pdfUnix: Process User ID is inherited –children of your shell ... finish its task or continue

A Two-Level Page Table Hierarchy

Level 1

page table

...

Level 2

page tables

VP 0

...

VP 1023

VP 1024

...

VP 2047

Gap

0

PTE 0

...

PTE 1023

PTE 0

...

PTE 1023

1023 nullPTEs

PTE 1023 1023 unallocated

pages

VP 9215

Virtual

memory

(1K - 9)null PTEs

PTE 0

PTE 1

PTE 2 (null)

PTE 3 (null)

PTE 4 (null)

PTE 5 (null)

PTE 6 (null)

PTE 7 (null)

PTE 8

2K allocated VM pagesfor code and data

6K unallocated VM pages

1023 unallocated pages1 allocated VM pagefor the stack

32 bit addresses, 4KB pages, 4-byte PTEs

61