You are on page 1of 144

CoSc2042

Operating Systems

Computer Science Program


Wollega University
March 2015
Chapter Two
Process and Process Management

2
Chapter Objectives
To introduce the notion of a process- a program in
execution, which forms the basis of all computation.
To describe the various features of processes,
including scheduling, creation and termination, and
communication.
To introduce the notion of a thread- a fundamental
unit of CPU utilization that forms the basis of
multithreaded computer systems.

3
Topics Included
The Process Concept
The Threads Concept
Inter-process Communication
Process Scheduling

4
The Process Concept

5
Objectives
After completing this topic you should be able to:
 Define process
 Differentiate program and process
 Explain different types of processes
 Explain different states of process
 Understand how OS manages processes

6
1 Process Concept
Process vs. Program
 Program
 It is sequence of instructions defined to perform some task
 It is a passive entity, a file containing a list of instructions stored on
disk(often called an executable file).
 Process
 It is a program in execution. A process needs certain resources,
including CPU time, memory, files, and I/O devices, to accomplish
its task.
 It is an instance of a program running on a computer.
 It is an active entity, with a program counter specifying the next
instruction to execute and a set of associated resources.
 A processor performs the actions defined by a process.
 A program becomes a process when an executable file is loaded into
memory.
 A process is more than the program code.. I
7
Process concept(cont’d)
 A process includes:
 program counter: contains the address of the next instruction to
be fetched
 Stack: which contains temporary data (such as function
parameters, return addresses, and local variables).
 data section: which contains global variables.
 Real life Example: Consider a computer scientist who is baking
a birthday cake for her daughter and who is interrupted by her
daughter’s bleeding accident

Analysis
Processes Baking Cake First Aid
Processor Computer Scientist Computer Scientist
Program Recipe First Aid Book
Input Ingredients First Aid Kit
Output Cake First Aid Service
Priority Lower Higher
States Running, Idle Running, Idle 8
Process concept(cont’d)
Sequence of actions
 Bringing ingredients i.e. flour, sugar, eggs, etc
 Placing the mixture into the oven
 Following the baking processes
 Hearing a cry and analyzing it to be because of bleeding
 Recording baking processes state and switching to provide
first aid service
 Providing first aid service
 Coming back and resuming the baking process

The key idea here is that a process is an activity of


some kind. It has a program, input, output, and a
state.

9
2. Types of Processes
There are two types of processes
 Sequential Processes
 Concurrent Processes

Sequential Processes
 Execution progresses in a sequential fashion.
 At any point in time, at most one process is being executed

Concurrent Processes - two types of concurrent


processes
 True Concurrency (Multiprocessing)
 Two or more processes are executed simultaneously in a
multiprocessor environment
 Supports real parallelism

10
Types of Processes(cont’d)
 Apparent Concurrency (Multiprogramming)
 Two or more processes are executed in parallel in a uniprocessor
environment by switching from one process to another
 Supports pseudo parallelism, i.e. the fast switching among
processes gives illusion of parallelism

11
3. Process Creation
Principal events that cause process creation:
 System initialization.
 When an operating system is booted, often several processes are
created. Some of them are foreground and others are background.
 Foreground processes: processes that interact with (human) users
and perform work for them.
 Background processes, which are not associated with particular
users, but instead have some specific function. For example, a
background process may be designed to accept incoming requests
for web pages hosted on that machine.
 Execution of a process creation system call by a running
process.
 a running process will issue system calls to create one or more
new processes to help it do its job.
 A user request to create a new process. E.g. open a document
 Initiation of a batch job(on large mainframes).
12
4. Process State
 During its lifetime, a process passes through a number of states.
 The most important states are: Ready, Running, Blocked
(waiting)

New
 A process that has just been created but has not yet been
admitted to the pool of executable processes by the operating
system
 Information concerning the process is already maintained in
memory but the code is not loaded and no space has been
allocated for the process

Ready
 A process that is not currently executing but that is ready to be
executed as soon as the operating system dispatches it

13
Process State(cont’d)
Running
 A process that is currently being executed

Blocked (Waiting)
 A process that is waiting for the completion of some event,
such as completion of an I/O operation

Exit (Terminated)
 A process that has been released from the pool of executable
processes by the operating system, either because it halted or
because it aborted for some reason.
 Conditions:
 Normal exit: processes terminate because they have done their
work

14
Process State(cont’d)
 Error exit: due to a program bug. Examples include executing an
illegal instruction, referencing nonexistent memory, or dividing by
zero.
 Fatal error: For example, if a user types the command cc foo.c
to compile the program foo.c and no such file exists, the compiler
simply exits.
 Killed by another process

15
4. Process Description
We can think of the OS as that entity that manages the use of
system resources by processes.

Processes and Resources

16
Process Description(cont’d)
What information does the OS need to control
processes and manage resources for them?
 Control Tables
 If the operating system is to manage processes and
resources, it must have information about the current status
of each process and resources
 The operating system constructs and maintains tables of
information, called control tables.
 There are four major control tables
 Memory tables
 I/O tables
 File tables
 Process tables

17
Process Description(cont’d)
Memory tables
 Are used to keep track of both main and secondary (virtual)
memory
 Some of main memory is reserved for use by the OS; the
remainder is available for use by processes.
 The memory tables must include the following information:
 Allocation of main memory to processes
 Allocation of secondary memory to processes
 Any information needed to manage virtual memory

I/O tables
 Used by the OS to manage the I/O devices.
 If an I/O operation is in progress, the OS needs to know the
status of the I/O operation and the location in main memory
being used as the source or destination of the I/O transfer.
18
Process Description(cont’d)
File tables
 These tables provide information about the existence of files,
their location on secondary memory, their current status, and
other attributes.

Process tables
 Are used to manage processes
 OS must know the location of each process and the attributes
of each process(process ID and process state)

The tables must be linked or cross-referenced in


some fashion.
The operating system must have some knowledge of
the basic environment, such as how much such as
how much main memory exists, what are the I/O
devices and what are their identifiers, and so on.
19
Process Description(cont’d)

General Structure of Operating System Control Tables


