ecs150 Fall 2006: Operating System - PowerPoint PPT Presentation

About This Presentation
Title:

ecs150 Fall 2006: Operating System

Description:

10/11/2006. ecs150 Fall 2006. 1. ecs150 Fall 2006: Operating System ... For each tick (10 msec): Piestcpu = Piestcpu Tick(Pi) For each decaying period (1 second) ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 99
Provided by: astpr
Category:

less

Transcript and Presenter's Notes

Title: ecs150 Fall 2006: Operating System


1
ecs150 Fall 2006Operating System2 Scheduling
and Mutual Exclusion(chapter 4)
  • Dr. S. Felix Wu
  • Computer Science Department
  • University of California, Davis
  • http//www.cs.ucdavis.edu/wu/
  • sfelixwu_at_gmail.com

2
Kernel and User Space
Memory space for this process
Process FOO
program
System call (or trap into the kernel)
conceptually
Process FOO in the Kernel
System Call
Kernel Resources (disk or IO devices)
3
States of a Process
  • Running, Blocked, and Ready

4
Scheduling Context Switching
5
Basic Concepts
  • Maximum CPU utilization obtained with
    multiprogramming
  • CPUI/O Burst Cycle Process execution consists
    of a cycle of CPU execution and I/O wait.
  • CPU burst distribution
  • Processes classified as CPU bound or I/O bound

6
CPU Scheduler
  • Selects from among the processes in memory that
    are ready to execute, and allocates the CPU to
    one of them.
  • CPU scheduling decisions may take place when a
    process
  • 1. Switches from running to waiting state.
  • 2. Switches from running to ready state.
  • 3. Switches from waiting to ready.
  • 4. Terminates.
  • Scheduling under 1 and 4 is nonpreemptive.
  • All other scheduling is preemptive.

7
Preemptive vs.Nonpreemptive
  • Preemptive
  • Nonpreemptive
  • Pros and Cons..

8
  • User P ? Kernel Process
  • Kernel P ? Kernel Process

9
Context Switching
10
Dispatcher
  • Dispatcher module gives control of the CPU to the
    process selected by the short-term scheduler
    this involves
  • switching context
  • switching to user mode
  • jumping to the proper location in the user
    program to restart that program
  • Dispatch latency time it takes for the
    dispatcher to stop one process and start another
    running.

11
Preemptive Scheduling
  • fixed time window ?timer/clock interrupt
  • scheduling decision
  • bill the process
  • context switching might or might not happen
  • Priority
  • Fairness

12
Example two schedules
100 msec slot with around 5 msec context
switching overhead.
Which one is better? And Why?
13
Optimization Criteria
  • Max CPU utilization
  • Max throughput
  • Min turnaround time
  • Min waiting time
  • Min response time
  • but obviously, we have some trade-off

14
First-Come, First-Served (FCFS)
  • Process Burst Time
  • P1 24
  • P2 3
  • P3 3
  • Suppose that the processes arrive in the order
    P1 , P2 , P3 The Gantt Chart for the schedule
    is
  • Waiting time for P1 0 P2 24 P3 27
  • Average waiting time (0 24 27)/3 17

15
Midterm - Correction
  • November 1st of 2006 in class.
  • NSF (Wednesday) Washington DC
  • November 6th (two lectures)
  • November 8th (discussion) France
  • November 13th (two lectures)
  • November 15th (discussion) Ireland
  • November 17th (guest lecture or tape) Ireland
  • November 20th (two lectures)
  • November 22nd (discussion) France
  • Interactive grading November 2730.

16
Example 1
17
Shortest-Job-First (SJF)
  • Associate with each process the length of its
    next CPU burst. Use these lengths to schedule
    the process with the shortest time.
  • Two schemes
  • nonpreemptive once CPU given to the process it
    cannot be preempted until completes its CPU
    burst.
  • preemptive if a new process arrives with CPU
    burst length less than remaining time of current
    executing process, preempt. This scheme is know
    as the Shortest-Remaining-Time-First (SRTF).
  • SJF is optimal gives minimum average waiting
    time for a given set of processes.

