Platform Technologies Module 3
Platform Technologies Module 3
PROCESS SYNCHRONIZATION
Module 3 of 4 modules
PLATFORM TECHNOLOGIES
Brueckner B. Aswigue
This module will discuss extensively the process synchronization and Central
Processing Unit (CPU) scheduling. The first lesson concerns with the topic of process
synchronization among concurrently executing processes includes mutual exclusion,
bounded-buffer, and readers/writers.
Another discussion is how the CPU schedules all the processes. It will introduce
the basic scheduling concepts and discuss in great length CPU scheduling. First Come
First Serve (FCFS), Short Job First (SJF), Round-Robin, Priority, and the other
scheduling algorithms should be familiar. Gantt charts, simulations, and play acting
are valuable ways to get the ideas across.
The number of hours allotted for this module shall be for 14 hours. You are
expected to finish the module in 7 weeks.
LEARNING OUTCOMES
PRE-TEST
The following questions cover general areas of this module. You may not
know the answers to all questions, but please attempt to answer them without
asking others or referring to modules.
Choose the best answer for each question and write the letter of your choice
after the number.
1
4. It is called indefinite blocking of any processes.
a. Deadlock
b. Starvation
c. Priority Inversion
d. Synchronization
5. Another problem in process synchronization that spend their lives alternating
thinking and eating
a. Dining-Philosophers problem
b. Reader-writers problem
c. Bounded-Buffer Problem
d. All of the above
6. Basis of multiprogramming operating systems
a. CPU and monitor
b. CPU scheduling
c. CPU multitasking
d. CPU ulitization
7. Once a process starts running it keeps running until it finishes.
a. Nonpreemptive
b. Preemptive
c. Scheduling
d. I/O Burst
8. It is a time required by the process to complete execution
a. Finish time
b. Jobs
c. User
d. Burst time
9. A process that you want to keep the CPU as busy as possible.
a. CPU utilization
b. Process
c. Job
d. Arrival Time
10. One measure of work is the number of processes that are completed per time
unit.
a. CPU utilization
b. Process
c. Job
d. Arrival Time
Objectives:
At the end of the lesson, you should be able to:
a. understand the basic of process synchronization;
b. understand the concepts of critical-section problem and solutions for the
consistency of shared data;
c. analyze the software and hardware solutions of the critical-section problem;
d. understand several classical process-synchronization problems; and,
e. analyze accurately tools that are used to solve process synchronization problems.
Let’s Engage.
In the context of operating system environment, the accessibility to sharable
object needs proper synchronization mechanism. Process synchronization is a
mechanism in which there should be a proper time schedule assigned between all the
cooperating processes. To implement this, it needs a rule of using sharable object. That
is, at a time only one process will get a chance to work with a sharable object, so that
remaining co-operating processes will have to wait. They will become active only after
2
they get a chance to work with sharable object. Preparing a time schedule to this
situation needs many mechanisms like mutual exclusiveness, progress and bounded
waiting. Mutual exclusiveness is a technique which allows only one process to access
the critical section of other process.
According to the Computer Science and Engineering Research Study, MIT, press
(1980), designing correct routines for controlling concurrent activities proved to be one
of the most difficult aspects of systems programming. The ad hoc techniques used by
programmers of early multiprogramming and real-time systems were always vulnerable
to subtle programming errors whose effects could be observed only when certain
relatively rare sequences of actions occurred. The errors are particularly difficult to
locate, since the precise conditions under which they appear are very hard to reproduce.
Internally, operating systems vary greatly in their makeup, since they are
organized along many different lines. The design of a new operating
system is a major task. It is important that the goals of the system be well
defined before the design begins. These goals form the basis for choices
among various algorithms and strategies.
3
PROCESS SYNCHRONIZATION
1. Background
• Processes can execute concurrently
o May be interrupted at any time, partially completing execution
• Concurrent access to shared data may result in data inconsistency
• Maintaining data consistency requires mechanisms to ensure the orderly
execution of cooperating processes
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
1. Although the producer and consumer routines shown above are correct
separately, they may not function correctly when executed concurrently. As an
illustration, suppose that the value of the variable counter is currently 5 and that
the producer and consumer processes concurrently execute the statements
“counter++” and “counter--”. Following the execution of these two statements,
the value of the variable counter may be 4, 5, or 6! The only correct result, though,
is counter == 5, which is generated correctly if the producer and consumer
execute separately.
Race Condition
2. It is called the several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order
in which the access takes place
3. counter++ could be implemented as
register1=counter
register1=register1+1
counter = register1
4. counter-- could be implemented as
4
register2=counter
register2=register2-1
counter = register2
5. Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 – 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 }
S5: consumer execute counter = register2 {counter = 4}
To guard against the race condition above, we need to ensure that only one
process at a time can be manipulating the variable counter. To make such
a guarantee, we require that the processes be synchronized in some way.
5
3. 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.
i. Assume that each process executes at a nonzero speed
ii. No assumption concerning relative speed of the n processes
8. Critical-Section Handling in OS
• Two approaches depending on if kernel is preemptive or non- preemptive
▪ Preemptive – allows preemption of process when running in kernel
mode
▪ Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
• Essentially free of race conditions in kernel mode
3. Peterson’s Solution
• Good algorithmic description of solving the problem
• Two process solution
• Assume that the load and store machine-language instructions are atomic; that
is, cannot be interrupted
• The two processes share two variables:
int turn;
Boolean flag[2]
• The variable turn indicates whose turn it is to enter the critical section
• The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready!
4. Synchronization Hardware
• Many systems provide hardware support for implementing the critical section
code.
• All solutions below based on idea of locking
o Protecting critical regions via locks
• Uniprocessors – could disable interrupts
o Currently running code would execute without preemption
o Generally too inefficient on multiprocessor systems
▪ Operating systems using this not broadly scalable
• Modern machines provide special atomic hardware instructions
o Atomic = non-interruptible
o Either test memory word and set value
o Or swap contents of two memory words
6
• Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
• test_and_set Instruction
Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
1. Executed atomically
2. Returns the original value of passed parameter
3. Set the new value of passed parameter to “TRUE”.
; /* do nothing */
7
/* critical section */
lock = 0;
/* remainder section */
} while (true);
5. Mutex Locks
• Previous solutions are complicated and generally inaccessible to application
programmers
• OS designers build software tools to solve critical section problem
• Simplest is mutex lock
• Protect a critical section by first acquire() a lock then release() the lock
o Boolean variable indicating if lock is available or not
• Calls to acquire() and release() must be atomic
o Usually implemented via hardware atomic instructions
• But this solution requires busy waiting
o This lock therefore called a spinlock
8
o Semaphore S – integer variable
o Can only be accessed via two indivisible (atomic) operations
o wait() and signal()
▪ Originally called P() and V()
o Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
o Definition of the signal() operation
signal(S) {
S++;
}
1. Semaphore Usage
o Counting semaphore – integer value can range over an unrestricted
domain
o Binary semaphore – integer value can range only between 0 and 1
o Same as a mutex lock
o Can solve various synchronization problems
o Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S 1;
signal(synch);
P2:
wait(synch);
S 2;
1. Can implement a counting semaphore S as a binary semaphore
2. Semaphore Implementation
o Must guarantee that no two processes can execute the wait() and signal()
on the same semaphore at the same time
o Thus, the implementation becomes the critical section problem where the
wait and signal code are placed in the critical section
o Could now have busy waiting in critical section implementation
a. But implementation code is short
b. Little busy waiting if critical section rarely occupied
o Note that applications may spend lots of time in critical sections and
therefore this is not a good solution
9
int value;
struct process *list;
} semaphore;
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
3. Deadlock and Starvation
o Deadlock – two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes
o Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
o Starvation – indefinite blocking
o A process may never be removed from the semaphore queue in
which it is suspended
o Priority Inversion – Scheduling problem when lower-priority
process holds a lock needed by higher-priority process
o Solved via priority-inheritance protocol
10
• The structure of the producer process
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
2. Readers-Writers Problem
• A data set is shared among a number of concurrent processes
o Readers – only read the data set; they do not perform any updates
o Writers – can both read and write
• Problem – allow multiple readers to read at the same time
o Only one single writer can access the shared data at the same time
• Several variations of how readers and writers are considered – all involve
some form of priorities
• Shared Data
o Data set
o Semaphore rw_mutex initialized to 1
o Semaphore mutex initialized to 1
o Integer read_count initialized to 0
11
• The structure of a reader process
do {
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);
3. Dining-Philosophers Problem
• Philosophers spend their lives alternating thinking and eating
• Don’t interact with their neighbors, occasionally try to pick up 2
chopsticks (one at a time) to eat from bowl
o Need both to eat, then release both when done
• In the case of 5 philosophers
o Shared data
▪ Bowl of rice (data set)
▪ Semaphore chopstick [5] initialized to 1
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
• What is the problem with this algorithm?
o Deadlock handling
▪ Allow at most 4 philosophers to be sitting simultaneously at
the table.
12
▪ Allow a philosopher to pick up the forks only if both are
available (picking must be done in a critical section.
▪ Use an asymmetric solution--an odd - numbered
philosopher picks up first the left chopstick and then the
right chopstick. Even-numbered philosopher picks up first
the right chopstick and then the left chopstick.
8. Monitors
• A high-level abstraction that provides a convenient and effective mechanism for
process synchronization
• Abstract data type, internal variables only accessible by code within the
procedure
• Only one process may be active within the monitor at a time
• But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
procedure Pn (…) {……}
Initialization code (…) { … }
}
}
Additional Notes:
1. Solaris Synchronization
• Implements a variety of locks to support multitasking, multithreading
(including real-time threads), and multiprocessing
• Uses adaptive mutexes for efficiency when protecting data from short code
segments
o Starts as a standard semaphore spin-lock
o If lock held, and by a thread running on another CPU, spins
o If lock held by non-run-state thread, block and sleep waiting for signal
of lock being released
• Uses condition variables
• Uses readers-writers locks when longer sections of code need access to data
• Uses turnstiles to order the list of threads waiting to acquire either an
adaptive mutex or reader-writer lock
o Turnstiles are per-lock-holding-thread, not per-object
• Priority-inheritance per-turnstile gives the running thread the highest of the
priorities of the threads in its turnstile
2. Windows XP Synchronization
• Uses interrupt masks to protect access to global resources on uniprocessor
systems
• Uses spinlocks on multiprocessor systems
a. Spinlocking-thread will never be preempted
13
• Also provides dispatcher objects user-land which may act mutexes,
semaphores, events, and timers
a. Events
i. An event acts much like a condition variable
b. Timers notify one or more thread when time expired
c. Dispatcher objects either signaled-state (object available) or non-signaled
state (thread will block)
3. Linux Synchronization
• Linux:
a. Prior to kernel Version 2.6, disables interrupts to implement short critical
sections
b. Version 2.6 and later, fully preemptive
• Linux provides:
a. semaphores
b. spinlocks
c. reader-writer versions of both
• On single-cpu system, spinlocks replaced by enabling and disabling kernel
preemption
If you want to know more interesting facts about this module, visit the
following:
• Operating system. http://www.wiley.com/college and clicking
“Who’s my rep?”
• Operating System. http://www.os-book.com.
• It may be a good idea to review the basic concepts of machine
organization and assembly language programming. You should be
comfortable with the concepts of memory, CPU, registers, I/O,
interrupts, instructions, and the instruction execution cycle. Since
the operating system is the interface between the hardware and
user programs, a good understanding of operating systems
requires an understanding of both hardware and programs.
• The powerpoint presentation will be additional references for you
to elaborate some of the topics see the attachment.
Direction: Read the passage carefully and plan what you will write. Place your answer
in the pad paper whether yellow or white to be submitted. The Essay rubrics have a
correspond points that will guide you in your essay. Explain how it is possible for
static methods also to be declared as synchronized.
14
Grammar, Virtually no Few spelling and A number of So many spelling,
Usage & spelling, punctuation spelling, punctuation and
Mechanics punctuation or errors, minor punctuation or grammatical
grammatical grammatical grammatical errors that it
errors errors errors interferes with the
meaning
Question: In this lesson, we used the synchronized statement with instance methods.
Calling an instance method requires associating the method with an object. Entering a
synchronized method requires owning the object’s lock. Static methods are unlike
instance methods in that they do not require association with an object when they are
called.
“Construct your determination with Sustained Effort,
Controlled Attention and Concentrated Energy, Opportunities never come to those who
wait… they are captured by those who dare to attack” – Paul J. Meyer
Objectives:
At the end of the lesson, you should be able to:
1. distinguish precisely the basis for multiprogrammed OS in CPU scheduling;
2. formulate thoroughly various CPU-scheduling algorithms; and
3. analyze precisely the scheduling algorithms of several operating systems.
CPU Scheduling is a process of determining which process will own CPU for
execution while another process is on hold. The main task of CPU scheduling is to make
sure that whenever the CPU remains idle, the OS at least select one of the processes
available in the ready queue for execution. The selection process will be carried out by
the CPU scheduler. It selects one of the processes in memory that are ready for
execution.
1. Basic Concepts
• Maximum CPU utilization obtained with multiprogramming
15
• CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution
and I/O wait
• CPU burst followed by I/O burst
• CPU burst distribution is of main concern
2. CPU Scheduler
• Short-term scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them
o Queue may be ordered in various ways
• 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 or cooperative.
16
• Under these conditions, once a process starts running it keeps running,
until it either voluntarily blocks or until it finishes. Otherwise the system is
said to be preemptive.
• All other scheduling is preemptive
o Consider access to shared data
o Consider preemption while in kernel mode
o Consider interrupts occurring during crucial OS activities
a. Dispatcher
• Dispatcher module gives control of the CPU to the process selected by the
short-term scheduler; this involves:
o switching context
o switching to user mode
o 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.
• The dispatcher should be as fast as possible, given that it is invoked during
every process switch. The time taken by the dispatcher to stop one process
and start another process is known as the Dispatch Latency. Dispatch
Latency can be explained using the below figure:
2. Scheduling Criteria
Many criteria have been suggested for comparing CPU-scheduling
algorithms. Which characteristics are used for comparison can make a
substantial difference in which algorithm is judged to be best. The criteria
include the following:
17
o CPU utilization. We want to keep the CPU as busy as possible.
Conceptually, CPU utilization can range from 0 to 100 percent. In a real
system, it should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily loaded system).
o Throughput. If the CPU is busy executing processes, then work is being
done. One measure of work is the number of processes that are completed
per time unit, called throughput. For long processes, this rate may be one
process per hour; for short transactions, it may be ten processes per
second.
o Turnaround time. From the point of view of a particular process, the
important criterion is how long it takes to execute that process. The
interval from the time of submission of a process to the time of completion
is the turnaround time. Turnaround time is the sum of the periods spent
waiting to get into memory, waiting in the ready queue, executing on the
CPU, and doing I/O.
o Waiting time. The CPU-scheduling algorithm does not affect the amount
of time during which a process executes or does I/O. It affects only the
amount of time that a process spends waiting in the ready queue. Waiting
time is the sum of the periods spent waiting in the ready queue.
o Response time. In an interactive system, turnaround time may not be the
best criterion. Often, a process can produce some output fairly early and
can continue computing new results while previous results are being
output to the user. Thus, another measure is the time from the submission
of a request until the first response is produced. This measure, called
response time, is the time it takes to start responding, not the time it takes
to output the response. The turnaround time is generally limited by the
speed of the output device.
3. Scheduling Algorithms
CPU scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU. There are many
different CPU-scheduling algorithms.
18
Preemptive Scheduling is a scheduling method where the tasks are mostly
assigned with their priorities. Sometimes it is important to run a task with a
higher priority before another lower priority task, even if the lower priority task
is still running.
At that time, the lower priority task holds for some time and resumes when
the higher priority task finishes its execution.
In this type of scheduling method, the CPU has been allocated to a specific
process. The process that keeps the CPU busy will release the CPU either by
switching context or terminating.
It is the only method that can be used for various hardware platforms.
That's because it doesn't need specialized hardware (for example, a timer) like
preemptive Scheduling.
A processor can be preempted to execute Once the processor starts its execution,
the different processes in the middle of it must finish it before executing the
any current process execution. other. It can't be paused in the middle.
Waiting and response time of preemptive Waiting and response time of the non-
Scheduling is less. preemptive Scheduling method is
higher.
19
Preemptive Scheduling algorithm can be In non-preemptive scheduling process
pre-empted that is the process can be cannot be Scheduled
Scheduled
In this process, the CPU is allocated to In this process, CPU is allocated to the
the processes for a specific time period. process until it terminates or switches
to the waiting state.
20
Example of Non-Preemptive Scheduling
Consider the following five processes each having its own unique burst time
and arrival time.
Step 2) At time =2, process P1 arrives and is added to the waiting queue. P4
will continue execution.
Step 3) At time = 3, process P4 will finish its execution. The burst time of P3
and P1 is compared. Process P1 is executed because its burst time is less
compared to P3.
21
Step 5) At time = 5, process P2 arrives and is added to the waiting queue. P1
will continue execution.
Step 6) At time = 9, process P1 will finish its execution. The burst time of P3,
P5, and P2 is compared. Process P2 is executed because its burst time is the
lowest.
Step 8) At time = 11, process P2 will finish its execution. The burst time of P3
and P5 is compared. Process P5 is executed because its burst time is lower.
22
Step 11) Let's calculate the average waiting time for above example.
Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
Average Waiting Time= 0+1+4+7+14/5 = 26/5 = 5.2
Step 1) The execution begins with process P1, which has burst time 4. Here,
every process executes for 2 seconds. P2 and P3 are still in the waiting queue.
Step 2) At time =2, P1 is added to the end of the Queue and P2 starts executing
Step 3) At time=4 , P2 is preempted and add at the end of the queue. P3 starts
executing.
23
Step 4) At time=6 , P3 is preempted and add at the end of the queue. P1 starts
executing.
Step 7) Let's calculate the average waiting time for above example.
Wait time
P1= 0+ 4= 4
P2= 2+4= 6
P3= 4+3= 7
KEY DIFFERENCES
24
1. First- Come, First-Served (FCFS) Scheduling
o FCFS is very simple - Just a FIFO queue, like customers waiting in line at
the bank or the post office or at a copying machine.
o Unfortunately, however, FCFS can yield some very long average wait times,
particularly if the first process to get there takes a long time. For example,
consider the following three processes:
CONVOY EFFECT. When one CPU intensive process blocks the CPU, a number
of I/O intensive processes can get backed up behind it, leaving the I/O devices
idle. When the CPU hog finally relinquishes the CPU, then the I/O processes pass
through the CPU quickly, leaving the CPU idle while everyone queues up for I/O,
and then the cycle repeats itself when the CPU intensive process gets back to the
ready queue.
Advantages
o Better for long processes
o Simple method (i.e., minimum overhead on processor)
o No starvation
Disadvantages
o Convoy effect occurs. Even very small process should wait for its turn to
come to utilize the CPU. Short process behind long process results in
lower CPU utilization.
o Throughput is not emphasized.
25
2. Shortest-Job-First (SJF) Scheduling
• The idea behind the SJF algorithm is to pick the quickest fastest little job
that needs to be done, get it out of the way first, and then pick the next
smallest fastest job to do next.
• Technically this algorithm picks a process based on the next shortest CPU
burst, not the overall process time.
• Associate with each process the length of its next CPU burst
o Use these lengths to schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a given set of
processes
o The difficulty is knowing the length of the next CPU request
o Could ask the user
• Example
Advantages
o It gives superior turnaround time performance to shortest process next
because a short job is given immediate preference to a running longer
job.
o Throughput is high.
Disadvantages
o Elapsed time (i.e., execution-completed-time) must be recorded, it results
an additional overhead on the processor.
o Starvation may be possible for the longer processes.
3. Shortest-remaining-time-first (SRTF)
o Preemptive SJF is sometimes referred to as shortest remaining time
first scheduling.
o Preemption occurs when a new process arrives in the ready queue that
has a predicted burst time shorter than the time remaining in the
process whose burst is currently on the CPU.
o Now we add the concepts of varying arrival times and preemption to the
analysis.
26
Using Short cut Method
Determine the Average of turnaround Time and Waiting Time (Based
on the Gantt Chart (G.C.)
FINISH
TIME of
minus A.T. equal TT minus B.T equal W.T
Process
P1 17 - 0 = 17 - 8 = 9
P2 5 - 1 = 4 - 4 = 0
P3 26 - 2 = 24 - 9 = 15
P4 10 - 3 = 7 - 5 = 2
TOTAL 50 TOTAL 26
Ave. T.T. 12.5 Ave. W.T. 6.5
Note: In the P1 in the G.C., it has two presentation of time 1 and 17. Get
only the time where the process end in the Gantt chart. P1 ends at time
17. This Formula is applicable to all CPU Scheduling.
CPU Utilization = (Last number in the Gantt Chart) minus (the total of
Burst time)
= 20/20 x 100% = 100%
o Compare the value to 7.75 ms for non-preemptive SJF or 8.75 for FCFS. )
4. Priority Scheduling
• Priority scheduling is a more general case of SJF, in which each job is
assigned a priority and the job with the highest priority gets scheduled first.
(SJF uses the inverse of the next expected burst time as its priority - The
smaller the expected burst, the higher the priority. )
• Note that in practice, priorities are implemented using integers within a
fixed range, but there is no agreed-upon convention as to whether "high"
priorities use large numbers or small numbers.
27
b. Non-Preemptive Scheduling (NPP)
In this type of scheduling method, the CPU has been allocated to a
specific process. The process that keeps the CPU busy, will release the CPU
either by switching context or terminating. It is the only method that can be
used for various hardware platforms. That's because it doesn't need special
hardware (for example, a timer) like preemptive scheduling.
Characteristics of Priority Scheduling
• A CPU algorithm that schedules processes based on priority.
• It used in Operating systems for performing batch processes.
• If two jobs having the same priority are READY, it works on a FIRST
COME, FIRST SERVED basis.
• In priority scheduling, a number is assigned to each process that
indicates its priority level.
• Lower the number, higher is the priority.
• In this type of scheduling algorithm, if a newer process arrives, that is
having a higher priority than the currently running process, then the
currently running process is preempted.
Consider following five processes P1 to P5. Each process has its unique
priority, burst time, and arrival time.
Step 0) At time=0, Process P1 and P2 arrive. P1 has higher priority than P2.
The execution begins with process P1, which has burst time 4.
Step 2) At time 2, no new process arrives, so you can continue with P1.
P2 is in the waiting queue.
28
Step 3) At time 3, no new process arrives so you can continue with P1.
P2 process still in the waiting queue.
29
Step 8) At time= 8, no new process arrives, so we can continue with P3.
Step 10) At time interval 10, no new process comes, so we continue with
P3
30
Step 12) At time=12, P5 arrives. P3 has higher priority, so it continues
execution.
Step 14) At time =14, the P2 process has finished its execution. P4 and
P5 are in the waiting state. P5 has the highest priority and starts
execution.
Step 16) At time= 16, P5 is finished with its execution. P4 is the only
process left. It starts execution.
31
Step 17) At time =20, P5 has completed execution and no process is left.
Step 18) Let's calculate the average waiting time and turnaround time
for the above example.
P1 4 - 0 = 4 - 4 = 0
P2 14 - 0 = 14 - 3 = 11
P3 13 - 6 = 7 - 7 = 0
P4 20 - 11 = 9 - 4 = 5
P5 160 - 12 = 4 - 2 = 2
TOTAL 38 TOTAL 18
Ave. T.T. 9.6 Ave. W.T. 3.6
Note: In the P2 in the G.C., it has two presentation of time 6 and 14. Get
only the time where the process end in the Gantt chart. The P2 finished
at time 14. This Formula is applicable to all CPU Scheduling.
CPU Utilization = (Last number in the Gantt Chart) minus (the total of
Burst time)
= 20/20 x 100% = 100%
32
Disadvantages of priority scheduling
• If the system eventually crashes, all low priority processes get lost.
• If high priority processes take lots of CPU time, then the lower
priority processes may starve and will be postponed for an
indefinite time.
• This scheduling algorithm may leave some low priority processes
waiting indefinitely.
• A process will be blocked when it is ready to run but has to wait
for the CPU because some other process is running currently.
• If a new higher priority process keeps on coming in the ready
queue, then the process which is in the waiting state may need to
wait for a long duration of time.
Summary:
• Priority scheduling is a method of scheduling processes that is
based on priority. In this algorithm, the scheduler selects the
tasks to work as per the priority.
• In Priority Preemptive Scheduling, the tasks are mostly assigned
with their priorities.
• In Priority Non-preemptive scheduling method, the CPU has been
allocated to a specific process.
• Processes are executed on the basis of priority so high priority
does not need to wait for long which saves time
• If high priority processes take lots of CPU time, then the lower
priority processes may starve and will be postponed for an
indefinite time.
5. Round-Robin Scheduling
The name of this algorithm comes from the round-robin principle, where
each person gets an equal share of something in turns. It is the oldest, simplest
scheduling algorithm, which is mostly used for multitasking.
33
Example of Round-robin Scheduling
P1 4
P2 3
P3 5
Step 1) The execution begins with process P1, which has burst time 4. Here,
every process executes for 2 seconds. P2 and P3 are still in the waiting queue.
Step 2) At time =2, P1 is added to the end of the Queue and P2 starts executing
Step 3) At time=4, P2 is preempted and add at the end of the queue. P3 starts
executing.
Step 4) At time=6, P3 is preempted and add at the end of the queue. P1 starts
executing.
34
Step 5) At time=8 , P1 has a burst time of 4. It has completed execution. P2
starts execution
Step 7) Let's calculate the average waiting time for above example.
Wait time
P1= 0+ 4= 4
P2= 2+4= 6
P3= 4+3= 7
Try yourself: Compute the Average of waiting time, turnaround time, and CPU utilization.
35
• Its performance heavily depends on time quantum.
• Priorities cannot be set for the processes.
• Round-robin scheduling doesn't give special priority to more important
tasks.
• Decreases comprehension
• Lower time quantum results in higher the context switching overhead in
the system.
• Finding a correct time quantum is a quite difficult task in this system.
Worst Case Latency
This term is used for the maximum time taken for execution of all the tasks.
• dt = Denote detection time when a task is brought into the list
• st = Denote switching time from one task to another
• et = Denote task execution time
Formula:
Tworst = {(dti+ sti + eti ), + (dti+ sti + eti )2 +...+ (dti+ sti + eti )N., + (dti+ sti + eti + eti)
N} + tISR
t,SR = sum of all execution times
Summary:
• The name of this algorithm comes from the round-robin principle, where
each person gets an equal share of something in turns.
• Round robin is one of the oldest, fairest, and easiest algorithms and
widely used scheduling methods in traditional OS.
• Round robin is a pre-emptive algorithm
• The biggest advantage of the round-robin scheduling method is that If
you know the total number of processes on the run queue, then you can
also assume the worst-case response time for the same process.
• This method spends more time on context switching
• Worst-case latency is a term used for the maximum time taken for the
execution of all the tasks.
36
Multilevel Feedback-Queue Scheduling
Thread Scheduling
Contention Scope
• Contention scope refers to the scope in which threads compete for the use of
physical CPUs.
• On systems implementing many-to-one and many-to-many threads, Process
Contention Scope, PCS, occurs, because competition occurs between threads
that are part of the same process. ( This is the management / scheduling of
multiple user threads on a single kernel thread, and is managed by the thread
library. )
• System Contention Scope, SCS, involves the system scheduler scheduling
kernel threads to run on one or more CPUs. Systems implementing one-to-one
threads ( XP, Solaris 9, Linux ), use only SCS.
• PCS scheduling is typically done with priority, where the programmer can set
and/or change the priority of threads created by his or her programs. Even time
slicing is not guaranteed among threads of equal priority.
37
Multiple-Processor Scheduling
• When multiple processors are available, then the scheduling gets more
complicated, because now there is more than one CPU which must be kept
busy and in effective use at all times.
• Load sharing revolves around balancing the load between multiple processors.
• Multi-processor systems may be heterogeneous, ( different kinds of CPUs ),
or homogenous, ( all the same kind of CPU ). Even in the latter case there may
be special scheduling constraints, such as devices which are connected via a
private bus to only one of the CPUs. This module will restrict its discussion to
homogenous systems.
Processor Affinity
• Processors contain cache memory, which speeds up repeated accesses to
the same memory locations.
• If a process were to switch from one processor to another each time it got
a time slice, the data in the cache ( for that process ) would have to be
invalidated and re-loaded from main memory, thereby obviating the
benefit of the cache.
• Therefore SMP systems attempt to keep processes on the same
processor, via processor affinity. Soft affinity occurs when the system
attempts to keep processes on the same processor but makes no
guarantees. Linux and some other OSes support hard affinity, in which
a process specifies that it is not to be moved between processors.
• Main memory architecture can also affect process affinity, if particular
CPUs have faster access to memory on the same chip or board than to
other memory loaded elsewhere. ( Non-Uniform Memory Access, NUMA. )
As shown below, if a process has an affinity for a particular CPU, then it
should preferentially be assigned memory storage in "local" fast access
areas.
38
Load Balancing
Multicore Processors
• Traditional SMP required multiple CPU chips to run multiple kernel threads
concurrently.
• Recent trends are to put multiple CPUs ( cores ) onto a single chip, which
appear to the system as multiple processors.
• Compute cycles can be blocked by the time needed to access memory, whenever
the needed data is not already present in the cache. ( Cache misses. ) In Figure
5.10, as much as half of the CPU cycles are lost to memory stall.
39
1. Coarse-grained multithreading switches between threads only when one
thread blocks, say on a memory read. Context switching is similar to
process switching, with considerable overhead.
2. Fine-grained multithreading occurs on smaller regular intervals, say on
the boundary of instruction cycles. However the architecture is designed
to support thread switching, so the overhead is relatively minor.
• Note that for a multi-threaded multi-core system, there are two levels of
scheduling, at the kernel level:
1. The OS schedules which kernel thread(s) to assign to which logical
processors, and when to make context switches using algorithms as
described above.
2. On a lower level, the hardware schedules logical processors on each
physical core using some other algorithm.
▪ The UltraSPARC T1 uses a simple round-robin method to
schedule the 4 logical processors (kernel threads) on each physical
core.
▪ The Intel Itanium is a dual-core chip which uses a 7-level priority
scheme (urgency) to determine which thread to schedule when one
of 5 different events occurs.
Real-time systems are those in which the time at which tasks complete is crucial to
their performance
Minimizing Latency
• Event Latency is the time between the occurrence of a triggering event and
the ( completion of ) the system's response to the event:
40
• In addition to the time it takes to actually process the event, there are two
additional steps that must occur before the event handler ( Interrupt Service
Routine, ISR ), can even start:
o Interrupt processing determines which interrupt(s) have occurred,
and which interrupt handler routine to run. In the event of
simultaneous interrupts, a priority scheme is needed to determine
which ISR has the highest priority and gets to go next.
o Context switching involves removing a running process from the CPU,
saving its state, and loading up the ISR so that it can run.
Priority-Based Scheduling
41
• Hard real-time systems are often characterized by tasks that must run at
regular periodic intervals, each having a period p, a constant time required
to execute, ( CPU burst ), t, and a deadline after the beginning of each period
by which the task must be completed, d.
• In all cases, t <= d <= p
Rate-Monotonic Scheduling
42
▪ For example, supposing that P1 =50, T1 = 25, P2 = 80, T2 =
35, and the deadlines match the periods.
▪ Overall CPU usage is 25/50 = 0.5 for P1, 35 / 80 =0.44 for P2,
or 0.94 ( 94% ) overall, indicating it should be possible to
schedule the processes.
▪ With rate-monotonic scheduling, P1 goes first, and completes
its first burst at time 25.
▪ P2 goes next, and completes 25 out of its 35 time units before
it gets pre-empted by P1 at time 50.
▪ P1 completes its second burst at 75, and then P2 completes its
last 10 time units at time 85, missing its deadline of 80 by 5
time units. :-(
Earliest-Deadline-First Scheduling
43
Proportional Share Scheduling
• POSIX defines two scheduling classes for real time threads, SCHED_FIFO
and SCHED_RR, depending on how threads of equal priority share time.
• SCHED_FIFO schedules tasks in a first-in-first-out order, with no time
slicing among threads of equal priority.
• SCHED_RR performs round-robin time slicing among threads of equal
priority.
• POSIX provides methods for getting and setting the thread scheduling
policy, as shown below:
The Linux CFS scheduler provides an efficient algorithm for selecting which
task to run next. Each runnable task is placed in a red-black tree—a balanced
binary search tree whose key is based on the value of vruntime. This tree is
shown below:
44
( smaller values of vruntime ) are toward the left side of the tree, and tasks
that have been given more processing time are on the right side. According
to the properties of a binary search tree, the leftmost node has the smallest
key value, which for the sake of the CFS scheduler means that it is the task
with the highest priority . Because the red-black tree is balanced, navigating
it to discover the leftmost node will require O(lgN) operations (where N
is the number of nodes in the tree). However, for efficiency reasons, the
Linux scheduler caches this value in the variable rb_leftmost, and thus
determining which task to run next requires only retrieving the cached value.
45
Figure 19 – List of tasks indexed according to priority (Operating System.
http://www.os-book.com).
46
when those events occur. ( Larger numbers are a higher, i.e. better
priority. )
• Solaris 9 introduced two new scheduling classes: Fixed priority and fair share.
o Fixed priority is similar to time sharing, but not adjusted dynamically.
o Fair share uses shares of CPU time rather than priorities to schedule
jobs. A certain share of the available CPU time is allocated to a project,
which is a set of processes.
• System class is reserved for kernel use. ( User programs running in kernel
mode are NOT considered in the system scheduling class. )
47
ALGORITHM EVALUATION
• The first step in determining which algorithm (and what parameter settings
within that algorithm) is optimal for a particular operating environment is to
determine what criteria are to be used, what goals are to be targeted, and what
constraints if any must be applied. For example, one might want to "maximize
CPU utilization, subject to a maximum response time of 1 second".
• Once criteria have been established, then different algorithms can be analyzed
and a "best choice" determined. The following sections outline some different
methods for determining the "best choice".
Deterministic Modeling
• If a specific workload is known, then the exact values for major criteria can be
fairly easily calculated, and the "best" determined. For example, consider the
following workload ( with all processes arriving at time 0 ), and the resulting
schedules determined by three different algorithms:
P1 10
P2 29
P3 3
P4 7
P5 12
FCFS:
Non-preemptive SJF:
Round Robin:
• The average waiting times for FCFS, SJF, and RR are 28ms, 13ms, and 23ms
respectively.
48
• Deterministic modeling is fast and easy, but it requires specific known input,
and the results only apply for that particular set of input. However by
examining multiple similar cases, certain trends can be observed. ( Like the fact
that for processes arriving at the same time, SJF will always yield the shortest
average wait time. )
Queuing Models
• Specific process data is often not available, particularly for future times.
However a study of historical performance can often produce statistical
descriptions of certain important parameters, such as the rate at which new
processes arrive, the ratio of CPU bursts to I/O times, the distribution of CPU
burst times and I/O burst times, etc.
• Armed with those probability distributions and some mathematical formulas, it
is possible to calculate certain performance characteristics of individual waiting
queues. For example, Little's Formula says that for an average queue length of
N, with an average waiting time in the queue of W, and an average arrival of
new jobs in the queue of Lambda, then these three terms can be related by:
N = Lambda * W
Simulations
49
Figure 23 - Evaluation of CPU schedulers by simulation (Operating System.
http://www.os-book.com).
Implementation
• The only real way to determine how a proposed scheduling algorithm is going to
operate is to implement it on a real system.
• For experimental algorithms and those under development, this can cause
difficulties and resistance among users who don't care about developing OSes
and are only trying to get their daily work done.
• Even in this case, the measured results may not be definitive, for at least two
major reasons: (1) System work loads are not static, but change over time as
new programs are installed, new users are added to the system, new hardware
becomes available, new work projects get started, and even societal changes.
(For example the explosion of the Internet has drastically changed the amount
of network traffic that a system sees and the importance of handling it with
rapid response times). (2) As mentioned above, changing the scheduling system
may have an impact on the work load and the ways in which users use the
system. (The lesson gives an example of a programmer who modified his code to
write an arbitrary character to the screen at regular intervals, just so his job
would be classified as interactive and placed into a higher priority queue).
• Most modern systems provide some capability for the system administrator to
adjust scheduling parameters, either on the fly or as the result of a reboot or a
kernel rebuild.
50
If you want to know more interesting facts about Operating System
structures, visit the following:
• Operating system. http://www.wiley.com/college and clicking
“Who’s my rep?”
• The powerpoint and movies presentation will be additional references for you
to elaborate some of the topics see the attachment.
Activity:
Direction: Read the passage carefully and plan what you will write. Place your
answer in the pad paper (yellow or white) to be submitted. Each question has 10
points each. The Essay rubrics have a correspond points that will guide you in
your essay.
“Construct your determination with Sustained Effort, Controlled Attention and Concentrated
Energy, Opportunities never come to those who wait… they are captured by those who dare to
attack” – Paul J. Meyer
Features 9-10 points 7-8 points 4-6 points 1-3 points
Expert Accomplished Capable Beginner
Understanding Writing shows Writing shows a Writing shows Writing shows
strong clear adequate little
understanding understanding understanding understanding
Quality of Piece was Piece was written Piece had little Piece had no style
Writing written in an in an interesting style
extraordinary style
style
Gives no new
Very informative Somewhat Gives some new information and
and well- informative and information but very poorly
organized organized poorly organized organized
Grammar, Virtually no Few spelling and A number of So many spelling,
Usage & spelling, punctuation spelling, punctuation and
Mechanics punctuation or errors, minor punctuation or grammatical
grammatical grammatical grammatical errors that it
errors errors errors interferes with the
meaning
QUESTIONS:
1. A CPU-scheduling algorithm determines an order for the execution of its scheduled
processes. Given n processes to be scheduled on one processor, how many different
schedules are possible? Give a formula in terms of n.
51
3. Suppose that the following processes arrive for execution at the times indicated.
Each process will run for the amount of time listed. In answering the questions, use
nonpreemptive scheduling, and base all decisions on the information you have at the
time the decision must be made.
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. What is the average turnaround time for these processes with the
FCFS scheduling algorithm?
b. What is the average turnaround time for these processes with the
SJF scheduling algorithm?
c. The SJF algorithm is supposed to improve performance, but notice
that we chose to run process P1 at time 0 because we did not know
that two shorter processes would arrive soon. Compute what the
average turnaround time will be if the CPU is left idle for the first
1 unit and then SJF scheduling is used. Remember that processes
P1 and P2 are waiting during this idle time, so their waiting time
may increase. This algorithm could be called future-knowledge
scheduling.
4. What advantage is there in having different time-quantum sizes at
different levels of a multilevel queueing system?
POST TEST
Directions: The following questions cover general areas of this module. You may not
know the answers to all questions, but please attempt to answer them without asking
others or referring to books. Place your answer at the separate page or pad paper in able
to be submitted.
Choose the best answer for each question and write the letter of your
choice after the number.
1. It is called the several process access and manipulate the same data concurrently.
a. Critical Section Problem
b. Race Condition
c. General Structure
d. Entry Section
2. A solution to the critical problem that are complicated and generally inaccessible to
application programmer
a. Mutex Locks
b. Compare_ad_swap
c. Bounded-waiting mutual exclusion
d. Test-and-set
3. Two or more process are waiting indefinitely for an event that can be caused by
only one of the waiting processes
a. Deadlock
b. Semaphore
c. Synchronization
d. Starvation
52
4. Part in the Reader-writer problem that they do not perform any updates
a. Mutexlock
b. Dataset
c. Writers
d. Readers
5. A high level abstraction that provides a convenient and effective mechanism for
process synchronization.
a. Monitor
b. Solaris
c. Turnstile
d. Readers-writer
6. Short-term scheduler select from among the process in ready queue and allocate
the CPU to one of them
a. CPU – I/O
b. CPU scheduler
c. CPU scheduling
d. CPU Ulitization
7. Give control of the CPU to the process selected by the short-term scheduler
a. Latency
b. Dispatcher
c. Response Time
d. Switch context
8. A number of program which can present in memory at the same time.
a. Multiprogramming
b. Jobs
c. User
d. CPU/IO Burst
9. An important criterion is how long it takes to execute that process.
a. CPU utilization
b. Throughput
c. Turnaround Time
d. Waiting Time
10. The sum of the period spent waiting in the ready queue.
a. CPU utilization
b. Throughput
c. Turnaround Time
d. Waiting Time
REFERENCES
Silberschatz, A. et. al. (2013). Operating System Concepts. 9th Edition. Hoboken, New
Jersey, USA: John Wiley & Sons, Inc.
Stallings, William (2012). Operating System Internals and Design Principles. 7th edition.
Upper Saddle River, New Jersey: Pearson Education, Inc.
53