Download presentation
Presentation is loading. Please wait.
Published byBethany Lyons Modified over 9 years ago
1
More Discussions on hw5 Timer interrupts –called ticks. ISR called tick handler Kernel uses ticks for: –time keeping, incrementing the global system time variable –counting down the quantum and causing preemption when it reaches zero –handling periodic housekeeping actions –other time-delayed actions such as network timeouts
2
How to Handle the Quantum Add a new member p_quantum to PEntry data structure To set a quantum of QUANTUM ms, initialize p_quantum =QUANTUM Example code in timer ISR: if(--curproc->p_quantum = = 0) { /* preempting, start over with full quantum */ curproc->p_quantum = QUANTUM; schedule();/* find another process to run */ } Code runs with IF=0, so do not use sti Important to realize that the tick handler is counting down the current process curproc
3
Preemption in Kernel Code Don’t know a tick happened in user or kernel code. Preemption happens in both. Both Solaris and Win2K allows kernel preemption. Linux v2.6 allows unlimited preemption Preemption occurs in kernel code where IF=1 Race conditions can occur with kernel global variables (e.g. in hw3 : number_of_zombie++;) Lost Update Problem: –proc 1 exec the first instruction and then get preempted. Proc 2 exec both instructions. Proc 1 resumes and updates the old value Assembled into: 1. Read value 2. Increment and store
4
More on Lost Update Problem Solution: Use kernel mutex For hw5, make IF = 0 for this code This only happens to global variables For local variables, this is not an issue: –Updates occur only on the process’s private stack –Updates on one process do not affect another
5
Preemptive Scheduler in CPU-bound tasks For SAPC, use systems 1-10 for faster output With hw3’s non-preemptive scheduler, output stops during the running of the CPU-bound tasks,. Example in main1 of uprog1.c: Preemptive scheduler in hw5 will change this …a a a z z z-------------------AAA... I/O bound CPU bound No output
6
Process Switching In hw3’s scheduler, the decision for running the next process is round robin –is process 1 runnable? Run it if it is. –Is process 2 runnable? Run it if it is. –… In hw5’s scheduler, we want the decision to be: –if process 1 is running now, run process 2 if it is runnable –if process 2 is running now, run process 3 if it is runnable –...
7
Process Blocking with Semaphores Both hw 5 projects are independent of each other Semaphores can be implemented without preemption Operations on semaphores: –Down: check value > 0; if yes, decrement value,continue if no, block by putting process to sleep –Up: increment the value and wait up a waiter
8
Semaphore Implementations Syscall Services semid= sem_create(init_count); down(semid); up(semid); sem_delete(semid); Semaphore IDs mutex : control mutual exclusion up: control consumer down: control producer
9
Semaphore Program Flow Start from Tutor Init memory Init kernel Start-up User program lib calls to do write, exit Scheduler User program Start-up startup0.s: Same as $pclibsrc/startup0.s - init stack startup1.c: init kernel sched.c: modified from hw3 -schedule() ; sleep();wakeup() -call asmswtch which calls _ustart1, _ustart2, or _ustart3 crt01.s, crt02.s,crt03.s: same as hw3 -entry points:_ustart1, _ustart2, _ustart3 -call _main1, _main2, or _main3 -call _exit prodcons.c or testmutex.c : provided in hw5; uses functions in intqueue.c to queue integer items -entry points: main1, main2, main3 -call write, sem_create, down, _up, sem_delete ulib.s: modified from hw3; -add _sem_create, _down, _up, _sem_delete Part of tunix.c: modified from hw3 -call ioinit, set_trap_gate(0x80, &syscall): -init PEntry[] for Proc 1,2,3 -call init_semaphores -call schedule()
10
Kernel Program Flows-syscall Trap handler wrapper System call dispatcher System call trap handler routine sysentry.s: same as hw3 -push eax,ebx,ecx,edx on stack -call system call dispatcher: _syscallc -pop stack and iret Part of tunix.c: same as hw3 -entry point _syscallc -depend on syscall #, call the handler routine tty.c: modified from hw3 -call wakeup at irqinthandc for tx interrupt -call sleep at ttywrite when queue is full semaphore.c: new file -add syssem_create,sysdown, sysup, syssem_delete
11
Wakeup Code In hw3, wakeup() wakes up all processes waiting on a waitcode In hw5, wakeup() called by up() should only wake up 1 process
12
Pseudocode for up Use the semaphore id as the event number If the waiter count is greater than 0, locate the process waited on by this semaphore and mark it “Appointed” using the sem-status member of PEntry. Make sure the other waiters on this semaphore are not so marked call wakeup(event) If the waiter count is equal to 0, increment sem count
13
Pseudocode for down Use semaphore id as the event number If sem count > 0, decrement sem count If sem count =0, store process id in the waiter queue. If the process is not appointed, call sleep().
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.