20
Process Control Block
Each process is represented in the OS by process
control block(PCB).
The PCB simply serves as the repository for any
information that may vary from process to process.
Information associated with each process.
 Process state
 Program counter
 CPU registers
 CPU scheduling information
 Memory-management information
 Accounting information
 I/O status information

21
Process Control Block(cont’d)
Process state. The state may be new, ready running,
waiting, halted, and so on.
Program counter
 The counter indicates the address of the next instruction to be
executed for this process.

CPU registers
 The registers vary in number and type, depending on the
computer architecture. They include accumulators, index
registers, stack pointers, and general-purpose registers, plus
any condition-code information. Along with the program
counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly
afterward

22
Process Control Block(cont’d)
CPU-scheduling information
 This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.

Memory-management information
 This information may include such information as the value of
the base and limit registers, the page tables, or the segment
tables, depending on the memory system used by the
operating system.

Accounting information
 This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers,
and so on.

I/O status information


 This information includes the list of I/O devices allocated to
the process, a list of open files, and so on. 23
5. Process Switching
Process switch occurs when a running process is
interrupted and the operating system assigns another
process to the running state and turns control over to
that process
Process switch is sometimes called context switch
A running process may be interrupted by
 Interrupt: An event that occurs outside the process and that is
recognized by the processor (clock interrupt, I/O interrupt)
 Supervisor Call: A procedure call used by user programs to
call an operating system function.
 Trap: An error or exception condition generated within the
currently running process (illegal file access attempt)

24
Process Switching…

25
The Thread Concept

26
Objectives
After completing this topic you should be able to:
 Define thread
 Differentiate process and thread
 Understand thread management

27
Processes and Threads
The discussion so far has presented the concept of a
process as embodying two characteristics:
 A. Resource ownership: A process includes a virtual address
space to hold the process image; collection of program, data,
stack, and attributes defined in the process control block.
 The OS performs a protection function to prevent unwanted
interference between processes with respect to resources.
 B. Scheduling/execution: a process has an execution state
(Running, Ready, etc.) and a dispatching priority and is the
entity that is scheduled and dispatched by the OS.
 These two characteristics are independent and could be
treated independently by the OS.

28
Processes and Threads(cont’d)
The unit of dispatching is usually referred to as a
thread or lightweight process, while the unit of
resource ownership is usually still referred to as a
process or task.
A process is a collection of one or more threads and
associated system resources.
Processes are used to group resources together;
threads are the entities scheduled for execution on
the CPU.
Traditional operating systems are single-threaded
systems
Modern operating systems are multithreaded systems
29
Multithreading
 Multithreading is a technique in which a process, executing an
application, is divided into threads that can run concurrently.
 Handling several independent tasks of an application
 Having great control over the modularity of the application and the
timing of application related events.
 Multithreading can make your program more responsive and
interactive, as well as enhance performance.
 For example,
 A good word processor lets you print or save a file while you are typing.
 When downloading a large file, we can put multiple threads to work—one
to download the clip, and another to play it.

 The ability of an OS to support multiple, concurrent paths of


execution within a single process.
 A single thread of execution per process is referred to as a
single-threaded approach. E.g. MS-DOS
 All threads share the same address space and a separate thread
table is needed to manage the threads. 30
Single and Multithreaded Processes

31
Shared and Private Items

A B

A) Items shared by all threads in a process


B) Items private to each thread
32
Benefits of Threads
The key benefits of threads derive from the
performance implications:
 It takes far less time to create a new thread in an existing
process than to create a brand-new process.
 It takes less time to terminate a thread than a process.
 It takes less time to switch between two threads within the
same process than to switch between processes.
 Threads enhance efficiency in communication between
different executing programs.
 Threads within the same process share memory and files, they can
communicate with each other without invoking the kernel.

33
User-Level and Kernel-Level Threads
Threads can be managed in the kernel or user space
 Kernel Space
 The operating system is aware of the existence of multiple threads
 When a thread blocks, the operating system chooses the next one
to run, either from the same process or a different one
 The kernel has a thread table
 Thread management is slow
 User Space
 The operating system is not aware of the existence of multiple
threads
 The user space has a thread table
 Thread management is much faster (specially switching)
 When one thread blocks, the kernel blocks the entire process

34
User-Level and Kernel-Level Threads(cont’d)
Advantage of ULT
 Allow each process to have its own customized scheduling
algorithm.
 Can be implemented on an operating system that does not
support threads.

Disadvantages of ULTs
 when a thread is blocked, all of the threads within that process
are blocked
 cannot take advantage of multiprocessing
 kernel assigns one process to only one processor at a time

Both systems are in use and various hybrid schemes


have been proposed as well

35
User-Level and Kernel-Level Threads(cont’d)

User-Level and Kernel-Level Threads

36
Inter-process Communication
(IPC)

37
Objectives
After completing this topic you should be able to:
 Define concurrency
 Understand problems associated with concurrency.
 Understand different techniques used for solving issues
related with concurrency.

38
Central theme of OS design
Multiprogramming
 management of multiple processes within a uniprocessor
system.

Multiprocessing
 management of multiple processes within a multiprocessor
system.

Distributed processing
 management of multiple processes executing on multiple,
distributed computer systems.

The most fundamental task of modern operating


systems is management of multiple processes within
uniprocessor, multiprocessor or distributed computer
systems.
39
Concurrency
The fundamental design issue in the management of
multiple processes is concurrency: simultaneous
execution of multiple processes.
Concurrency arises in three different contexts
 Multiple applications: concurrently running applications
 Structured applications: an application structured as a set of
concurrent processes (threads)
 OS structure: OS implemented as a set of processes or
threads.

40
Inter-process Communication
Concurrency provides major benefits in processing
efficiency and in program structuring.
There is frequent interaction among concurrently
running processes.
There are three issues here:
 The first is how one process can pass information to another.
 The second has to do with making sure two or more processes
do not get into each other's way when engaging in critical
activities (suppose two processes each try to grab the last 1
MB of memory).
 The third concerns proper sequencing when dependencies are
