Recitation 8 Nov 1 Outline TA Kun Gao

  • Slides: 34
Download presentation
Recitation 8 (Nov. 1) Outline TA: Kun Gao n Lab 5 hints n Virtual

Recitation 8 (Nov. 1) Outline TA: Kun Gao n Lab 5 hints n Virtual Memory Reminder n Shell Lab: n Due THIS Thursday Modified from Minglong Shao’s Recitation, Fall 2004

Lab 5 n A tiny shell (tsh) with job control & I/O redirection n

Lab 5 n A tiny shell (tsh) with job control & I/O redirection n Key points: n Understand process tree n Executing programs n Reap all child processes n Handle SIGCHLD, SIGTSTP, SIGINT n Avoid race hazard n I/O redirection

Process tree for shell pid=10 pgid=10 Background job #1 pid=20 Forepgid=20 ground job Child

Process tree for shell pid=10 pgid=10 Background job #1 pid=20 Forepgid=20 ground job Child pid=21 pgid=20 Shell Child pid=22 pgid=20 Foreground process group 20 pid=32 pgid=32 Background process group 32 Background job #2 pid=40 pgid=40 Backgroud process group 40 Each job has a unique process group id int setpgid(pid_t pid, pid_t pgid); setpgid(0, 0);

Process tree for shell (part 2) pid=5 pgid=5 pid=10 pgid=10 als UNIX shell Foreground

Process tree for shell (part 2) pid=5 pgid=5 pid=10 pgid=10 als UNIX shell Foreground job receives SIGINT, SIGTSTP, when you type ctrl-c, ctrl-z tsh n ig s d r a w r Fo pid=20 Forepgid=20 ground job Child pid=21 pgid=20 Child pid=22 pgid=20 Foreground process group 20 Background job #1 pid=32 pgid=32 Background process group 32 Background job #2 pid=40 pgid=40 Backgroud process group 40 int kill(pid_t pid, int sig) pid > 0: send sig to specified process pid = 0: send sig to all processes in same group pid < -1: send sig to group -pid

Execute program int execve(const char *fname, char *const argv[], char *const envp[]); Examples: execve(“/bin/ls”,

Execute program int execve(const char *fname, char *const argv[], char *const envp[]); Examples: execve(“/bin/ls”, NULL); execve(“. /mytest”, argv, envp);

Reaping child process waitpid(pid_t pid, int *status, int options) pid: wait for child process

Reaping child process waitpid(pid_t pid, int *status, int options) pid: wait for child process with pid (process id or group id) -1: wait for any child process status: tell why child terminated options: WNOHANG: return immediately if no children zombied WUNTRACED: report status of terminated or stopped children In Lab 5, use waitpid(-1, &status, WNOHANG|WUNTRACED) In sigchld_handler(): while ((c_pid = waitpid(…)) > 0) { … }

Reaping child process (part 2) n int status; waitpid(pid, &status, WNOHANG|WUNTRACED) What to check

Reaping child process (part 2) n int status; waitpid(pid, &status, WNOHANG|WUNTRACED) What to check in sigchld_handler: n WIFEXITED(status): child exited normally (the child finished, and quit normally on its own) n WEXITSTATUS(status): returns code when child exits n WIFSIGNALED(status): child exited because a signal is not caught (SIGINT, or typing CTRL-C) n WTERMSIG(status): gives the terminating signal number n WIFSTOPPED(status): child is stopped by the receipt of a signal (SIGSTOP, or typing CTRL-Z) n WSTOPSIG(status): gives the stop signal number

Reaping child process (part 3) n Where to put waitpid(…) n In sigchld_handler(): for

Reaping child process (part 3) n Where to put waitpid(…) n In sigchld_handler(): for both n tsh should still wait for fg job to complete, how?

Busy wait foreground job In eval(): if(fork() != 0) { /* parent */ addjob(…);

Busy wait foreground job In eval(): if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ /* do nothing */ } }

Sleep In eval(): if(fork() != 0) { /* parent */ addjob(…); while(fg process still

Sleep In eval(): if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ sleep(1); } }

Race hazard n A data structure is shared by two pieces of code that

Race hazard n A data structure is shared by two pieces of code that can run concurrently n Different behaviors of program depending upon how the schedule interleaves the execution of code.

