0% found this document useful (0 votes)
8 views

Learning Module: Surigao State College of Technology

The document discusses process management in operating systems. It covers the definition of a process, process types, process states, and scheduling. Process management is a fundamental functionality of operating systems and involves managing resources for multiple concurrent processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Learning Module: Surigao State College of Technology

The document discusses process management in operating systems. It covers the definition of a process, process types, process states, and scheduling. Process management is a fundamental functionality of operating systems and involves managing resources for multiple concurrent processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Module No. 2
CHAPTER 2: PROCESS MANAGEMENT
In this chapter, we will discuss the following:
2.1 Overview of Process
2.2 Types of processes
2.3 Process states
2.4 Thread Management
2.5 Inter-Process Communication (IPC)
2.6 Scheduling and Dispatch
2.7 Deadlock
Time Frame: 17 hrs
Introduction:
The most central notion in any operating system is a process. A process is a program
currently running and that needs different kinds of resources to be allocated. A process also
interacts with other processes more frequently. Thus, an operating system is responsible to
manage all the requirements of processes and also coordinate communication among
different processes. This unit covers in detail processes, types of processes, the different
states a process be in, the difference between process and thread and the interaction among
different processes.

Traditionally, a computer system executes processes sequentially. There are cases


where we would like the system to divert from this fashion and handle processes execution
differently. Scheduling is a means for deciding which process runs at a given time. It is a
process of selecting one of the processes currently in main memory that are ready to run, and
grant the CPU to one of them. Scheduling is the basis for multi-programmed systems as the
system needs to make choices to grant the CPU to the ready state threads and processes.
This unit elaborates the need for scheduling and the different scheduling algorithm used to
schedule processes effectively. A deadlock situation and its consequences as well as the
solution towards it will also be discussed in this unit.

Objectives
Upon completion of this unit you should be able to:

• Discuss how process is managed


• Explain the concept of a process and its process states
• Illustrate how CPU scheduling is managed
• Illustrate a deadlock situation and how deadlock is managed
• Differentiate a thread from a process
• Describe how the OS handles inter-process communication

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 20


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Learning Activities
Read Me!
2.1 Overview of Processes

❖ One of the fundamental functionalities of operating system is process


management. What is process? What types does it have?
❖ Early computer systems were capable of running only one program at a time and the
program had full control of the systems’ resources and thus no structuring is required.
But today’s computer systems are designed to support multiprogramming, running
more than one program concurrently, besides the operating system they have. This
arrangement requires the system to know which is which so that going back and forth
between the programs is possible and thus enforces the need for structuring.
❖ Process is an instance of a program that is being executed. It is a program that has
been loaded from disk onto memory and is running. While a program is just a passive
set of commands, process is the actual execution of these instructions and one
program can be partitioned to more than one processes. It is the basic unit of execution
in an operating system. A process consumes some amount of assets like CPU time,
main memory, and I/O devices to carry out its operations. It is the duty of the operating
system to manage all these requirements of processes and bring them to termination.
❖ A process is the basic execution unit in an operating system with unique number
assigned as an identifier (PID). Though the CPU is running only one program at a time,
it may also work on multiple programs by switching and running them for tens or
hundreds of milliseconds. This in turn requires more control and partitioning of
programs which resulted in a process. Thus, the operating system will have many
processes running at the same time all requiring resources.
A process has three main components namely
address space, processor state and OS
resources.
1. Address space is a memory location that
the process can access. It is how a process sees
its memory. Every process has a separate
address space. An address space encompasses
many parts like the instructions, local variables,
heap, stack, etc. as can be seen in the following
diagram.

From figure 2.1. :


✓ Text Segment /Code Section holds
the executable Code of the program which
is read from secondary storage when the
program is launched
✓ Data Segment maintains static
variables and initialized Global variables
prior to the execution of main

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 21


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

✓ Heap is used for dynamically allocated memory during run time and it is
managed through new, delete, malloc, etc. calls.

✓ Stack Segment holds all local variables and saved registers for each
procedure call. The stack will be freed when the variables go out of scope

2. Processor state: these are CPU registers associated with the running process. The
registers hold the results so far of running the code of the process. It includes general purpose
registers, program counter, stack counter, etc. the information on these registers must be
stored and restored as the processes swap out of memory and later restored.

3. OS resources: the different OS state related with the process. Examples: open files,
network sockets, etc.

➢ The operating system maintains a process table to store these details of each process.

➢ A conceptual model to simplify the management of several parallel processes is being


used while designing an operating system. The design organizes all executable
programs on a system into a number of sequential processes. The CPU has a
multiprogramming capability where it switches between processes back and forth to
manage their execution.
➢ Every process has its own virtual program counter that will be placed onto the actual
physical program counter of the system when the process runs as shown in figure 2.
below.
➢ And when the process finishes its execution for now, the content of the actual program
counter is maintained in the processes’ logical counter in memory. Processes shall not
be developed with built-in timing assumptions as the amount at which a process carries
out its operations may not be
consistent due to the back and
forth movement of CPU among
processes.

❖ A general purpose operating


system allows for processes to
execute concurrently and

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 22


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

needs a mechanism for creation and termination of processes as needed during


operation to assure all necessary processes exist.
❖ There are three basic situations for a process creation. These are:
1. System initialization
➢ Several processes are created at system boot time. These processes can be
either foreground processes that allow users to accomplish a task or
background processes known as daemons that handles a specific activity for
the system such as web page or print handling and are not associated with a
specific user. The ps program and task manager are used to list processes in
UNIX and Windows operating system respectively.
➢ Running process: executing process creation system calls
➢ Sometimes, a running process requests a system call for creating new
processes to assist it on its jobs if the job can easily be formulated into multiple
inter-related but independent communicating processes.

2. User request
➢ New process can also be created when a user types a command or double
clicks an icon to start a program. If the operating system has a command based
interface, the new process uses the window in which it was created.
➢ On the other hand, if the operating system is graphical, a process do not have
a window when started so it creates one. In both cases, users can have multiple
windows opened simultaneously and they can interact with the processes
through a mouse click.

➢ The other way in which processes can be created is through batch systems
found on mainframe computers. In such cases, users give bunch of tasks to
the computer and if the operating system has the required resources to
entertain another task, it produces the new process requested.

➢ In all the process creation cases discussed, a process creation system call is
executed from an already existing process to create the new one.. The
operating system will be informed by the system call to create the new process
and which program to run in it. The system call used to create new processes
in UNIX is fork(). Fork makes an exact replica of the calling process which
results in parent and child processes. These processes have the same memory
image, environment string and open files. The child process can then execute
the execve system call to create its own memory image and run a new program.

3. Create Process
➢ Is the system call used to create new process in windows operating system.

2.2 Process states

Starting from the execution of process, it passes through different states till its termination.
What are those states? How is the transition from one state to another invoked?

❖ The state of a process changes while the process is running. The state indicates
what the process is currently doing. A process can assume any one of the following
three states.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 23


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

1. Ready: the process is ready to go but is waiting to get granted the CPU as some
other process is using it.
2. Running: the process is actually using the CPU at the moment and is being
executed.
3. Waiting: sleeping or blocked state where the process is waiting for an event.
Even if the CPU is free to handle requests, a process at this state will not use it
as it is waiting for some external events to occur.

Four possible moves can be made by a process between these states as it executes as shown
in figure 2.3 below.
❖ Transition 1 happens when a process has
to wait for some event like I/O and can’t continue
further.
❖ Transition 2 and 3 happens when the
process scheduler decides so without the
knowledge of the process.
❖ In transition 2, the scheduler decides that
the process has run long enough and it is time to
let others use the CPU or an interrupt has
occurred which signals the CPU to stop the
current process and do some other activity.
❖ In transition 3, the scheduler decides the
other processes have enough use of the CPU and
it is this process’s turn to use the CPU again.
❖ The last transition, transition 4, occurs when the process has got what it was waiting
for and jumps immediately to ready state where it is moved to the running state, if the
CPU is idle, or remains in the ready state until the CPU is idle. It is important to note
that several processes can be in ready and waiting state while only one process is
running at any instant of time.
❖ A process’s life cycle comprise of five basic states as new, ready, running, waiting and
terminate phase.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 24


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ The operating system represents each process by a task control block called Process
Control Block (PCB). This block is a big data structure holding different important
information associated with a process as shown in table 2.1. below.
❖ Process state

Process number, PID

Program counter

Registers

Memory limit

List of open files

Table 2.2 PCB Entries per


process
❖ Process state: indicate the current
status of the process as new, running, waiting, ready.
❖ Process number: a unique positive integer value associated with each process during
creation and used as an identifier. The getpid() command is used to return the current
process’s number, PID. In order to read the parent’s id, we can use the getppid()
command.
❖ Program counter: a value indicating the next instruction to be executed for the
process
❖ Registers: including accumulators, stack pointers, and the like maintains the current
value produced when the process runs so that the process resumes from where it left
off in case of interrupts.
❖ Memory limit: holds the value of the base and limit registers, page table size etc.

Assessment:
Instructions: Answer the following. Write your answers in a ½ sheet of paper and
submit it during the face to face schedule.

1. What are the different possible states that a process can be in?
2. When does a process move from running to waiting/blocked/sleeping state?
3. Why is it not possible to move a process from ready to waiting state?
4. What is the reason for a process to stay in its ready state?

2.3 Thread Management

❖ To perform several jobs at a time multiprogramming is a must. A program by itself


cannot address several problems at a time. But, there is a mechanism through which
a program can solve this problem. The answer is thread. What is thread? How does it
work?
❖ A process is a means through which interrelated resources such as the address space,
child processes, opened files, etc. are grouped together.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 25


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Threads are multiple execution streams within a single process and are used to
separate the resource management task of the process from its actual execution.
❖ While processes are used to group resources together, threads are the objects
scheduled for execution on the CPU.
❖ Thread can also be considered as lightweight process in that it does small portion of
the entire task of a process.
❖ Having a thread adds an advantage to a process by enabling multiple independent
executions in the same process environment by sharing the resources grouped
together.
❖ The CPU moves back and forth among the threads running and creates an illusion that
all of the threads are running in parallel.
❖ A process may create several threads (multi-threading) that share same address
space and work together to accomplish a job in a sense they are not as such
independent as two processes are.
❖ Threads are very important in today’s programming to handle execution of several
tasks of a single process independently of the others. This is mainly true when one of
the tasks of the process may block, and it is required to let the remaining tasks run
without blocking.
❖ For instance, in a word processor, a thread running in the background may validate
spelling and grammar while a foreground thread handles user’s input, and yet another
third thread fetches images from the local disk, and even a fourth thread is doing
periodic automatic backups of the file being edited and so on. Like a single thread (one
process), threads can be in any of the states a process can be in which we previously
discussed.
❖ In a multi-thread model, a process starts by creating only one thread first and the thread
creating other threads through a create-thread library procedure call. The newly
created thread runs automatically in the address space of the creating thread and the
identifier of the newly created thread will be passed to the creating thread. The
exit_thread library procedure is used to terminate a thread when it finishes its task. A
thread can also wait for another thread with a call to the library procedure thread_wait.
The CPU has no interrupt towards threads, as with processes, that forces them to
surrender their usage of the CPU. However, threads use the thread_yield procedure
call to willingly give up the CPU for other threads to use. This avoids the monopolized
usage of the system by a single thread.
❖ Multi-threading brings four main advantages to processes. These are:
1. Responsiveness –a thread may give results quickly while other threads are
waiting or slowed down performing some serious computations.
2. Resource sharing - By default threads use common code, data, and other
assets, that enables several jobs to be carried out concurrently in one address
space.
3. Economy – Creating, destroying and handling threads is much faster than
performing the same operation for processes since no resource is attached to
a thread
4. Scalability – A process with one thread can only run on a single CPU,
regardless of the number of CPUs available, while running a process with
several threads may require distributing the threads t amongst available
processors.
❖ Despite these advantages, threads also bring a complication to processes. One
problem is associated with the fork() system call. Assume a parent and child processes
with multi-threading capability. What would happen if a thread in the parent is waiting

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 26


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