present: if process A produces data and process B prints it, B
has to wait until A has produced some data before starting to
print.
41
Inter-process Communication(cont’d)
There are three ways in which concurrent processes
interact with each other:
 Competition for resources
 Cooperation by Sharing Resources
 Cooperation by Communication

Competition for resources


 occurs when independent processes that are not intended to
work together compete for the use of the same or shared
resource, e.g. printer, memory, or file
 There is no exchange of information between competing
processes
 Processes are unaware of each other.
 For example, two independent applications may both want to
access the same disk or file or printer.
42
Inter-process Communication(cont’d)
Cooperation by Sharing Resources
 It occurs when processes that are not necessarily aware of
each other use and update shared data without reference to
other processes but know that other processes may have
access to the same data.
 E.g.
 multiple processes may have access to shared variables or to
shared files or databases. Processes may use and update the
shared data without reference to other processes but know that
other processes may have access to the same data.
 Thus the processes must cooperate to ensure that the data they
share are properly managed.
 Processes are aware of each other indirectly.

Cooperation by Communication
 It occurs when various processes communicate with each
other, for instance with message passing in order to provide a
way to synchronize or coordinate their various activities.
 Processes are aware of each other directly. 43
Concurrency Problems
Race Conditions
 A situation that occurs when two or more processes are
reading or writing into some shared data and the final result
depends on who runs precisely when.
 E.g. Printer Spooler: when a process wants to print a file, it
enters the file name in a special spooler directory. Assume
that the spooler directory has a large number of slots,
numbered 0,1,2,…
 There are two globally shared variables
 Outfile: points to the next file to be printed

 Infile: points to the next free slot in the directory


 There were some files in the spooler directory and assume the
current value of infile is 7 and that of outfile is 4

44
Concurrency Problems(cont’d)
 Assume that simultaneously process A and process B decide
they want to queue a file for printing
 Process A reads infile and stores the value 7 in its local variable
(x=infile)
 An interrupt occurs and the CPU decides that process A has run
long enough so it switches to process B
 Process B reads infile and stores the value 7 in its local variable
(y=infile)
 Process B stores the name of its file in slot 7 and adjusts infile to
be 8
 Eventually process A runs again, it checks x and finds 7 there, and
writes its file name in slot 7, erasing the name process B just put
there, it updates infile to be 8
 The printer will now print the file of process A, process B file will
never get any output

45
Concurrency Problems(cont’d)

Two processes want to access shared memory at same time

46
Concurrency Problems(cont’d)
Deadlock
 It is the permanent blocking of a set of processes compete for
system resources or communicate with each other.
 It involves conflicting needs for resources by two or more
processes.
 It refers to a situation in which a set of two or more processes
are waiting for other members of the set to complete an
operation in order to proceed, but none of the members is able
to proceed.
 E.g. Traffic deadlock
 It is a difficult phenomenon to anticipate and there are no easy
general solutions to this problem

47
Concurrency Problems(cont’d)
Example:
 Consider two processes, P1 and P2, and two resources, R1
and R2.
 Suppose that each process needs access to both resources to
perform part of its function.
 Then it is possible to have the following situation: the OS
assigns R1 to P2, and R2 to P1.
 Each process is waiting for one of the two resources.
 Neither will release the resource that it already owns until it
has acquired the other resource and performed the function
requiring both resources.
 The two processes are deadlocked.

48
Concurrency Problems(cont’d)

Bridge Crossing Example

49
Concurrency Problems(cont’d)
Starvation
 It referees to the situation in which a process is ready to
execute but is continuously denied access to a processor in
deference to other processes.
 E.g. suppose that there are three processes P1, P2, and P3
and each require periodic access to resource R. If the
operating system grants the resource to P1 and P2 alternately,
P3 may indefinitely be denied access to the resource, thus
starvation may occur.
 In large part, it is a scheduling issue

50
Mutual Execution
The key to preventing race condition is to enforce
mutual exclusion.
It is the ability to exclude (prohibit) all other processes
from using a shared variable or file while one process
is using it.
Part of a program where shared resource (critical
resource) is accessed is called critical region or
critical section.

51
Mutual Execution(cont’d)

52
Mutual Execution(cont’d)

Mutual exclusion using critical regions

53
Mutual Execution(cont’d)
 Four conditions to provide mutual exclusion
 No two processes simultaneously in critical region
 No assumptions made about speeds or numbers of CPUs
 No process running outside its critical region may block another
process
 No process must wait forever to enter its critical region.
 Implementing Mutual Execution with Busy Waiting
 while one process is busy updating shared memory in its critical
region, no other process will enter its critical region and cause
trouble.
 Types:
 Disabling Interrupts
 Lock Variables
 Strict Alternation
 Peterson’s Solution
 TSL Instruction
54
Disabling Interrupts
An interrupt is an event that alters the sequence in
which a process executes instruction
In this technique, each process disables all interrupts
just after entering its critical section and re-enables
them just before leaving it.
With interrupts turned off the processor will not be
switched to another process

55
Disabling Interrupts(cont’d)
This approach is generally unattractive because it is
unwise to give user processes the power to turn off
interrupts.
 For instance, a process can turn off interrupts but never turn
them on again in which the whole system freezes.
 If the system is multiprocessor, disabling interrupts affects only
the processor that executed the disable instruction. The other
ones will continue running and can access the shared
memory.

Disabling interrupts is often a useful technique within


the operating system itself.
But, is not appropriate as a general mutual exclusion
mechanism for user processes.
56
Lock Variables
 It is a software solution.
 Assume we have a single shared (lock) variable initially set to 0
 A process enters its critical section if this variable is 0, when it
enters it sets it to 1
 If the lock is already 1, the process waits until it becomes 0
 Thus a 0 means that no process is in its critical region, and a 1
means that some process is in its critical region
 Disadvantages
 Suppose that one process reads the lock and sees that it is 0.
 Before it can set the lock to 1, another process is scheduled, runs and
sets the lock to 1.
 When the first process runs again, it will also set the lock to 1 and the