An example of race hazard sigchld_handler() { while ((pid = waitpid(…)) > 0){ deletejob(pid);

An example of race hazard sigchld_handler() { while ((pid = waitpid(…)) > 0){ deletejob(pid); } } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler may run BEFORE addjob()*/ addjob(…); }

An okay schedule time Shell Signal Handler Child fork() addjob() execve() exit() sigchld_handler() deletejobs()

An okay schedule time Shell Signal Handler Child fork() addjob() execve() exit() sigchld_handler() deletejobs()

A problematic schedule time Shell Signal Handler Child fork() execve() exit() sigchld_handler() deletejobs() addjob()

A problematic schedule time Shell Signal Handler Child fork() execve() exit() sigchld_handler() deletejobs() addjob() Job added to job list after the signal handler tried to delete it!

Solution: blocking signals sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { sigprocmask(SIG_BLOCK, …)

Solution: blocking signals sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { sigprocmask(SIG_BLOCK, …) pid = fork(); if(pid == 0) { /* child */ sigprocmask(SIG_UNBLOCK, …) execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); sigprocmask(SIG_UNBLOCK, …) }

Solution: blocking signals (part 2) n What should our block set be? n SIGSTP

Solution: blocking signals (part 2) n What should our block set be? n SIGSTP n SIGINT n SIGCHLD

I/O redirection n Do it before call execve() in child process eval() { pid

I/O redirection n Do it before call execve() in child process eval() { pid = fork(); if(pid == 0) { /* child */ /* Redirect I/O */ if (redirect_input) dup 2(…); /* redirect STDIN */ if (redirect_output) dup 2(…); /* redirect STDOUT */ execve(…); } addjob(…); }

dup 2(int oldfd, int newfd) n Covered in Chapter 11 oldfd: old file descriptor

dup 2(int oldfd, int newfd) n Covered in Chapter 11 oldfd: old file descriptor newfd: new file descriptor n Dup 2 makes newfd be a copy of the oldfd (i. e. operations on newfd is done on oldfd instead) Some examples: Get input from in_fd instead of standard input dup 2(in_fd, STDIN_FILENO); Print output to out_fd instead of standard output dup 2(out_fd, STDOUT_FILENO);

Reminders n Some important system calls: n fork(), execve(), waitpid(), sigprocmask(), setpgid(), kill() …

Reminders n Some important system calls: n fork(), execve(), waitpid(), sigprocmask(), setpgid(), kill() … n Check man pages for details about system calls n man 2 kill n Check return values of all system calls n STEP by STEP n Test your shell by typing commands first n Each trace worth the same (work on easy ones first!) n Start now!

Virtual memory n One of the most important concepts in CS n Why use

Virtual memory n One of the most important concepts in CS n Why use virtual memory (VM) n Use RAM as a cache for disk n Easier memory management n Access protection n Enable “partial swapping” n Share memory efficiently

Virtual memory Memory Per Process: Virtual Addresses 0: 1: Page Table 0: 1: Physical

Virtual memory Memory Per Process: Virtual Addresses 0: 1: Page Table 0: 1: Physical Addresses CPU P-1: n Page table n Page hits, Page faults n Demand Paging N-1: Disk

Conceptual address translation n Higher bits of address (page number) mapped from virtual addr.

Conceptual address translation n Higher bits of address (page number) mapped from virtual addr. to physical addr. n Lower bits (page offset) stay the same. p p– 1 n– 1 virtual page number (VPN) page offset 0 virtual address translation m– 1 p p– 1 physical page number (PPN) 0 page offset Virtual Memory: up to 2 n -1 bytes Physical Memory: up to 2 m -1 bytes physical address

Address translation through page table n Translation n Separate (set of) page table(s) per

Address translation through page table n Translation n Separate (set of) page table(s) per process n VPN forms index into page table (points to a page table entry)

Address translation with TLB n– 1 p p– 1 0 virtual page number page

Address translation with TLB n– 1 p p– 1 0 virtual page number page offset valid . virtual address tag physical page number . TLB . = TLB hit physical address tag index valid tag byte offset data Cache = cache hit data

Integrating VM and cache n Most caches are physically addressed n Why? n Perform

Integrating VM and cache n Most caches are physically addressed n Why? n Perform address translation before cache lookup, could potentially go to memory =*( n TLB keeps things manageable 1. VA CPU 4. PA MMU/ Translation 2. VPN 3. PTE TLB 5. Data Cache/Memory

Integrating VM and cache (TLB hit) n Most caches are physically addressed n Why?

Integrating VM and cache (TLB hit) n Most caches are physically addressed n Why? n Perform address translation before cache lookup, could potentially go to memory =*( n TLB keeps things manageable 1. VA CPU 4. PA MMU/ Translation 2. VPN 3. PTE TLB 5. Data Cache/Memory

Integrating VM and cache (TLB miss) n TLB miss, but page table is in

Integrating VM and cache (TLB miss) n TLB miss, but page table is in cache/memory 3. PTEA (PTBP + VPN) 1. VA CPU MMU/ Translation 2. VPN TLB 6. Data 5. PA 4. PTE Cache/Memory

What is the worst case mem read (most delay)? n TLB miss, page fault

What is the worst case mem read (most delay)? n TLB miss, page fault on page table, then page fault on memory read 3. PTEA (PTBP + VPN) 1. VA CPU MMU/ Translation 2. VPN 5. PA 4. PTE Cache/Memory TLB Page Fault/ Page Table Mem Read 6. Data DISK

Example n Understand end-to-end addr. translation n 20 -bit virtual addresses n 18 -bit

Example n Understand end-to-end addr. translation n 20 -bit virtual addresses n 18 -bit physical addresses n Page size is 1024 bytes n TLB is 2 -way associative with 16 total entries

Part 1 n A. Virtual address TLBT 19 18 17 16 TLBI 15 14

Part 1 n A. Virtual address TLBT 19 18 17 16 TLBI 15 14 13 12 11 10 9 8 7 6 VPN 5 4 3 2 1 0 VPO n B. Physical address 17 16 15 14 13 PPN 12 11 10 9 8 7 6 5 4 PPO

Example: TLB and page table

Example: TLB and page table

Part 2 Virtual address 0 x 04 AA 4 n A. 04 AA 4

Part 2 Virtual address 0 x 04 AA 4 n A. 04 AA 4 = 0000 0100 1010 0100 n B. Address translation parameter VPN TLB Index TLB Tag Value 0 x 012 0 x 02 n C. Physical address 01 1010 0010 1010 0100 Parameter TLB hit? Page fault? PPN Value Y N 0 x 68

Part 2 Virtual address 0 x 78 E 6 n A. 078 E 6

Part 2 Virtual address 0 x 78 E 6 n A. 078 E 6 = 0000 0111 1000 1110 0110 n B. Address translation parameter VPN TLB Index TLB Tag Value 0 x 01 E 0 x 6 0 x 03 n C. Physical address 01 0101 1100 1110 0110 Parameter TLB hit? Page fault? PPN Value N N 0 x 57

End-to-end address translation n Section 10. 6. 4: a concrete example n Read carefully

End-to-end address translation n Section 10. 6. 4: a concrete example n Read carefully and solve practice problem 10. 4