for input? Does a thread in the child also get blocked? Who gets a typed input, only
the parent? The child? Or both?
❖ Another problem arose due to the common resources shared among threads. One
thread might close or modify a particular resource which is currently being also used
by another thread.
❖ In general, since the advantage of multi-threading overweighs its problems, modern
computer systems implement the concept of multi-threading but it requires a careful
thought and design.
❖ A system can implement and manage threads through two main ways namely user
thread and kernel thread.
User space thread:
✓ The thread packages are placed in their entirety on the user space. These are the
threads defined by application software developers during application creation with the
kernel having no knowledge about them and it operates as if a single-threaded process
is being managed. This is a good mechanism to implement threads on operating
systems that does not support one.
✓ The thread archive also has codes to be used for threads creation and deletion, for
transferring messages and data amongst threads, for scheduling thread execution and
also for storing and reinstating thread contexts. The application software starts with
one thread and its execution begins in that thread. Each process defines a separate
thread table which is then controlled by a run-time system to maintain information
related to each thread in the process. Whenever a thread would like to move from one
state to another, a run-time system procedure is called that checks the validity of the
change and stores the current values in the thread’s registers into the thread table so
that the thread will resume later on from where it has stopped.
✓ Invoking the run-time system procedures is way effective than performing a kernel call
as no trap and no context switch is needed and also as it does not require to flash the
memory cache making thread scheduling very fast. Such type of thread
implementation also allows processes to have their own customized scheduling
algorithm. Scalability is again another advantage of user space thread implementation
as several threads can be defined and implemented easily. User space thread
implementation has also its own drawbacks.
✓ One problem is in relation to page fault. System can be set not to load a program in its
entirety onto main memory at once. A page fault occurs when a process looks for
instructions not loaded in memory. During such cases, the process is blocked until the
operating system finds and reads the required instruction from disk. If a thread makes
a page fault to happen, the kernel will block the entire process until the missing request
is being processed as it is unaware of the multi-threaded implementation of the process
and deprive other runnable threads from running.
✓ The second problem associated with user space thread implementation is that a single
thread can monopolize the time slice and make starve the other threads within the
process unless it willingly returns the CPU. Since clock interruption is not found within
a single process round-robin scheduling is impossible and also making it impossible
for the scheduler to interrupt unless a thread enters the runtime system of its own free
will. There are cases where absence of clock interrupts is very important like during
synchronization. Especially, it is common in distributed systems for one thread to start
a job that requires reply of another thread and then waits in a busy loop checking if the
response has happened.
✓ This situation is called a spin lock or busy waiting. A spin lock is important especially
when the response is needed immediately and the cost of using semaphores is high.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 27


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

If threads are suspended automatically every few milliseconds using a clock interrupts,
this approach works fine. However, if threads are allowed to execute until they block,
this situation is an input for deadlock.

Kernel space thread

❖ Kernel is aware of available threads and manages the threads. All modern operating
systems provide kernel level threads, enabling the kernel to carry out several
concurrent jobs and/or to run multiple kernel system calls at a time. Rather than
defining separate thread tables in every process, the kernel maintains a single thread
table with similar attributes as the process’s thread table for all the threads found in
the system.
❖ The kernel also has a process table to maintain the existing processes. Whenever a
thread is blocked through system calls, the kernel can run either a ready thread from
same process or another process.
❖ Thread creation and destroy has larger costs in this kernel mode. To minimize this
cost, some systems implement thread recycling. In this case, a thread destroyed will
be deactivated without affecting its data structures and when a new thread is created,
the deactivated thread will
be brought back to life. In
Kernel threads there is no
need to define new non-
blocking system calls and
if a thread has
encountered a page fault,
the kernel will check for
another ready thread in a
process and allow this
thread to run until the
page fault is resolved.
❖ The main difference
between a kernel thread
and a user thread is the
fact that the kernel thread
has no user task memory

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 28


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

mapping. Like other user processes, it shares the kernel address space but the thread
operates only in kernel space with kernel privileges.
❖ Because kernel has full knowledge of all threads, Scheduler may allocate more
execution time to a process with large number of threads than processes having small
number of threads. Kernel-level threads are mainly useful for processes that frequently
block.
❖ However, kernel-level threads are known to be slow and inefficient. Threads operations
in kernel-level are hundreds times slower than that of user-level threads. Since kernel
has to control and schedule threads as well as processes, the kernel needs to keep
information of each thread through a full thread control block. As a result, there is
substantial overhead, substantial cost to system calls and increased complexity in
kernel.

Hybrid thread implementation

❖ Combining the advantages of both the user space thread implementation and the
kernel level thread implementation can be more effective than using them individually.
Different techniques had been explored on how to bring together the benefits of user-
level threads to that of kernel-level threads.
❖ Using kernel level threads and multiplexing user level threads onto all or some of the
kernel threads is one way identified. In this method the kernel knows only about the
kernel level threads, which might have zero or more multiplexed user-space threads
that take turns using it, and handles scheduling of those threads.
❖ Another approach is the so called scheduler activation that imitates the services of
kernel level threads, but with improved efficiency and better flexibility usually related
with thread packages implemented in user space. In this technique, the kernel
allocates some amount of logical processors to every process and allows the user-
space run-time system assign threads to processors.
❖ Initially, the kernel will assign a single virtual processor to each process but processes
can also demand for additional and can also give back if no longer needed. It is also
possible for the kernel to take back an already given virtual processors and re-allocate
them to other, more needy, processes.
❖ The kernel activates the user space run time system through an up call in order to
notify about an event happening on threads. For instance, when the kernel is aware of
a thread just moved to its blocked state, it will immediately notify the run time system
by putting the code of the thread identified along with a report of the event happened
on to the stack.
❖ With this knowledge, the runtime system tries to reschedule the threads according to
their states. A hardware interrupt may occur while a user thread is being executed
where the interrupted virtual CPU moves to a kernel mode and manages the interrupt.
If the interrupt occurred has no significance to the interrupted thread, it will be returned
back in its state before the interruption.
✓ If, on the other hand, the thread has some interest towards the interrupt, it will be
suspended and the runtime system will be up called with the state of this thread passed
onto the stack. The runtime system then decides which thread to grant the CPU for:
the interrupted thread, a new thread which is in its ready state, or some other third
choice.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 29


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Thread Management

✓ Pthreads are Linux implementation of thread management mechanism. Pthreads are


defined as a set of C language programming types and procedure calls. Vendors
usually provide a Pthreads implementation in the form of a header/include file and a
library, which you link with your program. The two names used in thread management
frequently are: pthread_t for thread objects and pthread_attr for thread attributes
objects.
✓ The pthread_create function is used to create a new thread, and pthread_exit function
for terminating a thread by itself. A thread to wait for termination of another thread uses
the function pthread_join.
✓ Normally when a program starts up and becomes a process, it starts with a default
thread which brings the conclusion that every process has at least one thread of
control. A process can create extra threads using the following function :

#include <pthread.h>

int pthread_create(pthread_t *restrict tidp, const


pthread_attr_t *restrict attr, void *(*start_rtn)(void), void
*restrict arg)

Where:

❖ The first argument is a pthread_t type address. Once the function is called
successfully, the variable whose address is passed as first argument will hold the
thread ID of the newly created thread.
❖ The second argument may contain certain attributes which we want the new thread to
contain. It could be priority etc.
❖ The third argument is a function pointer. This is something to keep in mind that each
thread starts with a function and that functions address is passed here as the third
argument so that the kernel knows which function to start the thread from.
❖ As the function may accept some arguments also we can pass these arguments in
form of a pointer to a void type, which is the fourth argument.
Assessment:
Instructions: Answer the following. Write your answers in a ½ sheet of paper and
submit it during the face to face schedule.

1. Define the importance of using threads?


2. Compare and contrast main differences between user space and kernel threads?
3. Determine the disadvantages of kernel level thread implementation?
4. Identify which problems of user space thread implementation are solved by using the
scheduler activation thread implementation method?

2.4 Inter-Process Communication (IPC)

✓ In a multiprogramming environment several processes executes at a time. These


processes compute to control resources. Processes need to communicate to each
other for sharing resources. How does the operating system handle such inter-process
communication?

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 30


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Concurrency and Its Problems