two processes will be in their critical region at the same time causing
race condition.
57
Strict Alternation
Strict alternation is shown in the program fragment for
two processes, process 0 and process 1.
This solution requires that the two processes strictly
alternate in entering their critical regions.
The integer turn, initially 0, keeps track of whose turn it is
to enter the critical region and examines or updates the
shared memory.
Initially, process 0 inspects turn, finds it to be 0, and
enters its critical region. Process 1 also finds to be 0 and
therefore sits in a tight loop continually testing turn to see
when it becomes 1. When process 0 leaves the critical
region, it sets turn to 1, to allow process 1 to enter its
critical region.
58
Strict Alternation(cont’d)
While (TRUE) {
While(turn!=0) /*wait*/;
Critical_region();
turn=1;
Noncritical_region();
}
(a) Process 0

A proposed solution to the critical region problem

59
Strict Alternation(cont’d)
While (TRUE) {
While(turn!=1) /*wait*/;
Critical_region();
turn=0;
Noncritical_region();
}
(b) Process 1

60
Strict Alternation(cont’d)
Continuously testing a variable waiting for some value
to appear is called busy waiting. This technique
wastes processor time
Disadvantages
 Taking turns is not a good idea when one of the processes is
much slower than the other. This situation violates condition 3
of implementing mutual exclusions: process 0 is being blocked
by a process not in its critical region.

61
Peterson’s Solution
It combines the idea of taking turns with the idea of
lock variables.
It does not require strict alternation.
Initially, neither process is in its critical section.
 Now process 0 calls enter_region.
 It indicates its interest by setting its array element & sets turn
to 0.
 Since process 1 is not interested, enter_region returns
immediately.
 If process 1 now calls enter_region, it will hang there until
interested[0] goes to false, an event that only happens when
process 0 calls leave_region to exit the critical region.

62
Peterson’s Solution(cont’d)
 Now consider the case that both processes call
enter_region almost simultaneously.
 Both will store their process number in turn.
 Whichever store is done last is the one that counts; the first one is
lost.
 Suppose that process 1 stores last, so turn is 1.
 When both processes come to the while statement, process 0
executes it zero times and enters its critical section. Process 1 loops
and does not enter its critical section.

63
Peterson’s Solution(cont’d)
Now consider the case that both processes call
enter_region almost simultaneously
 Both will store their process number in turn.
 Whichever store is done last is the one that counts; the first
one is lost.
 Suppose that process 1 stores last, so turn is 1.
 When both processes come to the while statement, process 0
executes it zero times and enters its critical section. Process 1
loops and does not enter its critical section.

64
Peterson’s Solution(cont’d)

Peterson's solution for achieving mutual exclusion 65


TSL Instruction
 This technique requires a little help from the hardware. It
uses the hardware instruction TSL.
 TSL (Test and Set Lock) is an indivisible atomic
instruction that copies the content of a memory location
into a register and stores a non-zero value at the
memory location.
 The operation of reading the word and storing into it are
guaranteed to be indivisible, i.e. no other processor can
access the memory word until the instruction is finished.
The processor executing the TSL instruction locks the
memory bus to prohibit other processors from accessing
the memory until it is done.

66
TSL Instruction(cont’d)
 To implement mutual exclusion with TSL instruction, we
use a shared variable, lock, to coordinate access to
shared memory. When lock is 0 the process may set it to 1
using TSL instruction and executes its critical section.
When it is done, the process sets lock into 0.
 Before entering its critical region, a process calls
enter_region, which does busy waiting until the lock is
free; then it acquires the lock and returns. After the critical
region the process calls leave_region, which stores a 0 in
lock.

67
TSL Instruction(cont’d)

Entering and leaving a critical region using the


TSL instruction
68
ME Without busy waiting
Both Peterson’s solution and the solution using TSL
are correct. But both have the defect of requiring busy
waiting which wastes CPU time and which can also
have unexpected effects, like the priority inversion
problem.
Priority inversion problem:
 consider a computer with two processes, H with high priority
and L with low priority.
 The scheduling rules are such that H runs whenever it is in
the ready state.
 At a certain moment, with L in its critical region, H becomes
ready to run. H now begins busy waiting, but since L is never
scheduled while H is running, L never gets the chance to leave
its critical region, so H loops.
69
ME Without Busy Waiting(cont’d)
Now let us look at some inter-process communication
primitives that block instead of wasting CPU time
when they are not allowed to enter their critical
regions.
 Sleep: It is a system call that causes the caller to block, i.e. be
suspended until another process wakes it up.
 Wakeup: It is a system call that causes the process specified
by the parameter to wake up.

As an example of how these primitives can be used


let us consider the producer-consumer problem (also
known as the bounded buffer problem)

70
Producer-Consumer Problem
Two processes share a common fixed-size buffer.
One of, the producers, puts information in the buffer,
and the other one, the consumer, takes it out.
When the producer wants to put a new item in the
buffer, it checks the buffer, if it is full, it goes to sleep,
to be awakened when the consumer has removed
one or more items.
When the consumer wants to remove an item from
the buffer and sees that the buffer is empty, it goes to
sleep until the producer puts something in the buffer
and wakes it up.
Let us see the producer-consumer problem using C
programming. 71
Producer-Consumer Problem(cont’d)

Producer-consumer problem with fatal race condition


72
Producer-Consumer Problem(cont’d)
Problem:
 Race condition can occur because access to count is
unconstrained.
 Consider the following situation.
 The buffer is empty and the consumer has just read count to see if
it is 0.
 At that instant, the scheduler decides to stop running the
consumer temporarily and start running the producer.
 The producer enters an item in the buffer, increments count, and
notices that it is now 1.
 Reasoning the count was just 0, and thus the consumer must be
sleeping, and the producer calls wakeup to wake the consumer up.
 Unfortunately, the consumer is not yet logically asleep, so the
wakeup signal is lost.

73
Producer-Consumer Problem(cont’d)
 When the consumer next runs, it will test the value of count it
previously read, find it to be 0, and go to sleep.
 Sooner or later the producer will fill up the buffer and also go to
sleep. Both will sleep forever.
 The problem arises because the wakeup signal is lost.
 A quick fix is to add to the rules by adding wakeup-waiting bit.
 When a wakeup is sent to a process that is still awake, this bit is
