Topic: Schedule Generalconcept ---------------- Short-term: used in kernel, to schedule the ready processes Long-term : used to schedule program and input. Preemptive Scheduling: Meaning: Kernel takes CPU away from the running thread Note: it need machine support timer interrupt. Not all the machines support it. Cooperate Scheduling Q: why OS is different between those two models? A: System is busy with an activity which involves changing kernel data. Chaos will happen if the running process is preempted. Q: When should there be a schedule decision? For preemptive Time interrupt Thread I/O Thread death Sleep()/yield() Thread creation Figure5.2/5.1 in the textbook: Process life circle Based on the time differences: Different thread type: CPU-burst/IO-burst Q: How long should the timer Quantum to be set for preemptive scheduling A: scover the most cpu burst There are static scheduler which uses fixed timer and some fancy timer which the timer is changing. Q: How to evaluate different scheduler? A: Textbook 5.2 Scheduling criteria Scheduling Algorithms (Textbook 5.3, examples used in class are the same as in the text book) ------------------------------------------------------------------- 5.3.1 First-come, first-served scheduling Example is on page 188/189. Turnaround time: 24+27+30/3 =27 Question: what if do in reverse order? 5.3.2 Shortest-Job-Scheduling Example is in page 160. Cons: how do we know the time? Ask the user. User can cheat. Problem: Care about the running time of the next cpu burst time. So use the previous one to predicate the next time. Exponentied averaging : predicate the length of the next cpu burst. The equations are on page 191 Tn+1 = ¦Átn +(1-¦Á)Tn ¦Á = 1: just use last time ¦Á = 0: history has no effect 5.3.3 priority scheduling Note: Multiple threads with same priority can use either algorithm to schedule. Q: how to solve starvation? A: Slowly increase the priority 5.3.5 multilevel scheduling Topic Synchronization: Reason for synchronization: cooperating processes share data Example: Bank Account problem Int deposit(int amount){ Balance = amout; } Different thread cause problems Assembly language: Register 1 = amount load Register 2 = balance load Register 2 += register 1 add Balance = register 2 store Example: Balance = 50, T1: deposit (10), T2: deposit (5) Each thread has a different copy of register r1-1 r1-2 r2-1 r2-2 balance T1: register1 = amount 10 50 T1: register2 = balance 10 50 50 T2: register1 = amount 5 50 T2: register2 = balance 5 50 50 T1: register2+=register1 10 60 50 T1: balance = register2 10 60 60 Then trouble comes: T2: register2+=register1 5 55 T2: balance = register2 5 55 55 Race Condition Q: how to guard against race condition? A: 1.interrrupt disable 2.other synchronization method Critical section problem(textbook page 227) == > What is critical section? == > How to solve this problem? A:Main idea: Entry section & exit section Entry section: Check if it is able to enter the critical section --------------------------------------------------------- | Remainder section | | Entry section -> | | Critical section | | Exit section -> | | Remainder section | --------------------------------------------------------- How to measure the different solution to critical section problem? ? Mutual exclusion ? Progress insurance( At least on is able to run) ? Bounded waiting time(no starvation) Q: Can it be done in software? Does it need hardware support? A: sort of. Simple machine can do it only with software. But the computer using today can¡¯t. 6.3 Peterson solution Two share variable: - Boolean flag[2] - int turn Examples: Producer/consumer problem Producer consumer P(full) P(empty) P(mutex) P(mutex) // add to buffer //remove V(mutex) V(mutex) V(empty) V(full)