140 likes | 292 Views
제 23 강 : Mutual Exclusion in UNIX Kernel. Mutual Exclusion in UNIX Kernel. Race Condition in (1) multi-processor system. multi-processor. x++. x++. CPU #0. CPU #1. bus. Shared Memory. x. Critical Section 2 CPU’s share a variable. multi-processor. CPU #0. CPU #1. bus.
E N D
제23강 : Mutual Exclusion in UNIX Kernel Mutual Exclusion in UNIX Kernel
Race Condition in(1) multi-processor system multi-processor x++ x++ CPU #0 CPU #1 bus Shared Memory x
Critical Section 2 CPU’s share a variable multi-processor CPU #0 CPU #1 bus • Also access x • Read x into register • Operation with ALU register • Write back to storage box Shared Memory x X++ X++
Interleaved execution multi-processor X++ CPU #0 CPU #1 X++ bus Read x into register (11) Increment ALU register (12) Write back to storage box (12) Read x into register (11) Increment ALU register (12) Write back to storage box (12) Incorrect result
Mutual Exclusion multi-processor CPU #0 CPU #1 bus Read x into register (11) Operation with ALU register (12) Write back to storage box (12) Shared Memory x Read x into register (12) Operation with ALU register (13) Write back to storage box (13) Allow only one process to enter critical section at a time (exclude other process) correct result
x Race Condition in uni-processor(2) interrupt handler v.s. kernel • Data sharing betweenI.H. & kernel functions • If kernel’s critical section is interrupted? eg kernel: disk_queue++; I.H.(disk): disk_queue--; Interrupt handler kernel functions interrupt load x into register (11) -- ALU register (10) Write back to memory (10) load x into register (11) ++ ALU register (12) Write back to memory (12) return Incorrect result
x interrupt handler v.s. kerneldisable/enable interrupt • Data sharing betweenI.H. & kernel functions • Do not allow interrupt during such critical sections Interrupt handler interrupt not allowed kernel functions X load x into register (11) -- ALU register (10) Write back to memory (10) disable interrupt load x into register (11) ++ ALU register (12) Write back to memory (12) enable interrupt
Add request to the disk queue 1 Read R0, DiskQ 2 Add R0 3 Store R0, DiskQ sh (parent) Remove a request from disk queue Interrupt Handler Data sharing betweenkernel & I. H. • Disable interrupt temporarily set CPU interrupt priority level User mode Kernel mode
UNIX disk driver / *------ RK disk driver -----*/ 5386: struct devtab rktab; 5387: struct buf rrkbuf; 5389: rkstrategy(abp) 5390: struct buf *abp; 5391: { 5392: register struct buf *bp; 5393: register *qc, *ql; 5408: spl5(); 5409: if (rktab.d_actf==0) 5410: rktab.d_actf = bp; 5411: else 5412: rktab.d_actl->av_forw = bp; 5414: if (rktab.d_active==0) 5415: rkstart(); 5416: spl0(); 5417: }
x Race Condition in uni-processor(3) processes sharing kernel data • (2) • CPU • preempted sh a.out vi a.out • (3) • system • call • (1) • system • call load x into register (11) ++ ALU register (12) Write back to memory (12) kernel a.out • (4) • access • same • variable load x into register (11) ++ ALU register (12) Write back to memory (12) kernel a.out • (6) • Context • Switch • (5) • return sh a.out vi a.out
sh’s data Process 1 sh (parent) vi’s data Process 2 vi (child) No data sharing between user mode process a.out’s (eg sh v.s. vi) User mode Kernel mode
1. Read kernel X into R0 2 Add R0 3 Store R0, X Process 1 sh (parent) Also update kernel X Process 2 vi (child) sh & vi share Kernel data Cure: Do not allow CPU preemption while in kernel mode User mode Kernel mode
x Race Condition in uni-processor(3) processes sharing kernel data sh a.out vi a.out X No preemption • (1) • system • call Forbid CPU preemption load x into register (11) ++ ALU register (12) Write back to memory (12) kernel a.out load x into register (11) ++ ALU register (12) Write back to memory (12) kernel a.out Allow CPU preemption sh a.out vi a.out
mutual exclusion summary • Uni-Processor • Kernel – Interrupt handler • disable, enable interrput • spl3(), spl0(); • Processes sharing kernel data • no preemption during system call • Multiprocessor • mutual exclusion primitive needed