set.
 Later, when the process tries to go to sleep, if the wakeup-waiting
bit is on, it will be turned off, but the process will stay awake.
 The wakeup waiting bit cannot be a general solution,
especially for any random number of processes.

74
Semaphores
Semaphores solve the lost-wakeup problem
A semaphore is a new integer variable type that
counts the number of wakeups saved for future use.
A semaphore could have the value 0, indicating that
no wakeups were saved or some positive value if one
or more wakeups were pending.
Two operations were proposed to implement
semaphores: up and down (which are generalizations
of sleep and wakeup, respectively).
 DOWN operation
 It checks the value of the semaphore to see if the value is greater
than 0. If so it decrements the value and just continues. If the
value is 0, the process is put to sleep without completing the
DOWN operation for the moment. 75
Semaphores(cont’d)
 Checking the value, changing it and going to sleep is all done
as a single, indivisible atomic operation. Once a semaphore
operation has started, no other process can access the
semaphore until the operation has completed or blocked.
 This atomicity is absolutely essential to solving
synchronization problems and avoiding race conditions.
 UP operation
 It increments the value of the semaphore
 The process of incrementing the semaphore and waking up one
process is also indivisible.

Solving the Producer – Consumer problem using


semaphores

76
Semaphores(cont’d)
The solution uses three semaphores:
 full, to count the number of full slots,
 empty, to count the number of empty slots and
 mutex, to make sure the producer and the consumer do not
access the buffer at the same time

Mutex is used for mutual exclusion


 It is designed to guarantee that only one process at a time will
be reading or writing the buffer and the associated variables.

Full/empty are used for synchronization


 ensure that the producer stops running when the buffer is full,
and the consumer stops running when it is empty.
They are designed to guarantee that certain event sequences do
or do not occur

77
Semaphores(cont’d)

The producer-consumer problem using semaphores


78
Semaphores(cont’d)
The producer stops running when the buffer is full,
and the consumer stops running when it is empty.
Semaphores that are initialized to 1 and are used by
two or more processes to ensure that only one of
them can enter its critical region at the same time are
called binary semaphores.
Problems
 Semaphores are too low-level and error prone. If you are not
careful when using them, errors like race condition, deadlocks
and other forms of unpredictable and irreproducible behavior
can occur.

79
Semaphores(cont’d)
 Suppose that the two downs in the producer’s code were
interchanged or reversed in order and suppose also the buffer
was full and mutex is 1
down (&mutex);
down (&empty);
 The producer does a down on mutex and mutex becomes 0
and then the producer does a down on empty.
 The producer would block since the buffer is full.
 Next time the consumer does a down on mutex and it blocks
since mutex is 0.
 Therefore both processes would block forever and hence deadlock
would occur.

80
Monitors
A monitor is a higher level of synchronization primitive
proposed by Hoare and Branch Hansen to make
writing a correct program easier.
A monitor is a collection of procedures, variables and
data structures that are all grouped together in a
special kind of module or package.
Rules associated with monitors
 Processes may call the procedures in a monitor whenever
they want to, but they can not directly access the monitor’s
internal data structures
 Only one procedure can be active in a monitor at any instant.
Monitors are programming language construct, so the compiler
knows that they are special and can handle calls to a monitor
procedures differently from other procedure calls
81
Monitors(cont’d)
 It is the compiler that implements mutual exclusion. The
person writing the monitor does not have to be aware of how
the compiler arranges for mutual exclusion. It is sufficient to
know that by turning all critical regions into monitor procedure,
no two processes will ever execute their critical regions at the
same time.

Monitors use condition variables, along with two


operations on them, WAIT and SIGNAL to block and
wake up a process.
 WAIT
 When a monitor procedure discovers that it can not continue(e.g.,
the producer finds the buffer full), it does a WAIT on some
condition variable that causes the calling procedure to block.
 It allows another process that had been previously prohibited from
entering the monitor to enter now.
82
Monitors(cont’d)
 SIGNAL
 A process can wake up its sleeping partner by doing a SIGNAL on
the condition variable that its partner is waiting on
 A process doing a SIGNAL statement must exit the monitor
immediately, i.e. SIGNAL will be the final statement in a monitor
procedure. This avoids having two active processes in a monitor at
the same time.
 If a SIGNAL is done on a condition variable on which several
processes are waiting, only one of them, determined by the
system scheduler is revived.

83
The producer-consumer problem using Monitor
84
Classical IPC Problems
Three better-known problems:
 Dinning-philosopher problem
 The Readers and Writers problem
 The sleeping barber problem

Dinning-philosopher problem
 Five philosophers are seated around a circular table.
 Each philosopher has a plate of spaghetti.
 Between each pair of plates is one fork.
 The life of a philosopher consists of alternate periods of eating and
thinking.

85
Dinning-philosopher problem

86
Dinning-philosopher problem(cont’d)
The procedure: take_fork waits until the specified fork
is available and then seizes it.
Problem:
 Suppose that all five philosophers take their left forks
simultaneously. None will be able to take their right forks, and
their will be deadlock.

87
Dinning-philosopher problem(cont’d)

A nonsolution to the dinning philosophers problem

88
Dinning-philosopher problem(cont’d)
Solution to dining philosophers problem
 After taking the left fork, the program checks to see if the right
fork is available.
 If it is not, the philosopher puts down the left one, waits for
some time, and then repeats the whole process.
 Still some problem. If all the philosophers could start the
algorithm simultaneously
 Picking up their right forks were not available, putting down their
left forks, waiting, picking up their left forks again simultaneously,
and so on, forever.
 This situation is called starvation.
 Just wait a random time instead of the same time. But, this
does not always works.

89
Dinning-philosopher problem(cont’d)

Solution to dining philosophers problem (part 1) 90


Dinning-philosopher problem(cont’d)

Solution to dining philosophers problem (part 2) 91


The Readers and Writers Problems
The dining philosopher problem is useful for modeling
process that are competing for exclusive access to a
limited number of resources, such as I/O devices.
The readers and writers problem, models access to
database.