18
Example
  • Process Arrival Time Burst Time
  • P1 0.0 7
  • P2 2.0 4
  • P3 4.0 1
  • P4 5.0 4
  • SJF (non-preemptive)
  • Average waiting time (0 6 3 7)/4 - 4

19
Issues for SJF
  • ???

20
CPU Bursts
  • CPUI/O Burst Cycle Process execution consists
    of a cycle of CPU execution and I/O wait.
  • CPU burst distribution
  • Processes classified as CPU bound or I/O bound

21
Determining Length of Next CPU Burst
  • Can only estimate the length.
  • Can be done by using the length of previous CPU
    bursts, using exponential averaging.

22
(No Transcript)
23
Priority Scheduling
  • A priority number (integer) is associated with
    each process
  • The CPU is allocated to the process with the
    highest priority (smallest integer ? highest
    priority).
  • Preemptive
  • Non-preemptive
  • SJF is a priority scheduling where priority is
    the predicted next CPU burst time.
  • FCFS is a priority scheduling where priority is
    the arrival time.

24
Fixed Priority
  • What is it?
  • The process sticks with the origin assigned
    priority.
  • A good or bad idea?
  • What other possible policy?
  • Dynamic policy.
  • Problem ? Starvation low priority processes may
    never execute.
  • Solution ? Aging as time progresses increase
    the priority of the process.
  • Hybrid policy.

25
Round Robin (RR)
  • Each process gets a small unit of CPU time (time
    quantum), usually 10-100 milliseconds. After
    this time has elapsed, the process is preempted
    and added to the end of the ready queue.
  • If there are n processes in the ready queue and
    the time quantum is q, then each process gets 1/n
    of the CPU time in chunks of at most q time units
    at once. No process waits more than (n-1)q time
    units.
  • Performance
  • q large ? FIFO
  • q small ? q must be large with respect to context
    switch, otherwise overhead is too high.

26
  • Process Burst Time
  • P1 53
  • P2 17
  • P3 68
  • P4 24
  • The Gantt chart is
  • Typically, higher average turnaround than SJF,
    but better response.

27
I/O system calls
1
2
3
28
Past CPU Usage
  • Round-Robin
  • How many CPU cycles so far
  • fairness
  • How about 3?

29
Past CPU Usage
  • Round-Robin
  • How many CPU cycles so far
  • fairness
  • How many CPU cycles you have used lately
  • aging factor 4 cycles

30
BSD Scheduling
  • For each tick (10 msec)
  • Piestcpu Piestcpu Tick(Pi)
  • For each decaying period (1 second)
  • Piestcpu Piestcpu(2load)/(2load1)Pinice
  • load of processes in the ready queue
  • Example 1 process in the queue
  • Piestcpu Piestcpu0.66 Pinice
  • in 4 seconds, (0.66)4 0.001296

31
Linux
  • Linux provides two process scheduling algorithms
  • Time sharing for fairness
  • Real-time, where priorities are more important
    than fairness
  • Linux allows only user-mode processes to be
    preempted
  • kernel mode processes may not be interrupted
  • Formula a balance between software real-time
    and fairness
  • Picredit Picredit / 2 Priorityi

32
Scheduling
  • Fairness Round-Robin
  • Responsiveness SJF
  • Utilization Aging, promoting System Calls.
  • Fairness in Inter-process communication

33
BSD Scheduling
  • For each tick (10 msec)
  • Piestcpu Piestcpu Tick(Pi)
  • For each decaying period (1 second)
  • Piestcpu Piestcpu(2load)/(2load1)Pinice
  • load of processes in the ready queue
  • Example 1 process in the queue
  • Piestcpu Piestcpu0.66 Pinice
  • in 4 seconds, (0.66)4 0.001296

