Processes, signals and shells Virtual memory - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

Processes, signals and shells Virtual memory

Description:

... are passed through the argv[] array: int main (int argc, char * argv ... including the name of the program or command being executed ( passed as argv[0] ... – PowerPoint PPT presentation

Number of Views:173
Avg rating:3.0/5.0
Slides: 80
Provided by: randa84
Category:

less

Transcript and Presenter's Notes

Title: Processes, signals and shells Virtual memory


1
Processes, signals and shellsVirtual memory
  • Processes and signals
  • Creating and destroying processes
  • Process Hierarchy
  • Shells
  • Signals.
  • Virtual memory
  • Motivation for VM
  • Address translation
  • Accelerating translation with TLBs
  • All source code is posted at http//reed.cs.depaul
    .edu/lperkovic/csc374/lectures/lecture4/

2
System calls
  • Unix systems provide many different types of
    systems calls to be used by application programs
    when they need a service from the kernel
  • Reading a file
  • Creating a new process
  • To get the complete list, type
  • man syscalls

3
fork Creating new processes
  • int fork(void)
  • creates a new process (child process) that is
    identical to the calling process (parent process)
  • returns 0 to the child process
  • returns childs pid to the parent process

if (fork() 0) printf("hello from
child\n") else printf("hello from
parent\n")
Fork is interesting (and often confusing) because
it is called once but returns twice
4
Fork Example 1
  • Key Points
  • Parent and child both run same code
  • Distinguish parent from child by return value
    from fork
  • Start with same state, but each has private copy
  • Including shared output file descriptor
  • Relative ordering of their print statements
    undefined

void fork1() int x 1 pid_t pid
fork() if (pid 0) printf("Child has x
d\n", x) else printf("Parent has x
d\n", --x) printf("Bye from process
d with x d\n", getpid(), x)
5
Fork Example 2
  • Key Points
  • Both parent and child can continue forking

void fork2() printf("L0\n") fork()
printf("L1\n") fork()
printf("Bye\n")
6
Fork Example 3
  • Key Points
  • Both parent and child can continue forking

void fork3() printf("L0\n") fork()
printf("L1\n") fork()
printf("L2\n") fork()
printf("Bye\n")
7
Fork Example 4
  • Key Points
  • Both parent and child can continue forking

void fork4() printf("L0\n") if (fork()
! 0) printf("L1\n") if (fork() ! 0)
printf("L2\n") fork()
printf("Bye\n")
8
Fork Example 5
  • Key Points
  • Both parent and child can continue forking

void fork5() printf("L0\n") if (fork()
0) printf("L1\n") if (fork() 0)
printf("L2\n") fork()
printf("Bye\n")
9
exit Destroying Process
  • void exit(int status)
  • exits a process
  • Normally return with status 0
  • atexit() registers functions to be executed upon
    exit

void cleanup(void) printf("cleaning
up\n") void fork6() atexit(cleanup)
fork() exit(0)
10
Zombies
  • Idea
  • When process terminates, it still consumes system
    resources
  • Various tables maintained by OS
  • Called a zombie
  • 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 Doesnt Reap?
  • If any parent terminates without reaping a child,
    then child will be reaped by init process
  • Only need explicit reaping for long-running
    processes
  • E.g., shells and servers

11
ZombieExample
void fork7() if (fork() 0) / Child
/ printf("Terminating Child, PID d\n",
getpid()) exit(0) else
printf("Running Parent, PID d\n",
getpid()) while (1) / Infinite loop /

linuxgt ./forks 7 1 6639 Running Parent, PID
6639 Terminating Child, PID 6640 linuxgt ps
PID TTY TIME CMD 6585 ttyp9 000000
tcsh 6639 ttyp9 000003 forks 6640 ttyp9
000000 forks ltdefunctgt 6641 ttyp9 000000
ps linuxgt kill 6639 1 Terminated linuxgt ps
PID TTY TIME CMD 6585 ttyp9 000000
tcsh 6642 ttyp9 000000 ps
  • ps shows child process as defunct
  • Killing parent allows child to be reaped

12
NonterminatingChildExample
void fork8() if (fork() 0) / Child
/ printf("Running Child, PID d\n",
getpid()) while (1) / Infinite loop /
else printf("Terminating Parent, PID
d\n", getpid()) exit(0)
linuxgt ./forks 8 Terminating Parent, PID
6675 Running Child, PID 6676 linuxgt ps PID
TTY TIME CMD 6585 ttyp9 000000
tcsh 6676 ttyp9 000006 forks 6677 ttyp9
000000 ps linuxgt kill 6676 linuxgt ps PID TTY
TIME CMD 6585 ttyp9 000000 tcsh
6678 ttyp9 000000 ps
  • Child process still active even though parent has
    terminated
  • Must kill explicitly, or else will keep running
    indefinitely

13
wait Synchronizing with children
  • int wait(int child_status)
  • suspends current process until one of its
    children terminates
  • return value is the pid of the child process that
    terminated
  • if child_status ! NULL, then it points to a
    status indicating why the child process terminated

14
wait Synchronizing with children
void fork9() int child_status if
(fork() 0) printf("HC hello from
child\n") else printf("HP hello
from parent\n") wait(child_status)
printf("CT child has terminated\n")
printf("Bye\n") exit()
15
Wait Example
  • If multiple children completed, will take in
    arbitrary order
  • Can use macros WIFEXITED and WEXITSTATUS to get
    information about exit status

void fork10() pid_t pidN int i
int child_status for (i 0 i lt N i) if
((pidi fork()) 0) exit(100i) /
Child / for (i 0 i lt N i) pid_t
wpid wait(child_status) if
(WIFEXITED(child_status)) printf("Child d
terminated with exit status d\n", wpid,
WEXITSTATUS(child_status)) else
printf("Child d terminate abnormally\n", wpid)

16
Waitpid
  • waitpid(pid, status, options)
  • Can wait for specific process
  • Various options

void fork11() pid_t pidN int i
int child_status for (i 0 i lt N i) if
((pidi fork()) 0) exit(100i) /
Child / for (i 0 i lt N i) pid_t
wpid waitpid(pidi, child_status, 0) if
(WIFEXITED(child_status)) printf("Child d
terminated with exit status d\n", wpid,
WEXITSTATUS(child_status)) else
printf("Child d terminated abnormally\n",
wpid)
17
Wait/Waitpid Example Outputs
Using wait (fork10)
Child 3565 terminated with exit status 103 Child
3564 terminated with exit status 102 Child 3563
terminated with exit status 101 Child 3562
terminated with exit status 100 Child 3566
terminated with exit status 104
Using waitpid (fork11)
Child 3568 terminated with exit status 100 Child
3569 terminated with exit status 101 Child 3570
terminated with exit status 102 Child 3571
terminated with exit status 103 Child 3572
terminated with exit status 104
18
Command line arguments in C/C
  • Command line arguments to C/C programs are
    passed through the argv arrayint main (int
    argc, char argv)argc is the number of
    command line arguments, including the name of the
    program or command being executed ( passed as
    argv0)Example 1 printArgs.cExample 2
    printN.c

19
exec Running new programs
  • int execl(char path, char arg0, char arg1, ,
    0)
  • loads and runs executable at path with args arg0,
    arg1,
  • path is the complete path of an executable
  • arg0 becomes the name of the process
  • typically arg0 is either identical to path, or
    else it contains only the executable filename
    from path
  • real arguments to the executable start with
    arg1, etc.
  • list of args is terminated by a (char )0
    argument
  • returns -1 if error, otherwise doesnt return!

main() if (fork() 0)
execl("/usr/bin/cp", "cp", "foo", "bar", 0)
wait(NULL) printf("copy completed\n")
exit()
20
Running printArgs from a program
  • Instead of running the printArgs program from
    the Unix shell, we can run it from a program,
    using execlp.
  • Example 1 runls.c
  • Example 2 prog.c
  • Example 3 prog2.c

21
Creating new processes in UNIX
  • A process creates a new process that executes a
    given program or command as followsCall fork(
    ) to create a new processCall exec( ) within
    the new process to execute the program or command
  • Example progExec.c

22
Shell Programs
  • A shell is an application program that runs
    programs on behalf of the user.
  • sh Original Unix Bourne Shell
  • csh BSD Unix C Shell, tcsh Enhanced C Shell
  • bash Bourne-Again Shell

23
Writing a Unix Shell
  • Pseudo Code for a shell
  • print a prompt.
  • while( EOF is not signaled and an input line is
    read )
  • create a child process (using fork)
  • have the child process replace its program (this
    shell program) with the program specified in the
    input line. (using execlp)
  • wait for the child to finish executing its
    program. (using wait)
  • print a prompt

24
Shell Programs
  • A shell is an application program that runs
    programs on behalf of the user.
  • sh Original Unix Bourne Shell
  • csh BSD Unix C Shell, tcsh Enhanced C Shell
  • bash Bourne-Again Shell

int main() char cmdlineMAXLINE
while (1) / read / printf("gt ")
Fgets(cmdline, MAXLINE, stdin) if
(feof(stdin)) exit(0) / evaluate
/ eval(cmdline)
Execution is a sequence of read/evaluate
steps See shellex.c
25
Simple Shell eval Function
void eval(char cmdline) char
argvMAXARGS / argv for execve() / int
bg / should the job run in bg or
fg? / pid_t pid / process id
/ bg parseline(cmdline, argv) if
(!builtin_command(argv)) if ((pid Fork())
0) / child runs user job / if
(execve(argv0, argv, environ) lt 0)
printf("s Command not found.\n",
argv0) exit(0) if (!bg) /
parent waits for fg job to terminate /
int status if (waitpid(pid, status, 0) lt
0) unix_error("waitfg waitpid
error") else / otherwise, dont
wait for bg job / printf("d s", pid,
cmdline)
26
Problem with Simple Shell Example
  • Shell correctly waits for and reaps foreground
    jobs.
  • But what about background jobs?
  • Will become zombies when they terminate.
  • Will never be reaped because shell (typically)
    will not terminate.
  • Creates a memory leak that will eventually crash
    the kernel when it runs out of memory.
  • Solution Reaping background jobs requires a
    mechanism called a signal.
  • Another problem Ctrl-C kills the shell, not the
    foreground job
  • Solution Ctrl-C requests the kernel to send a
    signal to the shell, which must redirect it to
    the foreground processes

27
Signals
  • A signal is a small message that notifies a
    process that an event of some type has occurred
    in the system.
  • Kernel abstraction for exceptions and interrupts.
  • Sent from the kernel (sometimes at the request of
    another process) to a process.
  • Different signals are identified by small integer
    IDs
  • The only information in a signal is its ID and
    the fact that it arrived.

28
Signal Concepts
  • Sending a signal
  • Kernel sends (delivers) a signal to a destination
    process by updating some state in the context of
    the destination process.
  • Kernel sends a signal for one of the following
    reasons
  • Kernel has detected a system event such as
    divide-by-zero (SIGFPE) or the termination of a
    child process (SIGCHLD)
  • Another process has invoked the kill system call
    to explicitly request the kernel to send a signal
    to the destination process.
  • A user can also do this using the kill program

29
Signal Concepts (cont)
  • Receiving a signal
  • A destination process receives a signal when it
    is forced by the kernel to react in some way to
    the delivery of the signal.
  • Three possible ways to react
  • Ignore the signal (do nothing)
  • Terminate the process.
  • Catch the signal by executing a user-level
    function called a signal handler.
  • Akin to a hardware exception handler being called
    in response to an asynchronous interrupt.

30
Signal Concepts (cont)
  • A signal is pending if it has been sent but not
    yet received.
  • There can be at most one pending signal of any
    particular type.
  • Important Signals are not queued
  • If a process has a pending signal of type k, then
    subsequent signals of type k that are sent to
    that process are discarded.
  • A process can block the receipt of certain
    signals.
  • Blocked signals can be delivered, but will not be
    received until the signal is unblocked.
  • A pending signal is received at most once.

31
Signal Concepts
  • Kernel maintains pending and blocked bit vectors
    in the context of each process.
  • pending represents the set of pending signals
  • Kernel sets bit k in pending whenever a signal of
    type k is delivered.
  • Kernel clears bit k in pending whenever a signal
    of type k is received
  • blocked represents the set of blocked signals
  • Can be set and cleared by the application using
    the sigprocmask function.

32
Process Groups
  • Every process belongs to exactly one process group

Shell
pid10 pgid10
Fore- ground job
Back- ground job 1
Back- ground job 2
pid20 pgid20
pid32 pgid32
pid40 pgid40
Background process group 32
Background process group 40
Child
Child
getpgrp() Return process group of current
process setpgid() Change process group of a
process
pid21 pgid20
pid22 pgid20
Foreground process group 20
33
Sending Signals with kill Program
  • kill program sends arbitrary signal to a process
    or process group
  • Examples
  • kill 9 24818
  • Send SIGKILL to process 24818
  • kill 9 24817
  • Send SIGKILL to every process in process group
    24817.

linuxgt ./forks 16 linuxgt Child1 pid24818
pgrp24817 Child2 pid24819 pgrp24817
linuxgt ps PID TTY TIME CMD 24788
pts/2 000000 tcsh 24818 pts/2 000002
forks 24819 pts/2 000002 forks 24820 pts/2
000000 ps linuxgt kill -9 -24817 linuxgt ps
PID TTY TIME CMD 24788 pts/2
000000 tcsh 24823 pts/2 000000 ps linuxgt
34
Sending Signals from the Keyboard
  • Typing ctrl-c (ctrl-z) sends a SIGTERM (SIGTSTP)
    to every job in the foreground process group.
  • SIGTERM default action is to terminate each
    process
  • SIGTSTP default action is to stop (suspend)
    each process

Shell
pid10 pgid10
Fore- ground job
Back- ground job 1
Back- ground job 2
pid20 pgid20
pid32 pgid32
pid40 pgid40
Background process group 32
Background process group 40
Child
Child
pid21 pgid20
pid22 pgid20
Foreground process group 20
35
Example of ctrl-c and ctrl-z
linuxgt ./forks 17 Child pid24868 pgrp24867
Parent pid24867 pgrp24867 lttyped
ctrl-zgt Suspended linuxgt ps a PID TTY
STAT TIME COMMAND 24788 pts/2 S 000
-usr/local/bin/tcsh -i 24867 pts/2 T
001 ./forks 17 24868 pts/2 T 001
./forks 17 24869 pts/2 R 000 ps a
bassgt fg ./forks 17 lttyped ctrl-cgt linuxgt ps
a PID TTY STAT TIME COMMAND 24788
pts/2 S 000 -usr/local/bin/tcsh -i
24870 pts/2 R 000 ps a
36
Sending Signals with kill Function
void fork12() pid_t pidN int i,
child_status for (i 0 i lt N i) if
((pidi fork()) 0) while(1) / Child
infinite loop / / Parent terminates the
child processes / for (i 0 i lt N i)
printf("Killing process d\n",
pidi) kill(pidi, SIGINT) /
Parent reaps terminated children / for (i
0 i lt N i) pid_t wpid wait(child_status)
if (WIFEXITED(child_status))
printf("Child d terminated with exit status
d\n", wpid, WEXITSTATUS(child_status)) els
e printf("Child d terminated abnormally\n",
wpid)
37
Receiving Signals
  • Suppose kernel is returning from exception
    handler and is ready to pass control to process
    p.
  • Kernel computes pnb pending blocked
  • The set of pending nonblocked signals for process
    p
  • If (pnb 0)
  • Pass control to next instruction in the logical
    flow for p.
  • Else
  • Choose least nonzero bit k in pnb and force
    process p to receive signal k.
  • The receipt of the signal triggers some action by
    p
  • Repeat for all nonzero k in pnb.
  • Pass control to next instruction in logical flow
    for p.

38
Default Actions
  • Each signal type has a predefined default action,
    which is one of
  • The process terminates
  • The process terminates and dumps core.
  • The process stops until restarted by a SIGCONT
    signal.
  • The process ignores the signal.

39
Installing Signal Handlers
  • The signal function modifies the default action
    associated with the receipt of signal signum
  • handler_t signal(int signum, handler_t handler)
  • Different values for handler
  • SIG_IGN ignore signals of type signum
  • SIG_DFL revert to the default action on receipt
    of signals of type signum.
  • Otherwise, handler is the address of a signal
    handler
  • Called when process receives signal of type
    signum
  • Referred to as installing the handler.
  • Executing handler is called catching or
    handling the signal.
  • When the handler executes its return statement,
    control passes back to instruction in the control
    flow of the process that was interrupted by
    receipt of the signal.

40
Signal Handling Example
void int_handler(int sig) printf("Process
d received signal d\n", getpid(),
sig) exit(0) void fork13() pid_t
pidN int i, child_status
signal(SIGINT, int_handler) . . .
linuxgt ./forks 13 Killing process 24973 Killing
process 24974 Killing process 24975 Killing
process 24976 Killing process 24977 Process
24977 received signal 2 Child 24977 terminated
with exit status 0 Process 24976 received signal
2 Child 24976 terminated with exit status 0
Process 24975 received signal 2 Child 24975
terminated with exit status 0 Process 24974
received signal 2 Child 24974 terminated with
exit status 0 Process 24973 received signal 2
Child 24973 terminated with exit status 0
linuxgt
41
Signal Handler Funkiness
  • Pending signals are not queued
  • For each signal type, just have single bit
    indicating whether or not signal is pending
  • Even if multiple processes have sent this signal
  • See signal1.c

void handler1(int sig) pid_t pid if ((pid
waitpid(-1, NULL, 0)) lt 0)
unix_error("waitpid error") printf("Handler
reaped child d\n", (int)pid)
Sleep(2) return int main() int i, n
char bufMAXBUF if (signal(SIGCHLD, handler1)
SIG_ERR) unix_error("signal error")
for (i 0 i lt 3 i) if (Fork() 0)
printf("Hello from child d\n",
(int)getpid()) Sleep(1) exit(0)
if ((n read(STDIN_FILENO, buf,
sizeof(buf))) lt 0) unix_error("read")
printf("Parent processing input\n") while (1)
exit(0)
42
Living With Nonqueuing Signals
  • Must check for all terminated jobs
  • Typically loop with wait
  • See signal2.c

void handler2(int sig) pid_t pid while
((pid waitpid(-1, NULL, 0)) gt 0)
printf("Handler reaped child d\n", (int)pid)
if (errno ! ECHILD) unix_error(waitpid
error) Sleep(2) return int main()
. . if (signal(SIGCHLD, handler2) SIG_ERR)
. .
43
A Program That Reacts toExternally Generated
Events (ctrl-c)
include ltstdlib.hgt include ltstdio.hgt include
ltsignal.hgt void handler(int sig)
printf("You think hitting ctrl-c will stop the
bomb?\n") sleep(2) printf("Well...")
fflush(stdout) sleep(1) printf("OK\n")
exit(0) main() signal(SIGINT,
handler) / installs ctl-c handler / while(1)

44
Virtual Memory
  • Virtual memory
  • Motivation for VM
  • Address translation
  • Accelerating translation with TLBs

45
Motivations for Virtual Memory
  • Use Physical DRAM as a Cache for the Disk
  • Address space of a process can exceed physical
    memory size
  • Sum of address spaces of multiple processes can
    exceed physical memory
  • Simplify Memory Management
  • Multiple processes resident in main memory.
  • Each process with its own address space
  • Only active code and data is actually in memory
  • Allocate more memory to process as needed.
  • Provide Protection
  • One process cant interfere with another.
  • because they operate in different address spaces.
  • User process cannot access privileged information
  • different sections of address spaces have
    different permissions.

46
Motivation 1 DRAM a Cache for Disk
  • Full address space is quite large
  • 32-bit addresses
    4,000,000,000 (4 billion) bytes
  • 64-bit addresses 16,000,000,000,000,000,000 (16
    quintillion) bytes
  • Disk storage is 400X cheaper than DRAM storage
  • 200 GB of DRAM 40,000
  • 200 GB of disk 110
  • To access large amounts of data in a
    cost-effective manner, the bulk of the data must
    be stored on disk

47
Levels in Memory Hierarchy
cache
virtual memory
Memory
disk
8 B
32 B
4 KB
Register
Cache
Memory
Disk Memory
size speed /Mbyte line size
32 B 1 ns 8 B
32 KB-4MB 2 ns 125/MB 32 B
1024 MB 30 ns 0.20/MB 4 KB
100 GB 8 ms 0.001/MB
larger, slower, cheaper
48
DRAM vs. SRAM as a Cache
  • DRAM vs. disk is more extreme than SRAM vs. DRAM
  • Access latencies
  • DRAM 10X slower than SRAM
  • Disk 100,000X slower than DRAM
  • Importance of exploiting spatial locality
  • First byte is 100,000X slower than successive
    bytes on disk
  • Bottom line
  • Design decisions made for DRAM caches driven by
    enormous cost of misses

DRAM
Disk
SRAM
49
Impact of Properties on Design
  • If DRAM was to be organized similar to an SRAM
    cache, how would we set the following design
    parameters?
  • Line size?
  • Large, since disk better at transferring large
    blocks
  • Associativity?
  • High, to mimimize miss rate
  • Write through or write back?
  • Write back, since cant afford to perform small
    writes to disk
  • What would the impact of these choices be on
  • miss rate
  • Extremely low. ltlt 1
  • hit time
  • Must match cache/DRAM performance
  • miss latency
  • Very high. 20ms
  • tag storage overhead
  • Low, relative to block size

50
Locating an Object in a Cache
  • SRAM Cache
  • Tag stored with cache line
  • No tag for block not in cache
  • Hardware retrieves information
  • can quickly match against multiple tags

51
Locating an Object in Cache (cont.)
  • DRAM Cache
  • Each allocated page of virtual memory has entry
    in page table
  • Mapping from virtual pages to physical pages
  • Page table entry even if page not in memory
  • Specifies disk address
  • Only way to indicate where to find page
  • OS retrieves information

Cache
Page Table
Location
0
On Disk

1
52
A System with Physical Memory Only
  • Examples
  • most Cray machines, early PCs, nearly all
    embedded systems, etc.
  • Addresses generated by the CPU correspond
    directly to bytes in physical memory

53
A System with Virtual Memory
  • Examples
  • workstations, servers, modern PCs, etc.

Memory
Page Table
Virtual Addresses
Physical Addresses
0
1
P-1
Disk
  • Address Translation Hardware converts virtual
    addresses to physical addresses via OS-managed
    lookup table (page table)

54
Page Faults (like Cache Misses)
  • What if an object is on disk rather than in
    memory?
  • Page table entry indicates virtual address not in
    memory
  • OS exception handler invoked to move data from
    disk into memory
  • current process suspends, others can resume
  • OS has full control over placement, etc.

Before fault
After fault
Memory
Memory
Page Table
Page Table
Virtual Addresses
Physical Addresses
Virtual Addresses
Physical Addresses
CPU
CPU
Disk
Disk
55
Servicing a Page Fault
(1) Initiate Block Read
  • Processor Signals Controller
  • Read block of length P starting at disk address X
    and store starting at memory address Y
  • Read Occurs
  • Direct Memory Access (DMA)
  • Under control of I/O controller
  • I / O Controller Signals Completion
  • Interrupt processor
  • OS resumes suspended process

Processor
Reg
(3) Read Done
Cache
Memory-I/O bus
(2) DMA Transfer
I/O controller
Memory
disk
Disk
56
Motivation 2 Memory Management
  • Multiple processes can reside in physical memory.
  • How do we resolve address conflicts?
  • what if two processes access something at the
    same address?

memory invisible to user code
kernel virtual memory
stack
esp
Memory mapped region For shared libraries
Linux/x86 process memory image
the brk ptr
runtime heap (via malloc)
uninitialized data (.bss)
initialized data (.data)
program text (.text)
forbidden
0
57
Solution Separate Virt. Addr. Spaces
  • Virtual and physical address spaces divided into
    equal-sized blocks
  • blocks are called pages (both virtual and
    physical)
  • Each process has its own virtual address space
  • operating system controls how virtual pages as
    assigned to physical memory

0
Physical Address Space (DRAM)
Address Translation
Virtual Address Space for Process 1
0
VP 1
PP 2
VP 2
...
N-1
(e.g., read/only library code)
PP 7
Virtual Address Space for Process 2
0
VP 1
PP 10
VP 2
...
M-1
N-1
58
Motivation 3 Protection
  • Page table entry contains access rights
    information
  • hardware enforces this protection (trap into OS
    if violation occurs)

Page Tables
Memory
Process i
Process j
59
VM Address Translation
  • Virtual Address Space
  • V 0, 1, , N1
  • Physical Address Space
  • P 0, 1, , M1
  • M lt N
  • Address Translation
  • MAP V ? P U ?
  • For virtual address a
  • MAP(a) a if data at virtual address a at
    physical address a in P
  • MAP(a) ? if data at virtual address a not in
    physical memory
  • Either invalid or stored on disk

60
VM Address Translation Hit
Processor
Hardware Addr Trans Mechanism
Main Memory
a
a'
physical address
virtual address
part of the on-chip memory mgmt unit (MMU)
61
VM Address Translation Miss
page fault
fault handler
Processor
?
Hardware Addr Trans Mechanism
Main Memory
Secondary memory
a
a'
OS performs this transfer (only if miss)
physical address
virtual address
part of the on-chip memory mgmt unit (MMU)
62
VM Address Translation
  • Parameters
  • P 2p page size (bytes).
  • N 2n Virtual address limit
  • M 2m Physical address limit

n1
0
p1
p
virtual address
virtual page number
page offset
address translation
0
p1
p
m1
physical address
physical page number
page offset
Page offset bits dont change as a result of
translation
63
Page Tables
Memory resident page table (physical page or
disk address)
Virtual Page Number
Physical Memory
Valid
1
1
0
1
1
1
0
1
Disk Storage (swap file or regular file system
file)
0
1
64
Address Translation via Page Table
65
Page Table Operation
  • Translation
  • Separate (set of) page table(s) per process
  • VPN forms index into page table (points to a page
    table entry)

66
Page Table Operation
  • Computing Physical Address
  • Page Table Entry (PTE) provides information about
    page
  • if (valid bit 1) then the page is in memory.
  • Use physical page number (PPN) to construct
    address
  • if (valid bit 0) then the page is on disk
  • Page fault

67
Page Table Operation
  • Checking Protection
  • Access rights field indicate allowable access
  • e.g., read-only, read-write, execute-only
  • typically support multiple protection modes
    (e.g., kernel vs. user)
  • Protection violation fault if user doesnt have
    necessary permission

68
Integrating VM and Cache
  • Most Caches Physically Addressed
  • Accessed by physical addresses
  • Allows multiple processes to have blocks in cache
    at same time
  • Allows multiple processes to share pages
  • Cache doesnt need to be concerned with
    protection issues
  • Access rights checked as part of address
    translation
  • Perform Address Translation Before Cache Lookup
  • But this could involve a memory access itself (of
    the PTE)
  • Of course, page table entries can also become
    cached

69
Speeding up Translation with a TLB
  • 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

70
Address Translation with a TLB
n1
0
p1
p
virtual address
virtual page number
page offset
valid
physical page number
tag
TLB
.
.
.

TLB hit
physical address
tag
byte offset
index
valid
tag
data
Cache

data
cache hit
71
Simple Memory System Example
  • Addressing
  • 14-bit virtual addresses
  • 12-bit physical address
  • Page size 64 bytes

(Virtual Page Offset)
(Virtual Page Number)
(Physical Page Number)
(Physical Page Offset)
72
Simple Memory System Page Table
  • Only show first 16 entries

73
Simple Memory System TLB
  • TLB
  • 16 entries
  • 4-way associative

74
Simple Memory System Cache
  • Cache
  • 16 lines
  • 4-byte line size
  • Direct mapped

75
Address Translation Example 1
  • Virtual Address 0x03D4
  • VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page
    Fault? __ PPN ____
  • Physical Address
  • Offset ___ CI___ CT ____ Hit? __ Byte ____

76
Address Translation Example 2
  • Virtual Address 0x0B8F
  • VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page
    Fault? __ PPN ____
  • Physical Address
  • Offset ___ CI___ CT ____ Hit? __ Byte ____

77
Address Translation Example 3
  • Virtual Address 0x0040
  • VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page
    Fault? __ PPN ____
  • Physical Address
  • Offset ___ CI___ CT ____ Hit? __ Byte ____

78
Multi-Level Page Tables
Level 2 Tables
  • Given
  • 4KB (212) page size
  • 32-bit address space
  • 4-byte Page table entry
  • Problem
  • Would need a 4 MB page table!
  • 220 4 bytes
  • Common solution
  • multi-level page tables
  • e.g., 2-level table (P6)
  • Level 1 table 1024 entries, each of which points
    to a Level 2 page table.
  • Level 2 table 1024 entries, each of which
    points to a page

Level 1 Table
...
79
Main Themes
  • Programmers View
  • Large flat address space
  • Can allocate large blocks of contiguous addresses
  • Processor owns machine
  • Has private address space
  • Unaffected by behavior of other processes
  • System View
  • User virtual address space created by mapping to
    set of pages
  • Need not be contiguous
  • Allocated dynamically
  • Enforce protection during address translation
  • OS manages many processes simultaneously
  • Continually switching among processes
  • Especially when one must wait for resource
  • E.g., disk I/O to handle page fault
Write a Comment
User Comments (0)
About PowerShow.com