92
The Readers and Writers Problems(cont’d)

A solution to the readers and writers problem


93
The Sleeping Barber Problem
The barber shop has one barber, one barber chair,
and n chairs waiting for customers.
If there are no customers present, the barber sits
down in the barber chair and falls asleep.
When a customer arrives, he has to wakeup the
sleeping barber.
If additional customers arrive, they either sit(if there
are empty chairs) or leave the shop(if all chairs are
full)
The problem is to program the barber and the
customers without getting into race conditions.

94
The Sleeping Barber Problem(cont’d)

The Sleeping Barber Problem


95
The Sleeping Barber Problem(cont’d)

Solution to sleeping barber problem.


96
Scheduling

97
Objectives
To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems.
To describe various CPU-scheduling algorithms.
To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system.

98
Scheduling
 Scheduling refers to a set of policies and mechanisms to
control the order of work to be performed by a computer
system.
 Of all the resources of a computer system that are
scheduled before use, the CPU/processor is the far most
important.
 Processor Scheduling is the means by which operating
systems allocate processor time for processes.
 It can be implemented on uniprocessor systems,
multiprocessor systems, and real-time systems.
 Scheduler: part of CPU which choice a process to be
executed when two process are simultaneously in ready
state.
 The algorithm it uses is called scheduling algorithm.
99
Scheduling(cont’d)
Process Behavior

Bursts of CPU usage alternate with periods of I/O


wait
 a CPU-bound process
 an I/O bound process

100
Scheduling(cont’d)
The operating system makes three types of
scheduling decisions with respect to execution of
processes:
Long-term Scheduling
 The decision to add to the pool of processes to be executed
 It determines when new processes are admitted to the system
 It is performed when a new process is created and admitted
 New  Ready
 Blocked/Suspend  Ready/Suspend

Medium-term Scheduling
 The decision to add to the number of processes that are
partially/fully in main memory
 It determines when a program is brought partially or fully into
main memory so that it may be executed 101
Scheduling(cont’d)
 It is performed when swapping is done
 Ready/Suspend  Ready
 Blocked/Suspend  Blocked(Read the situation from William
Stalling 6th ed., PP119)
Short-term Scheduling
 The decision of which ready process to execute next
 It determines which ready process will get processor time next
 It is performed when a ready process is allocated the
processor (when it is dispatched)
 Ready  Running

102
Scheduling(cont’d)

Scheduling and Process State Transitions


103
Scheduling(cont’d)
 Part of the operating system that makes scheduling decision is
called scheduler and the algorithm it uses is called scheduling
algorithm.
 In the case of short term scheduling, the module that gives
control of CPU to the process selected by the scheduler is
called dispatcher. Its functions are
 Process switch(Context Switch)
 Mode Switch: Kernel  User
 Control branching: to the proper location in the user program

There are some requirements that should be met by


short-term schedulers/scheduling algorithms.
 user-oriented, which relate to the behavior of the system as
perceived by the individual user or process. An example is
response time in an interactive system.
 system-oriented, which focus on effective and efficient
utilization of the processor. 104
Scheduling(cont’d)
Goal:
 Fairness – giving each process a fair share of the CPU.
 Different categories of process may be treated very differently.
 Policy enforcement – seeing that stated policy is carried out.
 Keeping all parts of the system busy when possible.

Scheduling criteria:
 Efficiency/CPU Utilization: The percentage of time that the
CPU is busy should be maximized.
 Throughput: The number of processes completed per unit
time should be maximized.
 Fairness: Each process should get its fair share of the CPU.
No process should suffer starvation

105
Scheduling(cont’d)
 Turn Around Time: The interval from the time of submission
of a process to the time of completion.
 The sum of the periods spent waiting to get into memory,
waiting in the ready queue, executing on the CPU, and
doing I/0.
 Waiting time: is the sum of the periods spent waiting in the
ready queue.
 Response Time: is the time from the submission of a request
until the first response is produced.
 For interactive users, the time from submission of request until
the response begins to be received should be minimized
It is desirable to maximize CPU utilization and
throughput and to minimize turnaround time, waiting
time, and response time.

106
Scheduling(cont’d)
There are two main characteristics of short-term
scheduling algorithms
 Selection function
 It determines which process, among ready processes, is selected
for execution.

 It may be based on
 Priority
 Resource requirement
 Execution behavior: time spent in system so far (waiting and executing),
time spent in execution so far, total service time required by the process

107
Scheduling(cont’d)
 Decision mode
 It specifies the instants in time at which the selection function is
exercised

 There are two types of decision modes


 Non-Preemptive
 Run to completion method: once a process is in running state, it
continues to execute until it terminates or blocks itself to wait for
some event
 Simple and easy to implement
 Used in early batch systems
 It may be well reasonable for some dedicated systems
 Efficiency can be attained but response time is very high

108
Scheduling(cont’d)

 Preemptive
 The strategy of allowing processes that are logically runnable to
be temporarily suspended and be moved to the ready state.
 Events that may result pre-emption are arrival of new processes,
occurrence of an interrupt that moves blocked process to ready
state and clock interrupt
 Suitable for general purpose systems with multiple users
 Guarantees acceptable response time and fairness
 Context switching is an overhead

109
First-Come-First-Served Scheduling
First-Come-First-Served Scheduling (FCFSS)
Basic Concept
 The process that requested the CPU first is allocated the CPU
and keeps it until it released it.
 The process that has been in the ready queue the longest is
selected for running.
 Its selection function is waiting time and it uses non
preemptive scheduling/decision mode.

110
FCFSS(cont’d)
Illustration
 Consider the following processes arrive at time 0

Process P1 P2 P3
CPU Burst/Service Time (in ms) 24 3 3
Case i. If they arrive in the order of P1, P2, P3
Process P1 P2 P3
Service Time (Ts) 24 3 3
Turn around time (Tr) 24 27 30
Response time 0 24 27
Tr/Ts 1 9 10
Average response time = (0+24+27)/3 = 17
Average turn around time = (24+27+30)/3=27
Throughput = 3/30= 1/10
111
FCFSS(cont’d)
Case ii. If they arrive in the order of P3, P2, P1
Process P3 P2 P1
Service Time (Ts) 3 3 24
Turn around time (Tr) 3 6 30
Response time 0 3 6
Tr/Ts 1 2 1.25
Average response time = (0+3+6)/3 = 3
Average turn around time = (3+6+30)/3=13
Throughput = 3/30= 1/10