34
Load 1
  • 2Load/(2Load1) 0.66
  • Piestcpu Piestcpu(0.66)1 20
  • Piestcpu Piestcpu(0.66)2 20
  • Piestcpu Piestcpu(0.66)3 20
  • Piestcpu Piestcpu(0.66)4 20
  • (0.66)4 0.001296

35
Load 50
  • 2Load/(2Load1) 0.99
  • Piestcpu Piestcpu(0.99)1 20
  • Piestcpu Piestcpu(0.99)2 20
  • Piestcpu Piestcpu(0.99)3 20
  • Piestcpu Piestcpu(0.99)4 20
  • (0.99)4 0.9606

36
1
RR
0
0
.
256 different priorities ?64 scheduling classes
1
0
1
37
kg_estcpu
1
RR
0
0
.
256 different priorities ?64 scheduling classes
063 bottom-half kernel (interrupt) 64127
top-half kernel 128159 real-time user 160223
timeshare 224255 idle
1
0
1
38
(No Transcript)
39
/usr/src/sys/kern/sched_4bsd.c
static void schedcpu(void arg) ...
FOREACH_PROC_IN_SYSTEM(p)
FOREACH_KSEGRP_IN_PROC(p, kg)
awake 0 FOREACH_KSE_IN_GROUP(kg,
ke) ... / end of
kse loop / kg-gtkg_estcpu
decay_cpu(loadfac, kg-gtkg_estcpu)
resetpriority(kg) FOREACH_THREAD_IN_G
ROUP(kg, td) if
(td-gttd_priority gt PUSER)
sched_prio(td, kg-gtkg_user_pri)
/ end of ksegrp loop
/ / end of process loop /
40
/usr/src/sys/kern/sched_4bsd.c
static void resetpriority(struct ksegrp kg)
register unsigned int newpriority struct
thread td if (kg-gtkg_pri_class
PRI_TIMESHARE) newpriority PUSER
kg-gtkg_estcpu /
INVERSE_ESTCPU_WEIGHT
NICE_WEIGHT (kg-gtkg_nice - PRIO_MIN)
newpriority min(max(newpriority,
PRI_MIN_TIMESHARE),
PRI_MAX_TIMESHARE) kg-gtkg_user_pri
newpriority FOREACH_THREAD_IN_GROUP(kg
, td) maybe_resched(td) / XXXKSE
silly /
41
/usr/src/sys/kern/sched_4bsd.c
struct kse sched_choose(void) struct kse
ke ke runq_choose(runq) if (ke ! NULL)
runq_remove(runq, ke) ke-gtke_state
KES_THREAD KASSERT((ke-gtke_thread !
NULL), ("runq_choose No thread on
KSE")) KASSERT((ke-gtke_thread-gttd_kse !
NULL), ("runq_choose No KSE on
thread")) KASSERT(ke-gtke_proc-gtp_sflag
PS_INMEM, ("runq_choose process swapped
out")) return (ke)
42
/usr/src/sys/kern/sched_4bsd.c
define loadfactor(loadav) (2
(loadav)) define decay_cpu(loadfac, cpu)
(((loadfac) (cpu)) / ((loadfac)
FSCALE)) static fixpt_t ccpu
0.95122942450071400909 FSCALE define
CCPU_SHIFT 11
43
/usr/src/sys/kern/kern_synch.c
/ Compute a tenex style load average of a
quantity on 1, 5 and 15 minute intervals.
XXXKSE Needs complete rewrite when correct info
is available. Completely Bogus.. only
works with 11 (but compiles ok now -)
/ static void loadav(void arg)
44
Lottery Scheduling(a dollar and a dream)
  • A process P(I) has L(I) lottery tickets.
  • Totally, we have N tickets among all processes.
  • P(I) has L(I)/N probability to get CPU cycles.
  • Difference between Priority and Tickets
  • Priority which process is more important?
  • The importance is infinitely big!!
  • Tickets how much more important?
  • Quantify the importance.

45
J, Pri(J) 1
I, Pri(I) 120
J,80
I
J
I
I
J
J
J
I
J
J
J
J
J
J
J
I
J
J
J
J
I
J
J
46
Lottery Scheduling
  • At each scheduling decision point
  • Ticket assignment
  • For each ticket in each process, produce a unique
    random number (we can not have more than one
    process sharing the prize).
  • Ticket drawing
  • Produce another random number until a winner is
    identified.

47
Efficient Implementation
  • Maybe only the of tickets matters
  • One rand( ) could produce 32 random bits.

0-16
48
Example
49
/usr/src/sys/kern/sched_4bsd.c
static void schedcpu(void arg) ...
FOREACH_PROC_IN_SYSTEM(p)
mtx_lock_spin(sched_lock)
FOREACH_KSEGRP_IN_PROC(p, kg)
awake 0 FOREACH_KSE_IN_GROUP(kg,
ke) ...
ke-gtke_sched-gtske_cpticks 0 /
end of kse loop / / If
there are ANY running threads in this KSEGRP,
then don't count it as sleeping. /
50
/usr/src/sys/kern/sched_4bsd.c
if (awake) ...
kg-gtkg_slptime 0 else
kg-gtkg_slptime if
(kg-gtkg_slptime gt 1) continue
kg-gtkg_estcpu decay_cpu(loadfac,
kg-gtkg_estcpu) resetpriority(kg)
FOREACH_THREAD_IN_GROUP(kg, td) if
(td-gttd_priority gt PUSER)
sched_prio(td, kg-gtkg_user_pri)
/ end of ksegrp loop /
51
/usr/src/sys/kern/sched_4bsd.c
void sched_clock(struct kse ke) struct ksegrp
kg struct thread td mtx_assert(sched_lock,
MA_OWNED) kg ke-gtke_ksegrp td
ke-gtke_thread ke-gtke_sched-gtske_cpticks kg-
gtkg_estcpu ESTCPULIM(kg-gtkg_estcpu 1) if
((kg-gtkg_estcpu INVERSE_ESTCPU_WEIGHT) 0)
resetpriority(kg) if (td-gttd_priority gt
PUSER) td-gttd_priority kg-gtkg_user_pri
52
/usr/src/sys/kern/kern_synch.c
avg averunnable sx_slock(allproc_lock) nr
un 0 FOREACH_PROC_IN_SYSTEM(p)
FOREACH_THREAD_IN_PROC(p, td) switch
(td-gttd_state) case TDS_RUNQ case
TDS_RUNNING if ((p-gtp_flag P_NOLOAD) !
0) goto nextproc nrun / XXXKSE
/ default break nextproc continue
sx_sunlock(allproc_lock) for (i 0
i lt 3 i) avg-gtldavgi (cexpi
avg-gtldavgi nrun FSCALE (FSCALE -
cexpi)) gtgt FSHIFT / Schedule the next
update to occur after 5 seconds, but add a
random variation to avoid synchronisation with
processes that run at regular intervals.
/ callout_reset(loadav_callout, hz 4
(int)(random() (hz 2 1)), loadav,
NULL)
53
/usr/src/sys/kern/sched_4bsd.c
static void schedcpu(void arg) ...
FOREACH_PROC_IN_SYSTEM(p)
mtx_lock_spin(sched_lock)
FOREACH_KSEGRP_IN_PROC(p, kg)
awake 0 FOREACH_KSE_IN_GROUP(kg,
ke) ...
ke-gtke_sched-gtske_cpticks 0 /
end of kse loop / / If
there are ANY running threads in this KSEGRP,
then don't count it as sleeping. /
54
Mutual Exclusion
  • Consistent access to the information.
  • Critical sections.
  • Race conditions.

55
Critical Sections
  • No two processes may be simultaneously inside
    their critical regions/sections
  • safety condition
  • No assumptions may be made about speeds or the
    number of CPUs
  • No process running outside of its critical region
    may block other processes
  • No process should have to wait forever to enter
    its critical region/section
  • liveness condition

56
Race Condition
4
Withdraw 2 from X Read balance X (1) If X lt 2
then exit Write new X X - 2(4) Release 2
Withdraw 3 from X Read balance X(2) If X lt 3
then exit Write new X X - 3(3) Release 3
2
1
2
57
(No Transcript)
58
How to Prevent?
  • Disable Interrupts
  • Busy Waiting and TSL
  • Strict Alternation
  • Protected Critical Sections
  • Hardware versus software solution.

59
Lets try some software solutions!!
  • Assuming we have only two processes.
  • The solutions should be able to extend to cover
    more general cases.

60
Solution 1
Process 0 Process 1 . .. while (turn !
0) while (turn ! 1) ltcritical
sectiongt ltcritical sectiongt turn 1 turn
0 . ...
What is the problem??
61
Problem
Process 0 Process 1 . .. while (turn !
0) ltcritical sectiongt turn
1 . ... while (turn !
0) ... while (turn ! 1) ltcritical
sectiongt turn 0 ... ltcritical
sectiongt turn 1
Maybe a very long period of time
Blocked
62
Solution 2
Process 0 Process 1 . .. while (flag1
true) while (flag0 true) flag0
true flag1 true ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
What is the problem??
63
Problem
Process 0 Process 1 flag0 false flag1
false . .. while (flag1
true) while (flag0 true) flag0
true flag1 true ltcritical
sectiongt ltcritical sectiongt flag1
false flag0 false . ...
00 01 10 11
NOT SAFE!!
64
Two Properties for ME
  • Safe no two may enter
  • Live eventually one will win

65
Solution 3
Process 0 Process 1 . .. flag0
true flag1 true while (flag1
true) while (flag0 true) ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
What is the problem??
66
Problem
Process 0 Process 1 . .. flag0
true flag1 true while (flag1
true) while (flag0 true) . ...
Nobody can progress!! (Liveness)
67
Solution 4
Process 0 Process 1 . .. flag0
true flag1 true while (flag1
true) while (flag0 true) flag0
false flag1 false ltdelay a
whilegt ltdelay a whilegt flag0 true
flag1 true ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
68
idea
Process 0 Process 1 . .. flag0
true flag1 true while (flag1 true
f2 true) while (flag0 true f2
false) flag0 false /mnbn /
flag1 false /mnbn/ flag2
flag2 flag2 flag2
flag0 true /mnbn / flag1 true
/mnbn / ltcritical sectiongt ltcritical
sectiongt flag0 false flag1
false . ...
69
Problems
  • Delay for how long?
  • A different problem Starvation with a small but
    non-zero probability.

70
Dekkers Algorithm
Process 0 Process 1 . .. flag0
true flag1 true while (flag1
true) while (flag0 true) if
(turn 1) if (turn 0)
flag0 false flag1 false while
(turn 1) while (turn 0) flag0
true flag1 true
ltcritical sectiongt ltcritical
sectiongt turn 1 turn 0 flag0
false flag1 false . ...
71
Dekkers
  • Three shared variables to solve the problem
  • flag0, flag1, and turn

72
Petersons Algorithm
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 1)) while
(flag0 (turn 0)) ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
Comparing Dekkers with Petersons!!
73
Petersons Algorithm
Process 0 Process 1 . .. flag0
true flag1 true turn
0 while (flag1 (turn
1)) turn 1 while (flag0 (turn
0)) ltcritical sectiongt ltcritical
sectiongt flag0 false flag1
false . ...
74
WWW stands for What Went Wrong?
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 1)) while
(flag0 (turn 0)) ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
75
WWW stands for What Went Wrong?
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 0)) while
(flag0 (turn 1)) ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
76
Petersons Algorithm
Process 0 Process 1 . .. flag0
true flag1 true turn
0 while (flag1 (turn
0)) turn 1 ltcritical sectiongt w
hile (flag0 (turn 1)) flag0
false ltcritical sectiongt flag1
false . ...
77
Petersons Algorithm
Process 0 Process 1 . .. flag0
true turn 0 while (flag1 (turn
0)) ltcritical sectiongt flag1
true turn 1 while (flag0
(turn 1)) flag0 false ltcritical
sectiongt flag1 false . ...
78
Petersons Algorithm
Process 0 Process 1 . .. flag0
true turn 0 flag1 true
turn 1 while (flag1 (turn
0)) while (flag0 (turn
1)) ltcritical sectiongt flag1
false ltcritical sectiongt flag0
false . ...
79
Safety and Liveliness properties
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 0)) while
(flag0 (turn 1)) At the moment when
both are in the CS flag0 1 flag1 1
turn 0 or 1 ltcritical sectiongt ltcritical
sectiongt flag0 false flag1
false . ...
80
Safety and Liveliness properties
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 0)) while
(flag0 (turn 1)) flag0 1
flag1 1 turn 0 or 1 case 1 0
?1 process 1 would not enter! ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
81
Safety and Liveliness properties
Process 0 Process 1 . .. flag0
true flag1 true turn 0 turn
1 while (flag1 (turn 0)) while
(flag0 (turn 1)) flag0 1
flag1 1 turn 0 or 1 case 1 0
?1 process 1 would not enter! case 2 1?
0 process 0 would not enter! ltcritical
sectiongt ltcritical sectiongt flag0
false flag1 false . ...
82
Monitor
Multiple Threaded Kernel
83
TSL
  • tsl register, lock
  • Do the following jobs atomically (they can not be
    broken apart)
  • copies lock to register
  • sets lock to 1

