Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS 143 Principles of Operating Systems

Similar presentations


Presentation on theme: "ICS 143 Principles of Operating Systems"— Presentation transcript:

1 ICS 143 Principles of Operating Systems
Discussion Section Week 5

2 Today Announcements Mid-term Instructions Programming Assignment
Process Synchronization HW#2 Solutions 11/18/2018

3 Announcements Mid-term exam Programming Assignment Homework #3:
Fri, 8 May 2015, 11:00-11:50am in EH 1200 Programming Assignment Out early next week Due final’s week Homework #3: Out late next week 11/18/2018

4 Midterm Instructions Fri, 8 May 2015, 11:00-11:50am in EH 1200
Please be there early!! Please bring your student ID Before sitting down, make sure a TA checked your name off the list Four problems: 15 T/F questions Processes vs. Threads CPU Scheduling Process Synchronization 11/18/2018

5 Programming Assignment Preview
Individual submission – everyone gets to code! It’s ok to discuss ideas, but everyone must submit their own code Implement different schedulers: First-Come, First-Served (FCFS) Shortest Remaining Time First (SRTF) Aka preemptive Shortest Job First (SJF) Extra credit: Proportional Share (PS) Preallocates certain amount of CPU time to each process Every job has a weight, and jobs receive a share proportional to the weight 1-page report Black-box testing Public tests and expected output provided Will test on public / private tests to make sure your code works 11/18/2018

6 Programming Assignment Preview
Input: text file One incoming process per line <process-id> <burst-time> <arrival-time> <priority> <share> Output: text file One scheduled process per line <process-id> <finish-time> <wait-time> <turnaround-time> Last line: <average-wait-time> <average-turnaround-time> Submission: .zip package via EEE DropBox 11/18/2018

7 Producer-Consumer Producer: creates filled buffers
Consumer: empties filled buffers 11/18/2018

8 Shared Data Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Need atomic operations: Operations that run to completion or not at all. In hardware: load/store - Atomic Operations required for synchronization Showed how to protect a critical section with only atomic load and store  pretty complex!

9 The Critical-Section Problem
N processes all competing to use shared data. Structure of process Pi Each process has a code segment, called the critical section, in which the shared data is accessed. repeat entry section /* enter critical section */ critical section /* access shared variables */ exit section /* leave critical section */ remainder section /* do other work */ until false Problem Ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.

10 Question 2: Critical Section
Mutual exclusion: If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Progress: If no process is executing in its critical section and there exists some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded waiting: A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. 11/18/2018

11 The Critical-Section Problem (2)
Requirements for Critical-Section Problem Assume that each process executes at a nonzero speed. No assumption concerning relative speed of the n processes. => This is where it gets tricky… if (flag[i] == true) flag[j] = false; …can get interrupted Murphy’s law will make sure we get interrupted in the worst possible moment

12 Lamport’s Bakery Algorithm
Bakery analogy: Imagine a bakery with a numbering machine at its entrance so each customer is given a unique number Numbers increase by one as customers enter A global counter displays the number of customers that is currently being served All other customers must wait until the baker finishes serving the current customer and the next number is displayed When the customer is done shopping, the number is incremented and the next customer can be served Critical section for n processes Before entering its critical section, process receives a number. Holder of the smallest number enters critical section. If processes Pi and Pj receive the same number, if i <= j, then Pi is served first; else Pj is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e. 1,2,3,3,3,3,4,4,5,5 Principles of Operating Systems - Process Synchronization