112
FCFSS(cont’d)
Consider the following processes arrive at time 0, 1,
2, 3 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 1 2 3
Service Time (Ts) 1 100 1 100
Turn around time (Tr) 1 100 100 199
Response time 0 0 99 99
Tr/Ts 1 1 100 1.99
Average response time = (0+0+99+99)/4 = 49.5
Average turn around time = (1+100+100+199)/4=100
Throughput = 4/202

113
FCFSS(cont’d)
Exercise
1. Consider the following processes arrive at time 0, 3,
8, 11 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 3 8 11
Service Time (Ts) 3 5 3 100
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Tr/Ts ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput =?

114
FCFSS(cont’d)
Process P1 P2 P3 P4
Arrival Time (Ta) 0 3 8 11
Service Time (Ts) 3 5 3 100
Turn around time (Tr) 3 5 3 100
Response time 0 0 0 0
Tr/Ts 1 1 1 1
Average response time = (0+0+0+0)/4 = 0
Average turn around time = (3+ 5+3+100)/4=27.75
Throughput = 4/111

115
FCFSS(cont’d)
2. Consider the following processes arrive at time 0, 2,
3, 4 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 2 3 4
Service Time (Ts) 4 16 50 1
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Tr/Ts ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput = ?

116
FCFSS(cont’d)
Consider the following processes arrive at time 0, 2,
3, 4 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 2 3 4
Service Time (Ts) 4 16 50 1
Turn around time (Tr) 0 18 67 67
Response time 0 2 17 66
Tr/Ts 0 1.33 1.34 67
Average response time = (0+2+17+66)/4 = 20.5
Average turn around time = (0+ 18+67+67)/4=38
Throughput = 4/71

117
FCFSS(cont’d)
Advantages
 It is the simplest of all non-preemptive scheduling algorithms:
process selection & maintenance of the queue is simple.
 There is a minimum overhead and no starvation.
 It is often combined with priority scheduling to provide
efficiency.

Drawbacks
 Poor CPU and I/O utilization: CPU will be idle when a process
is blocked for some I/O operation.
 Poor and unpredictable performance: it depends on the arrival
of processes.
 Unfair CPU allocation: If a big process is executing, all other
processes will be forced to wait for a long time until the
process releases the CPU. It performs much better for long
processes than short ones.
118
Shortest Job First Scheduling
Shortest Job First Scheduling (SJFS)
Basic Concept
 Process with the shortest expected processing time (CPU
burst) is selected next.
 Its selection function is execution time and it uses non
preemptive scheduling/decision mode.

SJF scheduling is used frequently in long-term


scheduling.

119
SJFS
Illustration
 Consider the following processes arrive at time 0
Process P1 P2 P3
CPU Burst (in ms) 24 3 3
 Case i. FCFSS
Process P1 P2 P3
Turn around time 24 27 30
Response time 0 24 27
Average response time = (0+24+27)/3 = 17
Average turn around time = (24+27+30)/3=27
Throughput = 3/30

120
SJFS(cont’d)
 Case ii. SJFS

Process P3 P2 P1

Turn around time 3 6 30

Response time 0 3 6

Average response time = (0+3+6)/3 = 3

Average turn around time = (3+6+30)/3=13

Throughput = 3/30

121
SJFS(cont’d)
Consider the following processes arrive at time 0,
2, 4, 6, 8 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 4 6 8
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 3 7 11 14 3
Response time 0 1 7 9 1
Tr/Ts 1 1.17 2.75 2.8 1.5
Average response time = (0+1+7+9+1)/5 = 3.6
Average turn around time = (3+7+11+14+3)/5=7.6
Throughput = 5/20

122
SJFS(cont’d)
Exercise
Consider the following processes arrive at time 0,
2, 3, 7, 10 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 3 8 10
Service Time (Ts) 7 10 5 1 2
Turn around time (Tr) ? ? ? ? ?
Response time ? ? ? ? ?
Tr/Ts ? ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput = ?

123
SJFS(cont’d)
Consider the following processes arrive at time 0,
2, 3, 7, 10 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 3 8 10
Service Time (Ts) 7 10 5 1 2
Turn around time (Tr) 7 23 9 5 5
Response time 0 13 4 4 3
Tr/Ts 1 1.17 2.75 2.8 1.5
Average response time = (0+13+4+4+3)/5 = 4.8
Average turn around time = (7+23+9+5+5)/5=9.8
Throughput = 5/25

124
SJFS(cont’d)
Advantages
 It produces optimal average turn around time and average
response time
 There is a minimum overhead

Drawbacks
 Starvation: some processes may not get the CPU at all as long
as there is a steady supply of shorter processes.
 It is not desirable for a time-sharing or transaction processing
environment because of its lack of processing.
 Variability of response time is increased, especially for longer
processes.

125
Shortest Remaining Time Scheduling
Shortest Remaining Time Scheduling (SRTS)
Basic Concept
 The process that has the shortest expected remaining process
time.
 If a new process arrives with a shorter next CPU burst than
what is left of the currently executing process, the new process
gets the CPU.
 Its selection function is remaining execution time and uses
preemptive decision mode

126
SRTS(cont’d)
Illustration
 Consider the following processes arrive at time 0, 2, 4
Process P1 P2 P3
Arrival Time (Ta) 0 2 4
Service Time (Ts) 3 8 4
Turn around time (Tr) 3 12 4
Response time 0 1 0
Tr/Ts 1 1.5 1
Average response time = (0+1+0)/3= 0.33
Average turn around time = (3+12+4)/3= 19/3
Throughput = 3/14

127
SRTS(cont’d)
Illustration
 Consider the following processes arrive at time 0, 2, 4, 6, 8
respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 4 6 8
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 3 13 4 14 2
Response time 0 1 0 9 0
Tr/Ts 1 2.17 1 2.8 1
Average response time = (0+1+0+9+0)/5 = 2
Average turn around time = (3+13+4+14+2)/5=7.2
Throughput = 5/20

128
SRTS(cont’d)
Advantages
 It gives superior turnaround time performance to SJFS,
because a short job is given immediate preference to a
running longer process

Drawbacks
 There is a risk of starvation of longer processes
 High overhead due to frequent process switch

Difficulty with SJFS


 Figuring out required processing time of each process

129
Round Robin Scheduling
Round Robin Scheduling (RRS)
Basic Concept
 A small amount of time called a quantum or time slice is
defined. According to the quantum, a clock interrupt is
generated at periodic intervals. When the interrupt occurs, the
currently running process is placed in the ready queue, and
the next ready process is selected on a FCFS basis.
 The CPU is allocated to each process for a time interval of
upto one quantum. When a process finishes its quantum it is
added to the ready queue, when it is requesting I/O it is added
to the waiting queue.
 The ready queue is treated as a circular queue
 Its selection function is based on quantum and it uses
preemptive decision mode

130
RRS(cont’d)
Illustration
 Consider the following processes arrive at time 0, 2, 4
respectively; quantum=4
Process P1 P2 P3
Arrival Time (Ta) 0 2 4
Service Time (Ts) 3 7 4
Turn around time (Tr) 3 12 7
Response time 0 1 3
Average response time = (0+1+3)/3
Average turn around time = (3+12+7)/3
Throughput = 3/14

131
RRS(cont’d)
Illustration
 Consider the following processes arrive at time 0, 3, 4, 7
respectively; quantum=4
Process P1 P2 P3 p4
Arrival Time (Ta) 0 3 4 7
Service Time (Ts) 7 5 9 10
Turn around time (Tr) 19 17 25 24
Response time 0 1 4 5
Average response time = (0+1+4+5)/4
Average turn around time =(19+17+25+24)/4
Throughput = 4/31

132
RRS(cont’d)
Features
 The oldest, simplest, fairest and most widely used preemptive
scheduling
 Reduces the penalty that short processes suffer with FCFS

Drawbacks
 CPU-bound processes tend to receive unfair portion of CPU
time, which results in poor performance for I/O bound
processes.
 It makes implicit assumption that all processes are equally
important. It does not take external factors into account
 Maximum overhead due to frequent process switch

133
RRS(cont’d)
Difficulty with RRS
 The length of the quantum should be decided carefully
 E.g.1) quantum =20ms, context switch =5ms, % of context
switch = 5/25 *100=20%
 Poor CPU utilization

 Good interactivity
 E.g.2) quantum =500ms, context switch =5ms, % of context
switch = 5/505 *100<1%
 Improved CPU utilization

 Poor interactivity
 Setting the quantum too short causes
 Poor CPU utilization

 Good interactivity
134
RRS(cont’d)
 Setting the quantum too long causes
 Improved CPU utilization

 Poor interactivity
 A quantum around 100ms is often reasonable compromise
 To enable interactivity, a maximum response time (MR) can be
specified, and the quantum (m) can be computed dynamically
as follows

 m = MR/n , where n is number of processes


 A new m is calculated when the number of processes changes

135
Priority Scheduling
Priority Scheduling (PS)
Basic Concept
 Each process is assigned a priority and the runnable process
with the highest priority is allowed to run i.e. a ready process
with highest priority is given the CPU.
 It is often convenient to group processes into priority classes
and use priority scheduling among the classes but round robin
scheduling within each class

136
PS(cont’d)
Ilustration
 Consider the following processes arrive at time 0
Process P1 P2 P3 P4 P5
Priority 2 4 5 3 1
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 18 10 4 15 20
Response time 15 4 0 10 18
Tr/Ts 6 1.67 1 3 10
Average response time = (0+15+4+10+18)/5 = 9.4
Average turn around time = (18+10+4+15+20)/5=13.4
Throughput = 5/20= 0.25

137
PS(cont’d)
Consider the following priority classes
 Processes type:
Deans Heads Instructors Secretaries Students
 Priority: 5 4 3 2 1

As long as there are runnable processes in priority


level 5, just run each one for one quantum, round
robin fashion, and never bother with lower priority
classes
If priorities are not adjusted occasionally, lower priority
classes may all starve to death

138
PS(cont’d)
Advantages
 It considers the fact that some processes are more important
than others, i.e. it takes external factors into account

Drawbacks
 A high priority process may run indefinitely and it can prevent
all other processes from running. This creates starvation on
other processes. There are two possible solutions for this
problem:

 Assigning a maximum quantum to each process

 Assigning priorities dynamically, i.e. avoid using static priorities

139
Multilevel Queues Scheduling
Multilevel Queues Scheduling (MLQS)
 It is used for situations for which processes can be classified
into different groups
 System processes

 Interactive processes

 Batch processes
 The ready queue is partitioned into several separate queues
 Each processes is assigned to one queue permanently, based
on some property of the process such as memory size,
process priority, process type, etc

140
MLQS(cont’d)
 Each queue has its own scheduling algorithm: RRS, FCFSS,
SJFS, PS etc
 System processes…………… PS

 Interactive processes…………RRS

 Batch processes………………FCFSS
 There must be some kind of scheduling between the queues
 Commonly implemented as fixed-priority preemptive scheduling,
i.e. system processes have absolute priority over the interactive
processes and interactive processes have absolute priority over the
batch processes. Lowest priority processes may be starved.

141
Quiz
Define the following terminologies (5 pts)
A. Throughput
B. Turnaround time
C. Response time

142
Quiz
Q1. (5 pts) Consider the following processes arrive at
time 0, 4, 5, 6 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 4 5 6
Service Time (Ts) 9 3 1 8
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Throughput = ?

Solve using SJF and SRTS

143
Quiz…
Q2. (5 pts) Consider the following processes arrive at
time 0 and quantum value is 5:
Process P1 P2 P3 P4
Service Time (Ts) 13 3 15 7
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Throughput = ?

Solve using RRS

144

You might also like