BEFORE Lock 1 AFTER Reg 1 Lock 1
BEFORE Lock 0 AFTER Reg 0 Lock 1
84
Critical Section with TSL
  • Enter??
  • Leave??

tsl register, lock
BEFORE Lock 1 AFTER Reg 1 Lock 1
BEFORE Lock 0 AFTER Reg 0 Lock 1
85
Critical Section with TSL
  • Enter??
  • loop  tsl  register, lock       cmp  register,
    0       jne  loop
  • Leave??
  • move lock, 0

86
CMPXCHG
  • Compare and Exchange
  • CMPXCHG reg, op1, op2
  • if (reg op1)
  • op1 ? op2
  • else
  • reg ? op1

87
Semaphores
  • A non-busy-waiting approach.
  • General and Binary Semaphore.

Waiting Queue for resources
I can only hold N (n4) units of resources
not really a queue!!!
88
Binary Semaphore
waitB(s) if (s.value 1) s.value
0 else place the process in s.queue block the
process signalB(s) if (s.queue is
empty) s.value 1 else remove P from
s.queue place P in the ready queue
89
General Semaphore
waitG(s) s.count -- if (s.count lt 0) place P
in s.queue block this process signalG(s) s.co
unt if (s.count lt 0) remove P from
s.queue place P in the ready queue
90
Binary Semaphore
1. How to do Mutual Exclusion with Semaphores? 2.
How to implement Semaphores in Software?
91
Mutual Exclusion
92
ME using Semaphore
. waitB(semA) ltenter critical sectiongt signalB(se
mA) ..
Waiting Queue for resources
I can only hold N (n1) units of resources
not really a queue!!!
93
Semaphores
94
Mutexes
  • A Mutex (Mutual Exclusion) is a data element that
    allows multiple threads to synchronize their
    access to shared resources
  • Like a binary semaphore, a mutex has two states,
    locked and unlocked
  • Only one thread can lock a mutex
  • Once a mutex is locked, other threads will block
    when they try to lock the same mutex, until the
    locking mutex unlocks the mutex, at which point
    one of the waiting threads lock will succeed,
    and the process begins again

95
Lock and TryLock
  • What is the difference?

96
Mutex vs. Cond
97
Pthread Implementation
in pthread_mutex_lock (mutex.c) SET_KERNEL_FLAG
semaphore implementation CLEAR_KERNEL_FLAG
in sighandler (signal.c) if (!is_in_kernel) pthr
ead_schedule_wrapper() else sigaddset()
98
Semaphores
Write a Comment
User Comments (0)
About PowerShow.com