A. Basic Concept
❖ The most fundamental task of modern operating systems is management of
multiple processes within uniprocessor, multiprocessor or distributed computer
systems.
❖ 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
✓ Concurrency provides major benefits in processing efficiency and in program
structuring
B. Inter-process Communication
❖ There is frequent interaction among concurrently running processes. There are three
ways in which concurrent processes interact with each other:
o Competition for Resources
❖ It 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
❖ No communication or message exchange exists amongst such competing processes
❖ Processes are unaware of the existence of each other
o Cooperation by Sharing Resources
❖ It occurs when processes that are not necessarily aware of each other usage and
interaction to shared data without reference to other processes but suspects that other
processes may have access to the same data
❖ Processes need to collaborate and guarantee that the data they share are properly
managed
❖ Processes are aware of the existence 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.
❖ There is nothing shared between processes
❖ Processes are aware of the existence of each other directly
C. Concurrency Problems
✓ There are some serious problems associated with the interaction of concurrently
running processes:
1. Race Condition
✓ 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. 1 Printer Spooler: a spooler folder is used by processes to maintain name of files
the process needs to print. Assume that this spooler directory consists a large number
of partitions, 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 3

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 31


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Assume that simultaneously process A and process B decide they want to queue a file
for printing
❖ Process A reads infile and keepsthe result 7 in its local variable (x=infile)
✓ An interrupt message has been sent to the CPU and it concluded that process A has
executed long enough so it gives the privilege to process B
✓ Process B reads infile and stores the value 7 in its local variable (y=infile)
✓ Process B keeps the label 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 daemon will now print the file of process A, process B file will never get any
output
E.g.2 Character echo procedure: consider the following globally shared procedure
Void echo
{

chin=getchar(); //read a character from keyboard

chout=chin;

putchar (chout); //display the character on the screen

❖ Consider two processes (P1 & P2) trying to access the procedure
❖ Process P1 invokes the echo procedure and is interrupted immediately after the
conclusion of getchar function (chin =x)
❖ Process P2 is activated and invokes the echo procedure, which runs to conclusion,
inputting an displaying a single character, y
❖ Process P1 is resumed. By this time the value x has been overwritten in chin and
therefore lost. Instead chin contains y, and is displayed twice
2. Deadlock
❖ It is the permanent blocking of a set of processes that either 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: consider a situation in which four cars have arrived at a four-way
stop intersection at the same time. The four quadrants of the intersection are the
resources over which control is needed. If all four cars proceed into the intersection,
then each car controls one resource (one quadrant) but cannot proceed because the
required second resource has already been controlled by another car. Hence deadlock
will occur. The main reason for this deadlock is because each car needs exclusive use
of both resources for certain period of time
❖ It is a difficult phenomenon to anticipate and there are no easy general solutions to this
problem.
❖ We shall be discussing deadlock problem and handling mechanisms in the next unit.
3. 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.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 32


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ 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 that shall be addressed in the next unit
D. Mutual Exclusion
❖ 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

❖ The fundamental
strategy for implementing mutual exclusion is avoiding the situation where no two
processes could ever be in their critical regions at the same time.
❖ There are some basic requirements that should be met while implementing mutual
exclusion
i. No two processes may be simultaneously in their critical regions
ii. No assumptions may be made about speed or the number of processors
iii. No process running outside its critical region may block other processes
iv. No process should have to wait forever to enter its critical region
Implementing Mutual Exclusion with Busy Waiting
✓ In this section, various proposals for achieving mutual exclusion with the help of busy
waiting are examined:
A. 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
Disadvantages
1. Processes shall not be given the ability to close or shut down interrupts. For instance,
a process can turn off interrupts but never turn them on again in which the whole
system freezes
2. 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.
3. 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.
B. Lock Variables
❖ 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 variable has a value 1, the process needs to wait until the value changes to 0

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 33


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Thus a 0 indicates that no process is in its critical region, and a 1 indicates some
process is in its critical region
Disadvantages
❖ Assume a process observes that the lock is set to 0.but just before this process
changes the lock to 1, another process is scheduled, runs and changes the lock to 1.
The first process then gets a chance to execute again, it will also set the lock to 1
making both processes to be in their critical region simultaneously causing race
condition
❖ Continuously testing a variable waiting for some value to appear is called busy waiting.
This technique wastes processor time
C. Strict Alternation
✓ Strict alternation is shown in the program fragment below for two processes, process
0 and process 1. This solution requires that the two processes strictly alternate in
entering their critical regions.
while (TRUE) {

while(turn!=0) /*wait*/;

critical_region();

turn=1;

noncritical_region();

while (TRUE) {

while(turn!=1) /*wait*/;

critical_region();

turn=0;

noncritical_region();

✓ 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 the first process, process 0, exits the critical region, it puts turn to 1, so
that process 1 can be in its critical region.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 34


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Disadvantages

✓ If processes are not of equal efficiency and one is much slower than the other,
taking turns is not a good idea as it violates condition 3 of implementing mutual
exclusions: process 0 is being blocked by a process not in its critical region.
D. Peterson’s Solution
✓ It is a software solution. It combines turn takings with lock variables and
warning variables.
✓ It does not require strict alternation.
✓ Before using the shared variable, i.e. before entering its critical region, each
process calls enter_region procedure with its own process number, 0 or 1 as a
parameter.
✓ This call will cause it to wait, if need be, until it is safe to enter. After it has
finished with the shared variables, the process calls leave_region procedure to
indicate that it is done and to allow the other process to enter, if it so desires.
The code is shown below:

#define FALSE 0

#define TRUE 1

#define N 2 /* number of processes*/

int turn; /*whose turn is it?*/

int interested[N]; /*all values initially 0 (FALSE)*/

void enter_region (int process) /*process is 0 or 1*/

int other; /*number of the other process*/

other = 1-process; /*the opposite of the process*/

interested[process] = TRUE; /*show that you are


interested*/

turn = process; /* set flag*/

while (turn ==process && interested[other]!=TRUE); /*null


statement*/

void leave_region (int process) /*leaving process*/

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 35


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

interested[process] = FALSE;

✓ At the start, both processes are not in their critical section. Now process 0
invokes enter_region. It shows its need by setting its array element & sets turn to
0. Since enter_region is not called by any other process (process 1), it responds
immediately. If process 1 now invokes enter_region, it will wait there until
interested process[0] goes to false, a situation that occurs only when process 0
instantiates leave_region to leave the critical region.
✓ Next assume a situation where both processes envoke enter_region almost
simultaneously. Both will put their process number in turn and the store
performed last is the one to exist in turn as the first one will be overridden and
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.
E. 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.
❖ To implement mutual exclusion with TSL instruction, a shared variable, lock, is
used and it monitors access to shared memory. When lock is 0 the process may
set it to 1 using TSL instruction and executes its critical section. When finished,
the process assigns back lock to 0.

enter_region:

TSL register, lock ; copy lock to register and set lock to 1

CMP register, #0 ; was lock 0?

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 36


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

JNE enter region; if it was non zero, lock was set, so loop

RET ; return to caller, critical region entered

leave_region:

MOV lock, #0 ; store 0 in lock

RET ; return to caller

✓ enter_region has busy waiting until the lock is free and needs to be invoked by a
process which requires to enter to its critical region, When the lock is free, the
enter_region gets the lock and returns this to the caller process making it enter its
critical region. After the critical region the process invokes the leave_region
method, to set lock to 0.

Implementing Mutual Exclusion 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: Let’s assume a computer system having two processes, H
with higher precedence 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
forever.
✓ Let’s move our discussion to some process communication primitives that blocks when
they can’t enter their critical region instead of wasting CPU time Sleep: It is a system
call that causes the caller to block, i.e. be suspended until another process wakes it
up.
✓ 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)

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.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 37


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ 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
#define N 100 /* number of slots in the buffer*/

int count =0; /*number of items in the buffer*/

void producer ()

while (TRUE)

{ /*repeat forever*/

item=producer_item(); /*generate next item*/

if(count==N) sleep(); /*if buffer is full, go to


sleep*/

insert_item(item); /*put item in buffer*/

count=count+1; /*increment count of items in buffer*/

if(count==1) wakeup(consumer); /*was buffer empty?*/

void consumer ()

while (TRUE)

if(count==0) sleep(); /*if buffer is empty, go to sleep*/

item=remove_item(); /*take item out of buffer*/

count=count-1; /*decrement count of items in buffer*/

if(count==N-1) wakeup(producer); /*was buffer full?*/

consume_item(item); /*print item*/

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 38


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

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.
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
a wakeup-waiting bit which is a piggy store for wakeup signals
❖ The bit will be set during a wakeup signal submission for an already awake process.
And later, if the process attempts to go to sleep while the wakeup-waiting bit is on, the
bit 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.
A. 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

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
❖ Checking the value, changing it and going to sleep is all done as a single, indivisible
atomic operation. after a semaphore has started its operation, other processes can’t
communicate with it unless it comes to completion or the operation is blocked

UP operation

❖ It increments the value of the semaphore. If one or more processes were sleeping on
that semaphore, unable to complete an earlier DOWN operation, one of them is chosen
by the system and is allowed to complete its DOWN operation
❖ The process of increasing the semaphore and waking up a process is also indivisible.
❖ Semantics of DOWN and UP operations
Void DOWN(s:semaphore)
{
if(s==0) sleep();
s=s-1;
}
Void UP(s:semaphore)
{
s=s+1;
wakeup a sleeping process if any;

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 39


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

}
❖ Semaphores can be implemented in either of the two ways:
❖ As system calls. The operating system disables all interrupts and performs semaphore
testing, update the semaphore and make the semaphore asleep, if required
❖ With lock variables: in a multi-processor system, lock variables can be used to protect
each semaphore and ensure only one CPU is evaluating a semaphore through the
TSL instruction.
Solving the Producer – Consumer problem using semaphores
#define N 100 /*number of slots in the buffer*/

typedef int semaphore; /*semaphores are a special kind of int*/

semaphore mutex =1; /*controls access to critical section*/

semaphore empty =N; /*counts empty buffer slots*/

semaphore full =0; /*counts full buffer slots*/

void producer ()

int item;

while (TRUE) {

item=produce_item(&item); /*generates something to put in


buffer*/

down(&empty); /*decrements empty counts*/

down(&mutex); /*enter critical section*/

insert_item(item); /*enter new item in buffer*/

up(&mutex); /*leave critical section*/

up(&full); /*increment count of full slots*/ }

void consumer ()

int item;

while (TRUE) {

down(&full); /*decrement full count*/

down(&mutex); /*enter critical section*/

item=remove_item(&item); /*take item from buffer*/

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 40


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

up(&mutex); /*leave critical section*/

up(&empty); /*increment count of empty slots*/

consumer_item(item); /*do something with the item*/ }

❖ The solution uses three semaphores: full, to count the number of full slots, empty, to
count the number of empty slots and mutex, to ensure both the producer and the
consumer are not accessing the buffer simultaneously
❖ Mutex is used for mutual exclusion, i.e. it is developed in a way that ensures read and
write operations can be performed on a buffer and associated variables only by one
process at a time Full/empty are used for synchronization, i.e. they are designed to
guarantee that certain event sequences do or do not occur. When the buffer is
observed to be full, the producer stops running , and the consumer does the same
when it observes the buffer is empty
❖ Binary semaphores are semaphores initialized to 1 and used by processes to
guarantee only one of them can be in their critical region at the same time.

Problems

✓ Semaphores are too low-level and error prone. If you are not careful when using them,
errors like race condition, deadlocks and also other unexpected and irreproducible
behaviors can occur.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.
B. 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

✓ While it is possible for processes to invoke procedures of a monitor, direct interaction


to the internal data structures of a monitor is not allowed to them.
✓ 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
✓ It is the compiler that implements mutual exclusion. it is not important for the monitor
programmer to know the mutual exclusion arrangement made by the compiler. It is
enough to know that using monitor procedures and replacing critical regions of
processes ensures no two processes will ever execute their critical regions at the same
time.
✓ Monitors use WAIT and SIGNAL operations on condition variables, to block and wake
up a process.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 41


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

WAIT

✓ When a monitor procedure knows it cannot go on, 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.

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
to eliminate the concurrent presence of two active processes in a monitor. Running a
SIGNAL operation on condition variable which is being waited by more than one
process revives only one of these processes based on the system scheduler’s
decision.
✓ Condition variables are not counters. They do not accumulate signals for later use,
unlike semaphores. If a condition variable is signaled with no one waiting on it, the
signal is lost.
✓ The WAIT must always come before the SIGNAL
✓ An outline of the producer-consumer problem with monitors is shown below

Monitor Producer-Consumer

Condition full, empty;

Integer count;

Procedure enter;

Begin

If count=N then wait(full);

Enter_item;

Count=count+1;

If count =1 then Signal(empty);

End;

Procedure remove;

Begin

If count=0 then wait(empty);

remove_item;

Count=count-1;

If count =N-1 then Signal(full);

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 42


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

End;

Count=0;

End monitor;

Procedure producer;

Begin;

While true do

Begin

Produce_item;

ProducerConsumer.enter;

End;

End;

Procedure consumer;

Begin;

While true do

Begin

ProducerConsumer.remove;

Produce_item;

End;

End;

✓ WAIT and SIGNAL operations resemble SLEEP and WAKEUP operations respectively
but with one critical difference. SLEEP and WAKEUP failed because while one process
was trying to go to sleep, the other one was trying to wake it up. With monitors, this
cannot happen.
✓ The automatic mutual exclusion on monitor procedures guarantees that if, say, the
procedure inside a monitor discovers that the buffer is full, it will be able to complete
the WAIT operation without having to worry about the possibility that the scheduler
may switch to the consumer just before the WAIT completes.

Advantage of monitors

✓ By making mutual exclusion of critical regions automatic, monitors make parallel


programming much less error prone than with semaphores

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 43


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Drawback of monitors

✓ A computer language with pre-defined monitors is required and languages that have
built-in monitors are rare. But adding semaphores in C, C++ and other languages is
easy.
Drawback of monitors and semaphores

✓ The initial objective was to avoid mutual exclusion situations in a single or


multiprocessor system sharing same main memory
✓ In a distributed system consisting of multiple CPUs, each with its own private memory,
connected by a local area network, these primitives become inapplicable
✓ Monitors are unusable in most programming languages due to their requirement and
semaphores are too primitive.
✓ And none of them also facilitate information exchange between systems.
C. Message Passing
✓ This technique uses t the SEND and RECEIVE methods
✓ SEND and RECEIVE are system calls and they can be put as library procedures:
SEND(dest, &msg), RECEIVE(source, &msg)
✓ If no message is available, the receiver could block until one arrives or could return
with an error code
✓ The producer-consumer problem with message passing
✓ An outline for the solution of producer-consumer problem with message passing is
shown below
#define N 100

void producer()

int item;

message m;

while (TRUE) {

produce_item(&item);

receive(consumer, &m);

build_message(&m, item);

send(consumer, &m); }

void consumer()

int item, i;

message m;

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 44


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

for(I=0;I<N;I++) send (producer, &m);

while(TRUE) {

receive(producer, &m);

extract_item(&m, &item);

send(producer, &m);

consume_item(item);

❖ All messages are of the same size


❖ Messages sent are buffered automatically by the operating system
❖ The consumer starts out by sending N empty messages
❖ Whenever the producer has an item to give to the consumer, it takes an empty
message and sends back a full one
❖ If the producer works faster, the message buffer will be full and all messages will end
up waiting for the consumer
❖ The producer will be blocked, waiting for an empty messages to comeback
❖ If the consumer works faster, the message buffer will be consumed quickly waiting for
the producer to fill them up
❖ The consumer will be blocked, waiting for a full message

Message passing variants (How messages are addressed?)

Using process address


❖ Assigning each process a unique address and have messages be addressed to
processes
o Using mailbox
❖ A mailbox is a space to keep some amount of messages, as indicated during the
mailbox definition
❖ The address parameters in SEND and RECEIVE calls are mailboxes not processes
❖ The producer sends packet filled with a data to the consumer’s mailbox and the
consumer sends empty messages to the producer’s mailbox
❖ Rendezvous (Eliminate buffering)
▪ SEND is done before RECEIVE

❖ SEND won’t run unless RECEIVE executes enabling direct transfer of message from
sender to the receiver and avoiding the need to have an intermediate buffering process
❖ If ,on the other hand, the receive is executed first, the receiver will be blocked until a
send happens
❖ It is easier to implement
❖ It has minimal flexibility as sender and receiver are forced to run in lock stop
❖ Design issues for message passing
❖ Messages can be lost. The solution for this problem is using acknowledgement
message

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 45


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Acknowledgement message can be lost. this problem can be overcomed by placing


successive sequence numbers within each original message
❖ Ambiguous process names. The solution for this problem to use naming conventions
❖ Authentication
❖ Performance, especially if sender and receiver processes are on the same machine

Assessment:
Instructions: Answer the following. Write your answers in a ½ sheet of paper and
submit it during the face to face schedule.

1. Discuss the problems of concurrency.

2. What kind of solution does test-and-set lock (TSL) provide?

3. Differentiate between semaphores and monitors?

4. How does deadlock happen in a system? Explain

2.5 Process Scheduling

❖ Starvation is one of the problems of concurrency. In a multiprogramming environment,


processes compute for the uniprocessor. How the operating system does execute
these processes in turn with minimal starvation?
❖ Kernel is one part of an operating system that carries out several activities and
operations among which process management is one. The process management task
includes handling process creation, termination, inter-process communication,
scheduling, switching, dispatching and management of process control blocks.
Scheduling is a means through which fair distribution of execution time is granted to
processes with similar state for a multiprogramming system. Multiprogramming aims
to have some process running at all times and maximize CPU utilization by switching
the CPU among processes so frequently that users can interact with each program
while it is running.
❖ The two main objectives of the process scheduling are to keep the CPU busy at all
times and to deliver “acceptable” response times for all programs, particularly for
interactive ones. The process scheduler must meet these objectives by implementing
suitable policies for swapping processes in and out of the CPU. The idea behind
multiprogramming is to execute a process until it changes its state to waiting, typically
for the completion of some I/O request and use this time productively rather than
making the CPU idle until the process comes back to its ready state again. To
accomplish this, several processes are kept in memory at one time and when one
process has to wait, the operating system takes the CPU away from that process and
grants to another process. This pattern continues.
❖ However, there is a frequent chance for more than one process to be in a ready state
at the same time which forces them to compete for the CPU. The system should then
make a wise choice as to which process to run next. Component of the operating
system with the responsibility of choosing a process to run is known as scheduler and
the steps it follows scheduling algorithm. The task of the scheduler is, thus, to decide
on a policy about which process to be dispatched, select a process according to the

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 46


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

scheduling algorithm chosen and load the process state or dispatches the process for
execution. Scheduling is a means by which process migration between the three
different queues maintained by a multiprogramming system can be achieved.
❖ These three different queues of processes are:
➢ Job queue: set of all processes found in the system.
➢ Ready queue: set of all processes residing in main memory, ready and waiting
to execute.

➢ Device queue: set of processes waiting for a particular I/O device.

Figure2.7. The three processes queue and CPU interaction among them

✓ There are two kinds of schedulers in a system: Long-term scheduler (job scheduler)
and Short-term scheduler (CPU scheduler).
1. Long-term scheduler: selects processes from job queue and loads the selected
process into memory for execution and then updates the ready queue. This
scheduler also manages the number of processes in memory that entitles it in
control of the multiprogramming degree within a system. A good mix of CPU-bound
and I/O bound processes should be maintained in memory by this scheduler to
make the system more efficient. This scheduler is invoked very infrequently and
thus can be slower when it comes to performance. Such type of schedulers may
not be present in some systems like time sharing ones.
2. Short-term scheduler: Decides which process to execute next and allocates the
CPU. It picks processes found in the ready queue and dispatches them for
execution by the CPU. This scheduler is also known as the dispatcher and invoked
very frequently which makes it to be extremely fast. As the CPU is one of the
primary computer resources, its scheduling is central to operating system design.
✓ Scheduling is fundamental to operating system function. Almost all computer
resources are scheduled before use. The scheduler will not have that much of a task
on simple multi-programmed PCs as only one program is actually running currently
and the user also waits for the completion of all programs. When it comes to high-end
networked workstations and servers, the scheduler has a real job here and scheduling
algorithms used significantly affects the efficient use of the system. When does a

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 47


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

system need to schedule is a question that should be addressed before going deep
into the scheduling process.

State change from running to waiting.

✓ The current process goes from the running to the waiting state because it issues an
I/O request or some operating system request that cannot be satisfied immediately.

Termination of the current process

✓ State change from running to ready. For instance, a timer interrupt causes the
scheduler to run and decide that a process has run for its allotted interval of time and
it is time to move it from the running to the ready state. State change from waiting to
ready. For instance an I/O operation is complete for a process that requested it and
the process now moves from the waiting to the ready state. The scheduler may then
decide to pre-empt the currently-running process and move this ready process into the
running state.
✓ Some processes spend more time in a waiting state as they have more I/O requests
while some processes spend more time in their running state. The period of
computation between I/O requests of a process is called the CPU burst. In general,
processes can be described as I/O bound or CPU bound. An I/O-bound process is one
that spends more of its time doing and waiting for I/O than it spends doing
computations. Such type of processes generally experience short CPU bursts. A CPU-
bound process, in contrast, generates I/O requests infrequently, using more of its time
doing computations and exhibit long CPU bursts. It is important that the job scheduler
selects a good process mix of I/O-bound and CPU-bound processes for best
performance.
✓ There are two types of process scheduling policies that can be used by the scheduler.
These are: pre-emptive scheduling algorithm and non-pre-emptive scheduling
algorithm.

Non Pre-emptive scheduling

✓ If the scheduler cannot take the CPU away from a process, it is referred as non-pre-
emptive or cooperative scheduler. Such types of scheduling run a process to
completion and there will not be any scheduling choice to be made. The process itself
releases the CPU for other processes willingly by terminating or by moving to a waiting
state. In both cases, a software interrupt is issued which in turn switches the CPU over
to start running the scheduler. The scheduler can now decide the next process to be
run on the CPU. A non-pre-emptive or cooperative scheduling is a way in which
processes will not be asked to give up the CPU unless they terminate or change their
state to waiting. Older versions of Macintosh operating system and Windows 3.x are
examples that had used the non-pre-emptive scheduling method.

Pre-emptive scheduling

✓ A pre-emptive scheduler has the ability to get invoked by an interrupt and move a
process out of a running state and let another process run. a special hardware device
called an interval timer is designed to produce a hardware interrupt on a periodical
basis (known as a time slice) and when this interrupt is received by the CPU, it will
switch over to start running the scheduler, which will in turn decide on a new process

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 48


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

to be run on the CPU. Such types of scheduling algorithms make interruption to


distribute the CPU fairly among processes. A process is allowed to run only for some
fixed amount of time and when the time is over, another process from the ready list will
be picked and granted the CPU.
✓ Pre-emptive scheduling has two main drawbacks, the extra cost incurred while
accessing shared data and the complexity it adds on the kernel of the operating
system. Note that in both cases, the scheduler will be activated upon reception of an
interrupt. For non-pre-emptive scheduling, the interrupt is a software interrupt issued
by the process, whereas in pre-emptive scheduling, the interrupt is a hardware
interrupt issued by an external hardware device or it can also be a software interrupt
(through an I/O operation) issued by a process which will activate the scheduler in the
same way as in non-pre-emptive scheduling.

Context switch and the interrupt handler

✓ Whenever the CPU switches from executing one process to executing another
process, a context switch is said to take place. Context switching requires saving of
information for the processes so that execution can recommence at the right place.
For example, assume you are arguing with your friend about the benefits of learning
Operating System instead of Computer Maintenance (or vice versa, of course!) and
you are rudely interrupted by another friend who insists on having an urgent discussion
about a project.
✓ After you have finished your discussion about the project, you return to your first friend.
At this stage, in order to be able to resume the conversation at exactly the point you
left off at, you will need to remember what you were talking about, the stage of the
argument you were at and all the points that you have presented so far. If you had not
previously ‘saved’ this information, you would have to restart the conversation from
scratch, hardly an appealing consideration for your friend. This same analogy should
be performed by the CPU every time one process’ execution is interrupted and another
process gets the privilege.
✓ Assume that the CPU is currently executing instruction 4 of process A, and for some
reason, a switch is made to process B. If execution is to be resumed at A, then we will
need to know the next instruction to be executed (presumably instruction 5). Thus the
contents of the PC and other registers as well must be saved in the PCB for process
A.
✓ After all the saving is done, the CPU is now ready to execute process B. In order to do
this, it will need to know the next instruction to execute in process B and store the
address of this instruction in the PC which can be found in the PCB of process B. In
effect, during a single context switch, we need to first save the context of the current
process that is going to be suspended and then restore the context of the new process
that the CPU is going to switch to.
✓ This entire context saving and restoring is done by a special program called the
interrupt handler that is part of the process management component of the OS. Saving
and restoring information by the interrupt handler is a time consuming operation and
represents wasted time as far as the CPU is concerned.
✓ This is because the CPU is only considered to be doing something useful when it is
executing instructions that belong to a user process (either process A or B in the
previous example). During a context switch, the CPU is actually executing the interrupt
handler which handles the saving and restoring of information contained in the
individual PCBs.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 49


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

✓ The more context switches there are between processes, the more time is wasted
running the interrupt handler (instead of running the processes) and the less efficient
the CPU will be. This is an important consideration especially when it comes to pre-
emptive scheduling.
Scheduling criteria

✓ Different schedulers aim to achieve different goals like minimizing latency, maximizing
throughput or maximize process utilization, etc. Some properties are needed by all
systems while some depend on the implementation environment due to a variant in
the specific requirements of the various application areas as well as different operating
systems.
✓ In a batch operating system, for instance, time is an insignificant attribute to be
considered by the scheduler as users are not looking for instant response from the
system. As a result non-pre-emptive or pre-emptive with long time for each process
scheduling algorithms can be used by the scheduler enhancing the system’s
performance through less process switching.
✓ On the other hand, in a real time environment, where processes are aware of giving
up the CPU so quickly to one another, light pre-emptive scheduling algorithm is
enough. For interactive systems, however, restricted pre-emptive scheduling algorithm
is needed to avoid depriving CPU among processes. Some of the common criteria
different schedulers considering a scheduling algorithm are the following.
➢ Fairness: all processes should be able to use the CPU fairly, especially
comparable processes. Fairness is a way in which the scheduler tries to allocate
CPU for comparable processes at comparable rate so that equitable access to
resources is granted. Sometimes, however, fairness is at odds with other
scheduling criteria. For instance, throughput and response time of a system can
be enhanced by making it less fair.
➢ System utilization: the CPU as well as I/O devices of the system should always
be kept busy. Keeping them execute all the time implies more operation
performed per second. The scheduler needs to keep a mixture of CPU and I/O
bound processes in memory to avoid idleness in all components of the system.
In real systems, this ranges from 40% to 90% utilization
➢ Throughput: is the amount of tasks completed by a system per time unit. Ideally,
a system is required to finish as many processes as possible though the actual
number depends upon the lengths of the processes. Throughput should be
maximized by minimizing overheads and efficiently using systems’ resources.
➢ Turnaround time: The time gap between arrivals of a process to its completion.
It is the sum of the periods spent waiting to get into memory, waiting in the ready
queue, executing on the CPU, and doing I/0. It predicts for how long an average
user needs to wait for a response from the system. Generally, a process is
expected to take minimal time from its creation to termination.
➢ Response time: is the time elapsed to get the result of a submitted command.
It is the amount of time it takes from when a request was submitted until the first
response is produced, not output. This is very crucial especially for interactive
and real time systems.
Ideally, a scheduling algorithm needs to maximize resource utilization and
through put while minimizing turnaround time and response time.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 50


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Scheduling Algorithms

First-Come First-Served (FCFS) Scheduling

❖ This is the simplest non- pre-emptive scheduling algorithm whereby the CPU executes
processes that arrived earliest. In this algorithm, processes are dispatched according
to their arrival time.
❖ There is one FIFO queue for ready state processes. When the first process comes to
the system, it will be started immediately and run till it finishes all its jobs or till it needs
to. If other jobs have arrived during execution of the first process, they will be lined up
in the ready queue and will get a chance to run sequentially when the first process
terminates or blocks.
❖ When a blocked process changes its state to ready, it will be treated like a new one
and put at the end of the queue. The main advantage of this algorithm is its
simplicity to implement.
❖ A single linked list needs to be created and every time the system is free, the front
child will be removed from the list and every time new process is added to the ready
state or a blocked process has moved to its ready state, a new node is added at
the end of the list.
❖ However, FCFS scheduling comes with a severe problem which is poor waiting
and turnaround time for processes.
❖ Consider three processes P1, P2, and P3 submitted to the ready queue in this
sequence and assume the processes have a burst time of 24, 3, and 3 respectively.
Process P1 will run automatically as there is no other process ahead of it in the
queue which makes the waiting time of it 0. Waiting time of P2 is 24 as it goes after
P1 has completed.
❖ Waiting time for P3 will be 27 (P1 run time+ P2 runtime) as shown in figure 2.8
below and the average waiting time becomes 17 [(0+24+27)/3].The average
completion time for the three process
also become 27 [(24+27+30)/3]. This
shows that for processes with long
runtime being in front of the queue, short
jobs get stuck behind and makes the
waiting time very long reducing the
performance of these processes. Such types of scheduling algorithms are also not
suitable for interactive systems.

Shortest Job First (SJF)Scheduling

❖ This is another non-pre-emptive scheduling algorithm that handles jobs based on the
length of time that the CPU requires to process them. When there are several equally
important jobs queued in the ready queue waiting to be started, the scheduler picks
the process with the shortest execution time requirement first.
❖ Moving a short job before long one decrease the waiting time for short job more than
it increases the waiting time for the longer process. SJF scheduling prefers I/O bound
processes because running short CPU-burst jobs first gets them done and moves them
out of the way and it also enables to overlap their I/O request which enhances the
efficiency of the CPU.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 51


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ This algorithm is actually taken from the “procrastination” behaviour of human beings.
When faced with too much work, we prefer to do the short tasks first and get them out
of the way leaving the big, hard tasks for later.
❖ This algorithm is efficient only if it is possible to predict in advance the amount of
CPU time required by each job which is possible for batch environments, but difficult
for interactive systems. If two processes have the same length next CPU burst,
FCFS scheduling is used to break the tie.
❖ Consider the previous three processes P1, P2 and P3 with same CPU time and similar
sequence. Now using SJF, P2 will be executed first followed by P3 and at last P1 if
they arrived simultaneously.

As you can observe on the figure, waiting time for P2 has become0, for P3 to be3 and
for P1 6 reducing the average waiting time from 17 (FCFS) to 3 and the average
completion time 13 [(3+6+30)/3] from 27.
✓ However, it is worth to note that SJF is optimal only for simultaneously available jobs.
Shortest Remaining Time Scheduling

❖ This is a pre-emptive variant of the SJF scheduling algorithm which preempt the
currently running job if a newly arriving job has shorter expected CPU burst time.
It operates by comparing the time left for completion of the running process with
the total execution time of a new process.
❖ The scheduler immediately stops the running process and gives higher priority to
the newly arriving process if and only if its execution time is smaller than remaining
completion time of the running one. Again in this algorithm, the runtime of a process
needs to be known in advance.
❖ The advantage of SRT scheduling algorithm is the optimal average response time
resulted and the reduced waiting time especially for short jobs. However, larger
jobs might be deprived of the CPU and end up in starvation if several small jobs
arrive.

Round Robin Scheduling (RR)

❖ This scheduling algorithm is known to be the simplest, fair and most widely used
scheduling algorithm most suitable for interactive systems.
❖ It assigns time slices to each process in equal portions and in circular order,
handling all processes without priority.
❖ Round-robin scheduling can also be applied to other scheduling problems, such as
data packet scheduling in computer networks. The name of the algorithm comes
from the round-robin principle known from other fields, where each person takes
an equal share of something in turn.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 52


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ It is similar to FCFS scheduling, but pre-emption is added to switch between


processes. A small unit of time, called a time quantum (or time slice), is defined
which is generally from 10 – 100 milliseconds.
❖ The ready queue is treated as a circular queue. The scheduler goes around the
ready queue, allocating the CPU to each process for a time interval of up to 1 time
quantum. No process is allocated the CPU for more than 1 time quantum in a row.
If a process’ CPU burst exceeds 1 time quantum, that process is preempted and is
put back in the ready queue.
❖ To implement the RR scheduling, we keep the ready queue as a FIFO queue of
processes. New processes are added to the tail of the queue. The CPU scheduler
picks from the head of the queue, sets a timer to interrupt after 1 time quantum,
and dispatches the process.
❖ One of two things will then happen. The process may have a CPU burst of less
than one time quantum in which case the process itself releases the CPU
voluntarily and the scheduler will then proceed to the next process in the ready
queue.
❖ Otherwise, if the CPU burst of the currently running process is longer than 1 time
quantum, the timer will go off and will cause an interrupt to the operating system.
A context switch will be executed, and the process will be put at the tail of the ready
queue.
❖ The scheduler then selects the next process in the ready queue. It is important to
note that the length of the quantum time determines the efficiency of the algorithm
as well as the turnaround time.
❖ If the quantum time is too large, RR becomes a mere copy of FCFS scheduling
algorithm and results in poor response for small interactive jobs. If too small is the
quantum time, it results in several switches between processes increasing the
average turnaround time and reduce the efficiency of CPU. A quantum around 20-
50msec can be a reasonable compromise.
❖ Example: Consider four processes P1, P2, P3 and P4 arrived at time 0 with CPU-
burst time of 53,8,68, and 24msecs respectively. Using the RR scheduling
algorithm with 20msec quantum time, the processes will be executed in the
sequence shown in the following chart below

Waiting time for P1=(68-20)+(112-88)=72

P2= (20-0) =20

P3= (28-0) + (88-48) + (125-108) =85

P4= (48-0) + (108-68) =88

Average waiting time = (72+20+85+88)/4=66¼

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 53


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Priority Scheduling

❖ In a multiprogramming system, processes can be given different rank taking into


consideration behaviours of the processes or other system defined or user defined
criteria.
❖ For instance, a daemon process transferring email in the background should have
less priority than a foreground process outputting video to the visual display in
real time.
❖ A priority scheduling algorithm works by allocating the CPU for processes with
highest priority value. Each process in a job queue is assigned a priority value
according to its importance and urgency, the scheduler then allocates CPU to the
processes according to their priority level. Again, note that for processes with
similar priority value, FCFS scheduling algorithm is used.
❖ Priority is expressed in terms of fixed range number such as 0 to 10. However
there is no general agreement on whether 0 is the highest or lowest priority. Some
systems use low numbers to represent low priority while others use low numbers
for high priority. But in this module, we will use low numbers to represent high
priority.
❖ Priority scheduling works as pre-emptive as well as non-pre-emptive policy. The
priority of a newly arriving process into a ready queue is compared with that of
the currently running process. A pre-emptive priority-scheduling algorithm will
pre-empt the CPU if the priority of the newly arrived process is higher than that
of the currently running process.
❖ A non-pre-emptive priority-scheduling algorithm, on the other hand, will simply
put the new process at the head of the ready queue and waits for the running
process to give the CPU voluntarily and run the arrived process then after.
❖ Processes can be given priority statically or dynamically. In a static priority, process
is assigned a fixed priority value according to some external criteria such as
importance of the process, owner of the process, or any other factor which is not
part of the operating system.
❖ In such cases, the priority will not be adjusted dynamically. In dynamic allocation
of priority, the system uses some measurable quantities or qualities including time
limits, memory requirements, file requirements and CPU or I/O requirements to
compute priority of a process and accomplish a certain system goals.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 54


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ For example a process with high I/O bound time, high priority will be given and
CPU is granted immediately to avoid memory occupation for unnecessarily long
time.
❖ Example: Consider list of processes with arrival time, burst time and priority level
as given in the following table below
Process Arrival CPU- Priorit
Burst y
time

P1 0 3 2

P2 1 2 1

P3 2 1 3

At time, 0sec:

Only P1 has arrived to the ready queue with Burst Time 3 and Priority 2. So CPU will
start P1 and do 1 second job of P1.

At time, 1sec:

Now the processes become two; P1 that arrived at 0sec and P2 arrived at 1sec. since
priority of P2 is higher than that of P1, P2 will get a chance to use the CPU and will run
for 1sec
At time, 2sec:

Now there are 3 jobs; P1 that arrived at 0sec and has 2sec job to be done with Priority
2, P2 that arrived at 1sec and has 1sec job to be done with Priority 1 and P3 that
arrived at 2sec and has 1sec job to be done with Priority 3.Here, P2 is with the highest
priority, thus CPU will do 1sec remaining job of P2 which brings the process to
completion.

At time, 3sec:

Two processes are left, P1 and P3 with 2 and 3 priority levels respectively. P1 will be
thus, executed to completion till 5sec.

At time, 5sec:

There is only 1 job left by now, that is P3 with burst time 1 and priority 3. So CPU will
do 1sec job of P3 bringing it to completion. This whole activity can be shown in a time-
line as shown below

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 55


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

The average waiting time can be computed as:

(P1’s Waiting Time +P2’s Waiting Time + P3’s Waiting Time)/3= (2+0+3)/3=1.7sec

❖ It is also possible to group processes into a priority class and use the priority scheduling
among the groups while RR or other scheduling algorithm can be used for the
processes inside a class
❖ A major problem with priority scheduling is indefinite blocking or starvation of
processes with the least priority value. Low priority processes indefinitely wait for the
CPU because of a steady stream of higher-priority processes. Aging can be used as a
solution towards this problem. Aging is a technique of gradually increasing the priority
of processes that wait in the system for a long period of time.

Multilevel Queue Scheduling

❖ This scheduling algorithm uses the best of each algorithm by having more than one
queue and is used for situations in which processes can easily be classified into
different groups.
❖ For example, a common division is made between foreground (interactive) processes
and background (batch) processes. These two types of processes have different
response-time requirements and so may have different scheduling needs.
❖ Ready queue is partitioned into several separate queues and processes will be
permanently placed in these queues based on some criteria.
❖ Each queue then implements its own scheduling algorithm for the processes inside
and commonly a static pre-emptive scheduling algorithm is used among the queues.
For example foreground queue may have absolute priority over background queue.

❖ Therefore no process in the background queue could run except the foreground
queues are empty. If a process entered the foreground queue while a process from
the background queue is running, the background queue process will be pre-empted
and the new process from the foreground queue gets the CPU causing possible
starvation for the background queue process.
❖ To address this problem, time slice can be used between the queues where each
queue gets a certain portion of the CPU time which it can then schedule among the
various processes in its queue. On the above example, for instance, the foreground
queue can be given 80% of the CPU time for RR scheduling among its processes,
whereas the background queue receives 20% of the CPU to give to its processes in
FCFS manner.
❖ Multilevel Feedback Scheduling algorithm is another implementation of multilevel
queue where processes have the right to move between the several queues of the
ready queue instead of being placed permanently into queues at arrival time based on
their CPU-burst properties. A process that uses too much CPU time is degraded to a
lower-priority queue and a process that waits too long is upgraded to a higher-priority
queue.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 56


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ For example consider a MLFQ scheduler with three queues, Q0 with time quantum 8
milliseconds, Q1 with time quantum 16 milliseconds and Q2 on FCFS basis only when
queues Q0 and Q1 are empty. A new job enters queue Q0 is served by FCFS and
receives 8 milliseconds. If not finished in 8 milliseconds, it is moved to Q1. At Q1 the
job is again served by FCFS and receives 16 milliseconds. If not completed, it is
preempted and moved to Q2 where it is served in FCFS order with any CPU cycles
left over from queues Q0 and Q1.

Figure 2.10. .Multilevel Feedback Queue

✓ In general, a MLFQ scheduler is defined by the following parameters:

➢ The number of queues


➢ The scheduling algorithm for each queue
➢ The method used to determine when to upgrade a process to a higher-
➢ priority and a lower-priority queue
➢ The method used to determine which queue a process will enter when that
process needs service
➢ In conclusion, there are four primary methods to servicing the queues in a
multilevel queue scheduling.
➢ In no movement between queues, all the jobs in the higher priority queue are
serviced first before any job in a lower priority queue.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 57


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Movement between queues and adjust priorities assigned to jobs. When a time slice
interrupt occurs, a job is pre-empted and moved to the end of the next lower priority
queue if it is CPU bound.
❖ On the other hand a job that is pre-empted because of an I/O operation is moved up
to the next higher level queue. In variable time quantum per queue, each queue is
given a time quantum as long as the previous queue.
❖ If a job does not finish execution in the first time quantum, it is moved down to the next
lower priority queue. If it doesn’t finish execution in the second time quantum (which
will now be longer than the first time quantum), it is moved down to the next lower
priority queue and so on.
❖ Aging is used to guard against indefinite postponement. This refers to a situation when
a job somehow, due to a combination of certain circumstances, is unable to gain
access to the CPU in order to execute.
❖ To guard against this possibility, whenever a job has not been serviced for a long
period of time, it is moved up to the next higher level queue. If it still is not serviced
after a given period of time, it is moved up again to the next higher level queue and so
on.
❖ From the definition of a multilevel queue scheduler, it is the most general CPU-
scheduling algorithm but also the most complex one as it requires some means by
which values for all the parameters can be selected.

Thread Scheduling

❖ In a previous section, we have discussed what threads are and the two different
implementations of a thread as user-level and kernel-level threads. Now let’s see how
the scheduler allocates execution time for threads in a process. When several
processes are available with several threads, scheduling differs as to whether the
threads are user-level or kernel-level.
❖ If the threads in a process are user-level, the kernel operates as usual only on
processes since it is unaware of the threads and the scheduler schedules the
processes by giving them control for a quantum. A thread scheduler inside a process
will then use any of the scheduling algorithm discussed previously and picks a thread
and grants the execution time.
❖ This thread runs until it finishes its job or until the quantum time for the process ends
in which case the system scheduler takes the CPU and grants it for another process.
When the first process is granted the CPU again, that same thread resumes execution
if it did not finish its execution earlier.
❖ In such cases, only the threads within the process are affected and other processes in
the system will run without being affected. If the threads in processes are of kernel-
level, the kernel operates on the threads and picks them to run by giving a fixed
quantum and forcibly suspend the thread if it exceeds the quantum allotted. The
scheduler does not consider from which process a thread is picked to run.
❖ The main difference between these two thread implementations scheduling is
performance where the kernel-level scheduling requires a full context switch as every
information related with a thread from one process must be maintained before a thread
from another process gets to execute, making the algorithm too slow. But the kernel
can take the cost associated with this context switching and make an optimal decision
that can improve this performance problem.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 58


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ On the other hand, scheduling the kernel-level threads will not suspend an entire
process when a thread in that process waits for I/O as user-level thread scheduling
does.
❖ One additional thing associated with user-level threads scheduling is the use of
application-specific thread scheduler where the urgency of the threads will be
examined and CPU is granted accordingly. This scheme increases parallelism in
processes where several I/O bound threads are available

Multiple Processor Scheduling

❖ So far, our discussion was solely about scheduling processes and threads on a single
processor system. However, there are also multi-processor systems with processes to
be scheduled where the scheduling problem becomes even more complex.
❖ Let’s consider a system with homogeneous or identical processors and discuss the
different possible scheduling mechanisms. One scheduling possibility is to use
asymmetric processing where one of the processors entitled as a master server and
handle all scheduling, I/O processing and all other system activities while the remaining
other processors handle only user codes.
❖ Another approach in multiprocessor system can be symmetric processing where all
processors have similar level and scheduling is done by each independently.
Scheduling then proceeds by having the scheduler for each processor examine the
ready queue and select a process to execute.
❖ The ready queue can be a separately maintained one by each processor or a common
ready queue shared by all processors where the first scenario could make one
processor be idle, with an empty queue, while another processor too busy, with full
ready queue. But if a common ready queue is used, all processes go into one queue
and are scheduled onto any available processor.
❖ However, it is important to ensure that no two processors choose the same process
and that processes are not lost from the queue. Nearly all modern operating system
support this symmetric scheduling approach.

Assessment:
Instructions: Answer the following. Write your answers in a ½ sheet of paper and
submit it during the face to face schedule.

1. Define the differences between pre-emptive and non-pre-emptive scheduling


2. Explain the differences in the degree to which the following scheduling algorithms
discriminate in favor of short processes: FCFS; RR

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 59


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

2.6 Deadlock

❖ Deadlock is one of the problems of concurrency. In a multiprogramming environment,


processes compute for resources. Processes wait for resources which are owned by
other processes; in turn these processes wait for that process to release a resource.
How does the operating system bring these processes to an agreement?
❖ In a multiprogramming system, processes are always in request of the several finite
resources available in the system for their successful execution. The resources are
partitioned into several types like Memory space, CPU cycles, files, and I/O devices
(such as printers and tape drives). A process must request a resource before using it,
and must release the resource after using it. When two or more processes
simultaneously request for a resource, only one process will be allowed to use the
resource at any instant of time to avoid unintended outcomes or corrupted outputs.
❖ But for most programs, a process requires exclusive use of more than one resource at
a time without exceeding the number of available resources in the system. For
instance, two processes, A and B, may need to write document being scanned on a
CD. Process A asks for the scanner first and is allowed to use it while process B asks
for the CD recorder first because of its different programming structure and will also be
permitted to use the recorder,. After process A completes the scanning task, it requests
for the CD recorder but will be denied as it is already being in use by process B.
Process B now requests for the scanner without releasing the CD recorder which is
requested by A. such situations causes both processes to be blocked or move to their
waiting state and remain there forever as both are in need of same resource
simultaneously resulting in a deadlock condition.
❖ Deadlock can be defined as a situation whereby two or more processes get into a state
whereby each is holding a resource that the other is requesting and processes are
waiting in a loop for the resource to be released. None of these waiting processes will
ever wake up and start execution as all the processes are blocked and needs an event
to happen by one of these blocked processes to move to a running state
❖ At this point, it is important to distinguish between starvation and a deadlock. In
starvation, a process cannot continue simply because it is not allocated the CPU while
in deadlock, processes cannot continue because they are blocked and would remain
in this state even if the CPU was available to them. Deadlock is a global condition
rather than a local one which means that if were to analyze any process involved in a
deadlock; we would not be able to find any error in the program of that process. The
problem does not lie with any particular process, but with the interaction between
groups of processes that are multitasked together. Deadlock effectively brings a large
portion of the system to a halt.
✓ Normally, a process goes through three consecutive steps to utilize a resource. These are:
➢ Request a resource
➢ Use the resource
➢ Release the resource

✓ The operating system keep in a system table the status of each resource as free or
allocated and if allocated, to which process. If a requested resource is not available, the
operating system can add the requesting process to a waiting queue of processes for this
resource or the process itself waits a little while and checks again the resource as is the
case in some systems. A resource can be either a pre-emptable or non-pre-empt able
type.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 60


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

✓ A pre-emptable resource is a type of resource that can be shared by taking away from a
process owning it without negatively affecting it. A good example of such type is a memory.
✓ A non-preemptable resource, in contrary, is a non-sharable resource that cannot be taken
from a process currently owning it without negatively affecting it. CD recorders are
examples of non-preemptable resources. A deadlock happens when resources are non-
preemptable types and is most common in multitasking and client/server environments.
For a deadlock to happen between processes, the following four conditions should hold
simultaneously in a system.
✓ Mutual Exclusion: resources are non-preemptable and once a process is allocated a
resource, it has exclusive access to that resource. This resource cannot be shared with
other processes
✓ Hold and Wait: a process must be holding exclusively at least one resource and still make
a request for another resource that is currently hold by another process.
✓ No Preemption: Resources can only be released voluntarily by the process itself rather
than by the action of an external event
✓ Circular Wait: two or more processes must be in a circular chain where each process
waits for a resource that the next process in the chain holds.
✓ All these four conditions are necessary but not sufficient for a deadlock to occur. Only if
the right combinations of unfortunate circumstances arise, then will deadlock manifest
itself.

Resource-Allocation (R-A) Graph

✓ The four conditions of the deadlock can be described


more precisely in terms of a directed graph called a
system Resource allocation graph. This graph
consists of a set of vertices V and a set of edges E.
✓ V is partitioned into two different types of nodes P =
{P1, P2, …, Pn}, the set consisting all the active
processes in the system, and R = {R1, R2, …, Rm},
the set consisting all resource types in the system. A
directed edge from process Pi, to resource type Rj,
known as request edge, is denoted by Pi → Rj and
signifies that process Pi requested an instance of
resource type Rj and is currently waiting for that
resource. A directed edge from resource type Rj to
process Pi, known as assignment edge, is denoted
by Rj → Pi; it signifies that the process has been Figure 2.11. Resource allocation graph
allocated an instance of resource Rj.
✓ When a process Pi request an instance of resource type Rj, a request edge is inserted in
the resource-allocation graph. The request edge will instantaneously change to an
assignment edge when the request is fulfilled. When the process no longer needs access
to the resource it releases the resource, and as a result the assignment edge is deleted.
✓ Figure 2.11 depicts a resource allocation graph with three processes P1, P2 and P3; four
different resources R1, R2, R3 and R4, and request from process to resource. Resource
R2 and R4 have two instances while R1 and R3 are only single instance.
✓ Process P1 is holding an instance of resource type R2, and is waiting for an instance of
resource type R1.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 61


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

✓ Process P2 is holding an instance of resource type R1 and R2, and is waiting for an
instance of resource type R3.
✓ Process P3 is holding an instance of resource type R3 and has no more requests further.
✓ As can be depicted from the figure and also the description above, the processes possible
to encounter deadlock are P1 and P2 as they have a waiting state.
✓ In general, if a cycle can be seen on the resource
allocation graph and each resource type has
exactly one instance, there is an implication for a
deadlock and each process involved in that cycle
is deadlocked.
✓ In this case, a cycle in the graph is both a
necessary and a sufficient condition for the
existence of deadlock. However, if each resource
type has several instances, then a cycle does not
necessarily imply that a deadlock has occurred. In
this case, a cycle in the graph is a necessary but
not sufficient condition for the existence of
deadlock.
✓ If no cycle exists in the graph, then all the
processes are free of any deadlock situation
✓ In the resource graph diagram above, suppose
process P3 requests an instance of resource type
R2.Since no resource instance is currently
available, a request edge P3 → R2 is added to
the graph as in figure 2.12
✓ .At this point, two minimal cycles can be Figure 2.12 Resource allocation graph with
observed: deadlock
P 1 → R1 → P2 → R3 → P3 → R2 → P1 and
P2 → R3 → P3 → R2 → P2
✓ Moreover, Process P2 is waiting for resource R3, which is held by process P3. Process
P3, on the other hand, is waiting for either process P1 or P2 to release resource R2. In
addition, process P1 is waiting for P2 to release resource R1. These two conditions result
in a deadlock situation for processes P1, P2 and P3.
✓ To conclude, resource allocation graphs are tools that can be used to identify the
occurrence of a deadlock given sequence of request/release. If a cycle can be seen in the
graph, there is a possibility of deadlock none otherwise.

Assessment:
Instructions: Answer the following. Write your answers in a ½ sheet of paper and
submit it during the face to face schedule.
1. Give a practical example on how dead lock occurs.
2. Demonstrate the example that you provided on (1) using resource
allocation graph.
3. What are the requirement for deadlock to occur?

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 62


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

2.7 Deadlock Handling Mechanisms


✓ If deadlock happens how does the operating system has to solve the problem? In this
activity, we will discuss the four strategies of handling deadlock. But, different operating
systems provide similar kind of solution.
Four different strategies can be used towards overcoming deadlocks but each with their own
corresponding advantages and drawbacks. These are:
✓ Ignoring: ignore the problem altogether and pretend that deadlock has never occur in the
system.
✓ Detection and recovery : make the system enter into a deadlock, detect the deadlock
and devise actions to recover from the situation
✓ Avoidance : carefully allocate resources to make sure no process will encounter a
deadlock situation
✓ Prevention: structurally negate one of the four conditions that results a deadlock and
prevent the occurrence. Let’s discuss each briefly

The Ostrich Algorithm

✓ Ignore and pretend that a deadlock never happened in the system. This method is the
simplest and used by most operating systems, including Linux, Windows. These operating
systems use this method by assuming infrequent deadlock is appreciated by users than a
restricting rule that forces all users to only one of everything including process and open
file.
✓ A trade-off between convenience and correctness has to be made though which is more
important and to whom is a serious point to consider. In such systems, application
developer must write programs that handle deadlocks.

Deadlock Detection and Recovery


Detection
✓ In this method, the system uses neither deadlock avoidance nor prevention but allows
deadlocks to happen. It then tries to detect when the deadlock happens and take actions
to overcome the problem after it happened. Such systems employ deadlock detection and
recovery algorithms that tracks resource allocation and process states, and rolls back and
restarts one or more of the processes in order to remove the deadlock. Detecting a
deadlock that has already occurred is easily possible since the resources that each
process has locked and/or currently requested are known to the resource scheduler or
OS. Let’s discuss some of the several deadlock detection as well as deadlock recovery
methods re available.

Deadlock detection with one resource of each type


❖ In such cases, systems with two instances of same resource are excluded and the
system has only one instance of each resource type: one CD recorder, one Printer, and
so on. If there is one or more cycle on the resource allocation graph of such system, it
indicates a deadlock and any process within the cycle is deadlocked.

❖ Periodically invoking an algorithm to search for a cycle in the resource allocation graph
enables the system to identify a deadlock. An algorithm to detect a cycle in a graph
requires an order of n2 operations, where n is the number of vertices in the graph.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 63


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

❖ Several such algorithms exist among which we consider the simplest one. This
algorithm defines a single data structure with list of nodes. The algorithm inspects the
graph and terminates either when a cycle is found or has shown none exists by marking
the arcs already inspected in the graph to avoid repetitive inspection. The algorithm
operates in the following manner for each node N, in the graph

➢ Initialize list of nodes, L, to a blank list and label each of the arcs as unmarked
➢ Append a new node to the tail of L and examine if this node now appears twice
in L. if it does, the algorithm exits as a cycle is identified.
➢ Inspect for any unmarked outgoing arcs from the given node and if so goto d and
if not proceed to e
➢ Randomly pick one of the outgoing unmarked arc and mark it. Then follow it to
the new current node and go to c
➢ This is a dead end and should be removed. Go to the node that was current
node prior to this one and label it current node then jump to step c. if this node
happens to be the initial node, then the diagram has no cycle and the algorithm
terminates
❖ Let’s see how the algorithm operates with example. Let’s take a system consisting of
seven processes and six resources as shown in figure 2.13 below.

Figure 2.13 (a) resource allocation graph Figure 2.13 (a) Cycle extracted from (a)

✓ Let’s start inspecting left to right and top to bottom as any node can be picked
arbitrarily. Let’s move resource R to the empty list L and initialize it. The only possible
move is to A which adds process A to the list making L={R, A}. Now move to S and L
is updated to be {R, A, S}. S is a dead end with no outgoing arcs forcing us to move
back to process A which in turn has no unmarked arcs that makes us retreat back to
R and finish examining node R. now, the algorithm restarts for another node, say A
emptying L. since this path has already been inspected, no need to trace it again for
us and the algorithm also terminates instantly because of the presence of only a single
arc from A to S. let’s start from B and follow outgoing arcs till we arrive at D which
makes L to be {B,T,E,V,G,U,D}. S or T can be taken randomly afterwards. If S is taken,
this will lead to a dead end causing to move back to D. if T is picked, it will cause the
algorithm terminate as this will make T to be found in L twice resulting in a cycle.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 64


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Deadlock detection with several instances of resource type


✓ Different deadlock detection mechanism is required when more than one instance of
a resource is found in a system. A matrix-based deadlock detection algorithm can be
used for n processes and m resource classes with Ei resources of class i (1<i< m). E
is the existing resource vector which gives the total number of instances of each
resource type found in the system.
✓ Let A be the available resource vector which gives the number of resource instances
currently unassigned. Let R and C be request matrix and current allocation matrix
respectively. How many instances of a resource class is currently held by process Pi
is represented in the ith row of C. thus Cij represents the number of resource instances
j held by process i while Rij depicts the number of resource instances j required by
process Pi. After defining these four vectors, the algorithm works by comparing the
vectors as follows:
➢ Look for any unmarked process Pi for which the ith row of R is less than or equal
to A
➢ Add the ith row of C to A, if such a process exists, note the process and return
back to step a
➢ If not, exit

✓ Initially, all the processes are unmarked but will be marked as the algorithm proceeds
to indicate the processes are completed concluding they are not deadlocked. If any
process remains unmarked when the algorithm finishes, such process is said to be
deadlocked. The Algorithm requires an order of m x n2 operations to detect whether
the system is in a deadlocked state.
✓ To illustrate the algorithm, consider an example system with three processes, P1 to
P3 and four resource classes, R1 to R4 with 4, 2, 3 and 1 instance respectively. The
number of instances of each resource class E is {4,2,3,1} and the available instances
A is {2,1,0,0}. Assume P1 has 1 R3, P2 has 2 R1 and 1 R4, P3 has 1 R2 and 2 R3 and
each process also requests for additional resources as can be seen from the below C
and R matrices respectively. .

✓ The algorithm looks first for a process whose request can be provided. P1’s request
can’t be fulfilled since there is no more instance of R4 in A. P2’s request can’t also be
granted as no instance of R3 is found in A. P3 is thus the only process that can be
given the current request as both of its needed additional resources (2 R1 and 1 R2)
are available in A. so P3 completes its execution and returns back all its resources
making A be {2, 2, 2, 0}.
✓ Now P2 gets a chance to run as its requests can be granted and finally making A to
be {4, 2, 2, 1}. Finally P1 gets what it has asked for implying occurrence of no deadlock.
However, if we make a little modification on the requests made by P2 asking for 2 R1,
1 R2, and 1 R4, the entire system ends up being deadlocked as none of the requests
of all the processes can be satisfied.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 65


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

✓ After identifying the means through which deadlock can be determined in a system,
when and how often to invoke the algorithms should be a question to be answered and
the answer to these questions, of course, depends on how often a deadlock is likely to
occur and how many processes will need to be rolled back.
✓ Generally, one possibility could be to detect the deadlock as early as possible by
running the algorithm with each resource request. This, however, costs the system
CPU time. Alternatively, the algorithm can also be invoked every k minutes or when
the CPU utilization becomes lower than some pre-set threshold value.

Recovery

✓ Once, the detection algorithm has succeeded in identifying a deadlock, several ways
exist to overcome the situation. One alternative can be through notifying the operator
about the deadlock and let the operator handle it manually. Another possibility can be
letting the system recover from the deadlock automatically. Two different approaches
are used to recover automatically: pre-emption and abortion.

Recovery through resource pre-emption

✓ This method works by temporarily taking away a resource from its current owner
process and gives it to another process and finally returns back to the owner. i.e.
successively preempt some resources from processes and give these resources to
other processes until the deadlock cycle is broken. Three important issues need to be
addressed if to use this method
➢ Victim: which resources and which processes are to be pre-empted? Cost
minimization must be considered while ordering the pre-emption. The number
of resources a deadlock process is holding and the time thus far used by the
deadlocked process during its execution can be taken as cost factors.
➢ Rollback: if a process is pre-empted a resource, what is the fate of this
process? Obviously, the normal execution of the process can’t proceed as it
lacks some required resources. Thus, the process must be rolled back to some
safe state and restarted from that state after the deadlock is solved. Since, in
general, it is difficult to determine what a safe state is, the simplest solution
could be a total rollback.
➢ Starvation: how can we guarantee that resources will not always be pre-
empted from the same process? Same process may always be picked as victim
which leads to starvation. Including the number of rollback in the cost factor can
be a solution to this problem.
✓ This recovery method is difficult, if not impossible, as picking a process to pre-empt is
highly dependent on the ease of taking back resources now held.

Recovery through process termination

✓ Killing one or more processes in a system may help to recover from deadlocks. Choose
a victim process from the cycle and abort it so that other processes continue running
by using the freed resources. This process can be repeated several times in the cycle
till breaking the cycle is possible. A process outside a cycle can also be picked as a

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 66


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

prey process where the resources hold by it is needed by one or more deadlocked
processes.
✓ For example two processes are running one holding a printer and requesting a CD
reader while the other one using a CD reader and waiting for a printer. The two
processes end up being deadlocked. If there is a third process with another identical
printer and CD reader resource granted, killing this process makes the resources free
which can then be allocated to the two processes solving the deadlock situation.
✓ Killing a process may not be easy. If the process was in the midst of updating a file or
printing data on a printer, terminating it will leave the file in an incorrect state and the
system needs to reset the printer to its correct state before printing the next job. Factors
such as the priority of the process, how many more resources are required to complete
the process, etc. affect the process chosen as a victim. In general, It is best if the
process chosen as a victim is a process that can resume from the beginning with no
bad effect or the termination cost is minimal.

Deadlock Avoidance

✓ This is a conservative approach to handling deadlock that requires priori information


about the maximum number of resources of each type that a process will ever request
for in its lifetime, the maximum claim. This method is the simplest and most useful
model which dynamically examines the resource-allocation state to ensure that there
can never be a circular-wait condition. Resource-allocation state is defined by the
number of available and allocated resources, and the maximum demands of the
processes.
✓ The idea is to ensure that a system consisting of a number of interacting processes
remain in a safe state. A safe state is a situation where even if every single process
simultaneously makes its maximum claim for resources at the same time, deadlock will
still not occur and there is some scheduling arrangement to allow every process run
and finish its execution. More formally, a system is in a safe state only if there exists a
safe sequence of processes <P1 , P2, ... , Pn> for the current allocation state. That is,
for each Pi, the resource requests that Pi can still make can be satisfied by the currently
available resources plus the resources held by all Pj, with j <i. In this situation, if the
resources that Pi needs are not immediately available, then Pi can wait until all Pj have
finished. When they have finished, Pi can obtain all of its needed resources, complete
its designated task, return its allocated resources, and terminate.
✓ When Pi terminates, Pi+l can obtain its needed resources, and so on. If no such
sequence exists, then the system is said to be in unsafe state. Let’s see with example
safe and unsafe states.
✓ Consider a system with 10 instances of one resource type and three different
processes P1, P2 and P3. P1 currently is using 3 instances of the resource but requires
6 more instances, P2 has 2 instances being in use and needs 2 more while P3 also
has 2 instances now and needs 5 more as shown in table 2.3. below
Process Currently Maximum
has requiremen
t

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 67


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

P1 3 9

P2 2 4

P3 2 7
Table 2.3. Resource
allocation and maximum claim

✓ From this, only 3 instances of the resource are available to be given to one of the
processes. The system is in a safe state time t0 with <P2, P3, P1> process sequence
as the scheduler can immediately allocate the resource to P2 (since it requires only 2
more instances leaving only 1 instance free) and finish it which will then make all the
4 instances available to be used. Then with 5 instances at hand, the system can now
schedule P3 to run to completion using all the available instances. Finally P1 gets the
chance to run as 7 instances are available and P1 only needs 6 of them. With this
careful scheduling, the system is able to avoid deadlock making it safe.
✓ On the other hand, assume at time t1, P1 requests one more instance and is granted
making the total resource instances occupied by P1 4 and all the others as described
on table 4.1 above. Now the system has only 2 instances free. With these, P2 can run
safely using both leftover instances. After P2’s completion, the system will have 4 free
instances to be given to one of the processes. However, both P1 and P3 require 5
instances to run which makes the scheduler get stuck as there is not enough free
resource available. Hence, the two processes cannot get to completion moving the
system to unsafe state.
✓ Of course, deadlock is not guaranteed to occur in an unsafe state, but should the worst
case scenario arise, then deadlock will definitely occur.
✓ A scheduling algorithm used to avoid such deadlock situation is known as the
Banker’s Algorithm which dynamically examines the resource-allocation state, which
is defined by the number of available and allocated resources and the maximum
demands of the processes, to ensure that a circular-wait condition can never exist. The
name was chosen because the algorithm could be used in a banking system to ensure
that the bank never allocated its available cash in such a way that it could no longer
satisfy the needs of all its customers.
✓ In this algorithm, the goal is not to entertain any request that can change the safe state
to unsafe one. Whenever a request comes for a resource, the algorithm checks if
fulfilling this request may result to unsafe state or not. If so, the request is postponed
until later but otherwise granted.
✓ This means the OS must always identify the process with the smallest number of
remaining resources and make sure that the current number of remaining resources is
always equal to, or greater than, the number needed for this process to run to
completion. The Banker’s algorithm works similar for both single and several resource
types .

For the Banker’s algorithm to work, it needs to know three things:

➢ How much of each resource each process could possibly request?


➢ How much of each resource each process is currently holding?
➢ How much of each resource the system has available?

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 68


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Given n processes and m resource types in a system, the data structures required to
implement the Banker’s algorithm are as follows:

➢ A: a vector specifying the available resources


➢ P: a vector of possessed resources by processes
➢ E: a vector of the existing instances of resources
The algorithm can now be stated as:

➢ Find a process P in the n by m matrix whose number of resource request rejections


are smaller or equal to A. if it is impossible to find one, the system will eventually
go to deadlock since no process can be brought to completion.

➢ If more than one process is obtained, the choice is arbitrary. Assume this process
has requested all the needed resources and finishes its execution. Mark the
process terminated and update vector A by adding all the resources returned back
by this process

❖ Repeat these two steps until either all the processes in the system comes to
termination, which shows safety of the initial state, or until a deadlock happens in which
case it was not. Like most algorithms, the Banker’s algorithm involves some trade-offs.
The required number of resources by a process needs to be known for the algorithm
to operate which is unavailable in most systems, making the Banker’s algorithm
useless.

❖ Moreover, it is also unrealistic to assume static number of processes in a system as


this number varies dynamically in most systems. The requirement that a process will
eventually release all its resources on termination is insufficient for a practical system
as waiting for hours even days for the resources to be released is unacceptable. In
conclusion, there are few, if any, currently existing systems using this algorithm to
avoid deadlocks.

Deadlock Prevention

✓ Deadlock avoidance is almost impossible in real systems and as discussed previously,


for a deadlock to occur, each of the four necessary conditions must hold. By ensuring
that at least one of the four necessary conditions cannot hold, a system can prevent
the occurrence of a deadlock. Let’s elaborate on this approach by examining each of
the four conditions separately.

Mutual exclusion

✓ Sharable resources, like Read-only files do not require mutually exclusive access
making them out of a deadlock situation. If several processes attempt to open a read-
only file at the same time, they can be granted simultaneous access to the file as a
process never needs to wait for a sharable resource.
✓ The mutual exclusion condition, however, must hold for non-sharable resources like
printer that cannot be simultaneously shared by several processes .Algorithms that
avoid mutual exclusion are called non-blocking synchronization algorithms. In general,
since some resources are inherently non-sharable, deadlocks cannot be avoided by
denying the mutual exclusion condition.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 69


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Hold and wait

✓ To ensure the hold and wait condition never happens in a system, we must guarantee
that whenever a process requests a resource, it does not hold any other resources.
This requires process to request and be allocated all its resources before it begins
execution which can be implemented by requiring that system calls requesting
resources for a process precede all other system calls.
✓ Alternatively, this can be avoided by allowing a process to request resources only when
the process has none. A process may request some resources and use them. To
request for another additional resource, however, it must release all the resources that
it is currently allocated.
✓ This method has two main drawbacks:
➢ Low resource utilization since many of the resources may be allocated but unused
for a long period of time and;
➢ starvation as a process that needs several popular resources may have to wait
indefinitely.
No pre-emption

✓ The third necessary condition for occurrence of a deadlock is that there be no pre-
emption of resources that have been allocated. To ensure that this condition does not
hold, the following algorithm can be used.
✓ If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released. The
pre-empted resources are then added to the list of resources for which the process is
waiting for and the process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting.
✓ This protocol is often applied to resources whose state can be easily saved and
restored later such as CPU registers and memory space. It cannot generally be applied
to such resources as printer and tape drives.
✓ If a process had been given the printer and is printing its output, taking away the printer
forcibly because another resource requested by the process could not be granted is a
complicated task if not impossible .Attacking this condition is even more difficult than
the previous two.

Circular wait condition

✓ The last condition leading to a deadlock is the circular wait. This can be avoided by
letting processes to wait but not in a circular fashion. One way to achieve this could be
through assigning a global precedence number to resources and force processes to
request for resources according to increasing precedence.
✓ That is, if a process holds some resources and the highest precedence of these
resources is m, then this process cannot request any resource with ordering smaller
than m. This forces resource allocation to follow a particular and non-circular ordering,
so circular wait cannot occur.
✓ Alternatively, a system can set a rule specifying a process is entitled to only a single
resource at any time. This allows holding only one resource per process and if a
process requests another resource, it must first free the one it’s currently holding (or
hold-and-wait).

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 70


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Self-Evaluation
Answer the following questions by providing yur answer on the space provided:
1. What is deadlock? How can it occur?
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________

2. Give an example of an interrupt request. Explain when will it occur?


_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 71


LEARNING MODULE SURIGAO STATE COLLEGE OF TECHNOLOGY

Review of Concepts:
✓ This unit is centered on the concept of Processes in OS. A process is a program in
execution. As a process executes, it changes state as new, ready, running, waiting,
or terminated. Threads are basic unit of CPU utilization, each consisting of their own
program counter, a stack, a set of registers, and a thread ID but share the address
space of the process they are in. the three different implementation modes of a
thread are user space, kernel or hybrid of these.
✓ This unit also treated two most important concepts of process management:
scheduling and deadlock. Scheduling is the means by which the operating system
picks processes out of the job queue and put them in a ready queue so that they can
be executed. Two types of schedulers are available in a system that performs this
operation: short-term scheduler and long-term scheduler.
✓ The short-term scheduler selects from among the processes in memory that are
ready to execute and allocate the CPU to one of them.
✓ The long-term scheduler also known as the dispatcher performs context switching
and gives control of the CPU to the process picked by the short-term scheduler.
Scheduling decisions are required when a process switches from
➢ Running to waiting state
➢ Running to ready state
➢ Waiting to ready state or
➢ Terminates
✓ Scheduling criteria varies based on the system type but generally the
focus is optimization which can be achieved through maximum CPU
utilization, maximum throughput, minimum turnaround time, minimum
waiting time and minimum response time. Algorithms that implements
Pre-emptive and non-pre-emptive scheduling policies are used by the
scheduler to guarantee these.
✓ Mutual exclusion, hold and wait, no pre-emption and circular wait are
the four conditions that must hold true simultaneously for a deadlock
to happen. Some systems, however, implement deadlock detection
and recovery methods while others also use prevention and
avoidance algorithms to overcome this deadlock situation and
maintain a safe state by ordering the execution of processes carefully.

References: William Korir, Introduction to Operating Systems,2017, Published under


http://en.wikipedia.org/wiki/Creative_Commons; Required readings and other resources:
Andrew S. Tanenbaum (2008). Modern Operating System. 3rd Edition. Prentice-Hall; A
Silberschatz, Peter B Galvin, G Gagne (2009). Operating System Concepts. 8th Edition. Wiley.

CPE 373- OPERATING SYSTEMS (GRACECHELL M. PASCUA) 72

You might also like