13 Lamport’s Bakery Algorithm (2)
Critical section for n processes Before entering its critical section, process receives a number. Holder of the smallest number enters critical section. If processes Pi and Pj receive the same number, if i <= j, then Pi is served first; else Pj is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e. 1,2,3,3,3,3,4,4,5,5 Notation - Lexicographic order(ticket#, process id#) (a,b) < (c,d) if (a<c) or if ((a=c) and (b < d)) max(a0,….an-1) is a number, k, such that k >=ai for i = 0,…,n-1 Shared Data var choosing: array[0..n-1] of boolean;(initialized to false) number: array[0..n-1] of integer; (initialized to 0) Principles of Operating Systems - Process Synchronization

14 Bakery Algorithm (cont.)
repeat choosing[i] := true; number[i] := max(number[0], number[1],…,number[n-1]) +1; choosing[i] := false; for j := 0 to n-1 do begin while choosing[j] do no-op; while number[j] <> 0 and (number[j] ,j) < (number[i],i) do no-op; end; critical section number[i]:= 0; remainder section until false; Makes it look atomic Be nice to others, wait until it’s your turn Principles of Operating Systems - Process Synchronization

15 Test-and-Set Shared data: var lock: boolean (initially false)
Process Pi repeat while Test-and-Set (lock) do no-op; critical section lock := false; remainder section until false; Returns current value of lock, then sets it to true In one atomic operation (HW) Principles of Operating Systems - Process Synchronization

16 Semaphores Semaphore S - integer variable (non-negative) used to represent number of abstract resources Can only be accessed via two indivisible (atomic) operations wait (S): while S <= 0 do no-op S := S-1; signal (S): S := S+1; P or wait used to acquire a resource, waits for semaphore to become positive, then decrements it by 1 V or signal releases a resource and increments the semaphore by 1, waking up a waiting P, if any If P is performed on a count <= 0, process must wait for V or the release of a resource. P():“proberen” (to test) ; V() “verhogen” (to increment) in Dutch Principles of Operating Systems - Process Synchronization

17 Semaphores (2) Critical section for n processes Shared variables
var mutex: semaphore initially mutex = 1 Process Pi repeat wait(mutex); critical section signal (mutex); remainder section until false Principles of Operating Systems - Process Synchronization

18 Homework #2 Solutions 11/18/2018

19 Question 1: Scheduling 1a) Which of the following scheduling algorithms could result in starvation? First-Come First-Served (FCFS) Shortest Job First (SJF) (Non-preemptive) Shortest Remaining Time First (SRTF) (Preemptive) Round Robin (RR) Priority For those algorithms that could result in starvation, in which situation starvation is most likely to occur? SJF and SRTF: Longer jobs may never execute Priority: Lower-priority jobs may never execute 11/18/2018

20 Question 1: Scheduling (2)
1b) Consider the set of process (smaller priority number means higher priority) 11/18/2018

21 Question 1: Scheduling (3)
11/18/2018

22 Question 1: Scheduling (4)
11/18/2018

23 Question 1: Scheduling (5)
11/18/2018

24 Question 1: Scheduling (6)
11/18/2018

25 Question 1: Scheduling (7)
11/18/2018

26 Question 1: Scheduling (8)
1.d) Briefly reason why the average waiting time of preemptive SJF is guaranteed to be no larger than that of non-preemptive. (No need to prove) SJF: Multiple, short jobs must wait on one currently executing, long process => adds to waiting time of all processes SRTF: SRTF can avoid this scenario by switching to shortest remaining time Worst case: same as SJF 11/18/2018

27 Question 1: Scheduling (9)
1e) Suppose the overhead of context switch 𝒕 is 2 milliseconds and we know that the 10 processes have roughly the same arrival time and burst time. To avoid unnecessary switch overhead, we want the total time for context switch only accounts for one-fifth of the total waiting time. Roughly speaking, what is the value of quantum q? In each round, a process must wait for 9 time quantums and 10 context switches: 9𝑞 + 10𝑡 Assume all processes will take the same number of rounds to complete Hence, ratio of waiting time to context switch time per round is the same as overall: 10𝑡 9𝑞+10𝑡 =1/5 𝑞= 40 9 𝑡 𝑡=2, so 𝑞= 80 9 ≅9 11/18/2018

28 Question 2: Test-and-Set
11/18/2018

29 Question 2: Critical Section
Mutual exclusion: If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Progress: If no process is executing in its critical section and there exists some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded waiting: A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. 11/18/2018

30 Question 2: Critical Section
Mutual exclusion: satisfied If one process is in critical section, the other cannot get in. 11/18/2018

31 Question 2: Critical Section (2)
Progress: not satisfied P0 and P1 can sync up so that they get stuck in the nested while loop. 11/18/2018

32 Question 2: Critical Section (3)
Bounded waiting: not satisfied If P1 in second loop and P0 goes from line 10 to critical section again, and again and again, P0 enters its critical section infinitely 11/18/2018

33 Question 3: Semaphores 11/18/2018

34 Question 3: Semaphores (2)
3.a) Use semaphores to enforce execution order according to the process execution diagram. s1=0; s2=0; s3=0; s4=0; s5=0; s6=0; s7=0; s8=0; 11/18/2018

35 Question 3: Semaphores (3)
Pr1: body; V(s1); V(s1); V(s1); Pr2: P(s1); body; V(s2); V(s2); Pr3: P(s2); body; V(s3); Pr4: P(s2); body; V(s4); Pr5: P(s1); body; V(s5); Pr6: P(s1); body; V(s6); Pr7: P(s6); body; V(s7); Pr8: P(s4); P(s5); body; V(s8); Pr9: P(s3); P(s7); P(s8); body 11/18/2018


Download ppt "ICS 143 Principles of Operating Systems"

Similar presentations


Ads by Google