Operating System Learning Outcome 2
Operating System Learning Outcome 2
One can view a process from two separate and potentially independent concepts: one relating to
resource ownership and the other one relating to execution (Stallings, 2014). Resource ownership
is where a process includes a virtual address space to hold the process image (a collection of
data, program, stack and attributes defined in the PCB). A process controls or owns resources
such as main memory, I/O channels, I/O devices and data for a while during execution.
Execution of a process entails following an execution path through one or more programs. This
execution can be connected to other processes. It is evident that a process requires a resource
during execution and in a computer system there are more than one process being executed thus
the need to understand process management.
Process state
When a program is loaded into the memory and it becomes a process, it can be divided into four
Stack contains temporary data such as method/function parameters and return address; Heap is a
dynamically allocated memory to a process during its runtime; Text contains current activity
represented by the value of PC (Program Counter) and contents of the processor‟s registers; Data
contains global and static variables
A process exists in various states such as New, Ready, Running, Waiting, Halted
Program counter
It indicates the address of the next instruction to be executed for that particular process.
CPU registers
Accumulators, index registers, general-purpose registers, stack pointers comprise the CPU
register. In the event an interrupt occurs CPU registers and program counter information must be
saved to allow a process to be continued correctly
It contains scheduling parameters such as process priority, pointers to scheduling queues etc.
It contains information such as limit registers, value of the base and the page tables/segment
tables.
Accounting information
It contains the amount of CPU and real time used, account numbers, time limits, job or process
numbers etc.
Process State
Process ID
Program Counter
Registers
Memory limits
………...
Process state
A process/task is created for a program that needs to be executed. The CPU executes instructions
in sequence dictated by the changing values in the program counter register. A trace is referred
to as listing the sequence of instructions that execute for that particular process. The life of a
process is bounded by its creation and termination. As a process executes, it changes state as
defined by the current activity of that process. The changing process states of a typical activity
from creation to termination is as shown in figure. Typically, activities can be removed from
memory when they are in the waiting, stopped or terminated states.
It is important to note that only one process can be running on any processor at any instant even
though processes may be ready and waiting. A process may exist in one or more of the following
states:
New: This is the state where a process is created. It is the transient state when the activity has
just begun.
Running: This state entails execution of instructions. Activities execute until they are
interrupted by another activity or user command. It is also referred to as Resumed state.
Waiting: It is a stop for an activity that is interrupted and ready to go into background mode.
The process is waiting for some event to occur for example I/O completion, or user starts another
application.
Ready: The process is waiting to be assigned a processor
Terminated: The process has completed the execution. Ideally the activities in this state
disappeared from the user‟s view.
New Terminated
Exit
Admitted
Interrupt
Ready Running
Schedular dispatch
Input/Output
Input/Output or
or event wait
event completion
Waiting
Concurrency control and control types
Inter-process communication
Inter-process communication (IPC) facility provides a mechanism to allow processes to
communicate and to synchronize their actions to prevent Race conditions (a condition where
several processes access and manipulate the same data concurrently and the outcome of the
execution depends on the particular order in which the access takes place).
To guard against this; certain mechanisms are necessary to ensure that only one process at a time
can be manipulating the data. The mechanisms:
It is a segment of code in which a process may be changing common variables, writing a file,
updating a table etc. Each process has its critical section while the other portion is referred to as
Reminder section.
The important feature of the system is that when one has process is executing in its critical
section no other process is to be allowed to execute its critical section. Each process must request
permission to enter its critical section. A solution to critical section problem must satisfy the
following:
Mutual Exclusion ensures that if a process is executing its critical section, then no other
process can be executing in their critical section.
Progress ensures that if no process is executing in its critical section and that there exist
some processes that wish to enter critical sections then those processes that are not executing
in their remainder section can be allowed.
Bounded Waiting ensures there must exist a bound on the number of times that other
processes are allowed to enter their critical sections after a process has made a request to
enter its critical section and before that request is granted. This helps prevent Busy waiting.
NB: Busy waiting occurs when one process is being executed in its critical section and
process 2 tests to confirm if process 1 is through. It is only acceptable technique when the
anticipated waits are brief; otherwise it could waste CPU cycles.
Techniques used to handle Critical section problem (Synchronization)
1. Semaphores
The main principle behind Semaphores is that two or more processes can operate by means of
simple signals such that a process can be forced to stop at a specified place until it has received a
special signal. Any complex coordinative requirement can be satisfied by the appropriate
structure of signals. For signaling special variables called semaphores are used.
To transmit a signal via semaphores, a process executes the primitive wait; if the corresponding
signal has not yet been transmitted the process is suspended until transmission takes place.
A semaphore can be viewed as a variable that has an integer value upon which three main
operations are defined: -
A binary semaphore accepts only two values 0 or 1. For both semaphore and binary semaphore a
queue is used to hold processes waiting on the semaphore. A strong semaphore has a mechanism
of removing processes from a queue e.g. using First In First Out (FIFO) policy. A weak
semaphore doesn‟t specify the order in which processes are removed from the queue. Example of
implementation:
2. Message passing
Is a technique that provides a means for co-operating processes to communicate when they are
not using shared memory environment. System calls are used by processes generally to send and
receive messages such as: send (receiver process; message) or Receive (sender process;
message).
A blocking send must wait for receiver to receive the message. A non-blocking send enables the
sender to continue with other processing even if the receiver has not yet received the message.
This requires buffering mechanisms to hold messages until the receiver receives it.
Acknowledgment protocols are used in distributed systems to pass message since
communication can be flawed sometimes. However, on some personal computers passing can be
flawless.
One complication in distributed systems with send / receive message passing is in naming
processes unambiguously so that send and receive calls references the proper process. Process
creation and destruction can be coordinated through some centralized naming mechanisms but
this can introduce considerable transmission overhead as individual machines request permission
to use new names.
3. Monitors
4. Event Counters
It is an integer counter that does not decrease. Introduced to enable process synchronization
without use of mutual exclusion. It keeps track of number of occurrences of events of a particular
class of related events. Some of the operations that allow processes to reference event counts
include; Advance event, Read event and Await event values.
Advance event signals the occurrence of an event of the class of events represented by
incrementing event count by 1.
Read event obtains value of E; because Advance (E) operations may be occurring during Read
(E) it is only guaranteed that the value will be at least as great as E was before the read started.
Await event blocks the process until the value of E becomes at least V; this avoids the need for
busy waiting.
Process scheduling
Process scheduling is required to enable the CPU to execute one process at a time. There are
various types of scheduler modules in the OS that execute at their appropriate time. Process
scheduling is the process of switching the CPU among processes consequently making the
computer more productive.
It is the basis of multi programmed Operating System. Normally several processes are kept in
memory and when one process has to wait, the OS takes the CPU away from that process and
gives the CPU to another process. The process scheduling modules of the OS include: -
CPU Scheduler
CPU dispatcher
CPU Schedular
The schedular selects a process from the Ready queue that is to be executed. Scheduling
decisions may take place under the following conditions:
(i) Non pre-emptive scheme where the process keeps CPU until when it releases it.
(ii) Pre-emptive scheme where the CPU can be taken from a process when it is in the
middle of updating or processing data by another process.
Types of schedulers
The schedulers are classified according to their frequency of their use in the system. When a
schedular is invoked frequently it is referred to as short-term schedular while when the use is
after a long time it is referred to as long-term. Operating system has many schedulers but the
three main schedulers are: -
SHORT-TERM SCHEDULER
The main objective of this type of schedular is to maximize CPU utilization. It allocates
processes in the Ready queue to CPU for immediate processing. It works more frequently and
faster because it selects a process quite often and executes the process only for a few seconds
before it goes for input output operations.
MEDIUM-TERM
Occasionally the OS need to remove a process from the main memory as this process may be in
need of some I/O operation. So, such processes may be removed (or suspended) from the main
memory to the hard disk. Later on, these processes can be reloaded into the main memory and
continued from where it was left earlier. This saving of suspended process is said to be rolled-
out. And this swapping (in and out) is done by the medium-term scheduler.
LONG-TERM SCHEDULER
This scheduler is responsible for selecting the process from secondary storage device like a disk
and loads them into the main memory for execution. At is also known as a job scheduler. The
primary objective of long-term scheduler is to provide a balance of mix of job to increase the
number of processes in a ready queue. Long term scheduler provides a good performance by
selecting processes with a combination of I/O and CPU bound type. Long-term scheduler selects
the jobs from the batch-queue and loads them into the memory. In memory, these processes
belong to ready queue.
CPU Dispatcher
CPU dispatcher gives control of CPU to the process selected by the scheduler. Its functions
include: -
Scheduling levels
a) High level scheduling (Job Scheduling) – determines which jobs should be allowed to
compete actively for the resources of the system. Also called Admission Scheduling.
Once admitted jobs become processes or groups of processes.
b) Intermediate level scheduling – determines which processes shall be allowed to compete
for the CPU.
c) Low level scheduling – determines which ready process will be assigned the CPU when it
next becomes available and actual assigns the CPU to this process.
Various CPU scheduling algorithms are available, thus in determining which is the best is for a
particular situation, the following criteria have been suggested. They include:
Preemptive scheduling is based on priority where a scheduler may preempt a low priority
running process anytime a high priority process enters into a ready state.
When a method switches from running state to ready state or from waiting state to ready state,
preemptive scheduling is used. That process stays in ready queue till it gets next chance to
execute. Algorithms based on preemptive scheduling include: Shortest Remaining Time First
(SRTF), Priority (preemptive version), Round Robin (RR).
1. First Come First Served (FCFS) / First in First Out (FIFO) Scheduling
It is the process that request CPU first and is allocated the CPU first. Code for FCFS is simple to
write and understand. Under FCFS Average waiting time is often quite long. FCFS is non-
preemptive discipline (once a process has the CPU it runs to completion).
Example
Processes arrive at time 0, with length of the CPU-burst time given in milliseconds.
Table 2: FCFS
Process Burst Time
P2 3
P3 3
P1 P2 P3
0 24 27 30
0 3 6 30
From example one, the average waiting time under FCFS policy is generally not minimal and
may vary substantially if processes CPU-Burst times vary greatly. FCFS algorithm is not ideal
for time sharing systems.
Example
Table 3: SJF
Process Burst Time
(ms)
P1 6
P2 8
P3 7
P4 3
P4 P1 P3 P2
0 3 9 16 24
The main disadvantage of SJF is knowing the next CPU request and this information is not
usually available. NB: SJF is non-preemptive in nature.
Example
Table 4: SRT
Process Arrival Time Burst Time
(ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 0 + (10 – 1) = 9
P2 (1 – 1) = 0
P3 17 – 2 = 15
P4 5 – 3 =2
P1 is started at time 0 since it‟s the only process in the queue. P2 arrives at time 1. The remaining
time for P1 (7ms) is larger than time for process P2 (4ms) so P1 is preempted and P2is
scheduled. Average waiting time is less as compared to non-preemptive SJF.
SRT has higher overhead than SJF since it must keep track of elapsed service time of the running
job and must handle occasional preemptions.
0 4 8 12 16 20 24 25 26
P1 0 + (16 – 4) = 12
P2 4–1=3
RR is effective in time sharing environments. The pre-emptive overheads are kept low by
efficient context switching mechanisms and providing adequate memory for the processes to
reside in main storage.
5. Priority Scheduling
SJF is a special case of general priority scheduling algorithms. It is the most common scheduling
algorithms in batch systems. A priority is associated with each process and CPU is allocated to
the process with highest priority. Equal priority processes are scheduled in FCFS order. Priorities
are generally some fixed range of numbers e.g. 0 – 7 or 0 – 4095. Some system uses low
numbers to represent low priority; others use low numbers for high priority. Priorities can be
defined either internationally or externally.
a) For internally defined priorities the following measurable quantities are used – time
limits, memory requirements, no of open files, ration of average I/O burst to CPU burst.
b) Externally defined priorities are set by criteria that are eternal to o/s e.g. importance of
process, type and amount of funds being paid for computer use, department sponsoring
the work and other political factors.
Priority scheduling can either be preemptive or non-preemptive. Major problem is indefinite
blocking or starvation: low priority jobs may wait indefinitely for CPU. Aging technique is used
to gradually increase the priority of process that waits in the system, for a long time.
For example, consider the Processes Pa, Pb, Pc and Pd with the following priority and execution
time.
Pd Pb Pa Pc
0 6 9 14 22
DEADLOCKS
Occurs when several processes compete for a finite number of resources i.e. a process is waiting
for a particular event that will not occur. The event here may be resource acquisition and release.
Example of Deadlocks
R1
Held by Request
P
P
R2 Held by
Request
This system is deadlocked because each process holds a resource being requested by the other
process and neither process is willing to release the resource it holds. This leads to deadlock.
A spooling system is used to improve system throughput by disassociating, a program from the
slow operating speeds of devices such as printers e.g. lines of text are sent to a disk before
printing starts. If disk space is small and jobs are many a dead lock may occur.
Deadlocks Characterization
1. Mutual Exclusion condition – a process claims exclusive control of resources they require.
2. Wait for Condition (Hold and Wait) –Processes hold resources already allocated to them
while waiting for additional resources.
3. No preemption condition – resources cannot be removed from the processes holding them
until the resources are used to completion.
4. Circular wait condition – A circular chain of processes exists in which each process holds
one or more resources that are requested by the next process in the chain
Major areas of deadlock research in computing
a) Deadlock Detection
b) Deadlock Recovery
c) Deadlock Prevention
d) Deadlock Avoidance
Deadlock detection
This is the process of determining that a deadlock exists and of identifying the processes
involved in the deadlock i.e. determine if a circular wait exists. To facilitate detection of
deadlocks resource allocation graphs are used which indicate resource allocation and requests.
These graphs change as process request resources, acquire them and eventually release them to
the OS.
Reduction of Resource Allocation Graphs is a technique used for detecting deadlocks i.e.
processes that may complete their execution and the processes that will remain deadlocked are
determined. If a process‟s resource request may be granted then we say that a graph may be
reduced by that process (arrows connecting process and resource is removed).
If a graph can be reduced by all its processes then the irreducible processes constitute the set of
deadlocked processes in the graph.
NB: - the order in which the graph reductions are performed does not matter; the final result will
always be the same.
P1 R1
P1 is requesting a resource of type R1
R2
P2
A resource of type R2 has been allocated
to process P2
P3
R3 Process P3 is requesting resource R3 which
P4 has been allocated to process P4
R3
R3
Given the resource allocation graph can you determine the possibility of a deadlock?
R6
I. Reducing by P9
R6
P8
P8
R7 R7
P9
II.P7Reducing by P7 P9
P7
III. Reducing by P8
R6 R6
P8 P8
R7 R7
P7 P9 P7 P9
NB: Deadlock detection algorithm should be invoked at less frequent intervals to reduce
overhead in computation time. If it is invoked at arbitrary points there may be many cycles in
resource graph and it would be difficult to tell which of the many deadlocked processes “caused”
the deadlock.
Deadlock recovery
a) Ostrich Algorithm (Bury head in the sand and assume things would just work out).
b) Informing the operator who will deal with it manually. Let the system recover
automatically.
There are 2 options for breaking a deadlock:
Aims at getting rid of conditions that cause deadlock. Methods used include:
Denying Mutual exclusion – should only be allowed on non-shareable resources. For
shareable resources e.g. read only files the processes‟ attempting to open it should be
granted simultaneous access. Thus, for shareable we do not allow mutual exclusion.
Denying Hold and wait (wait for condition) – To deny this we must guarantee that
whenever a process requests a resource it does not hold any other resources. Protocols
used include:
i) Requires each process to request and be allocated all its resources before it begins
execution. While waiting for resources to be available it should not hold any
resource – may lead to serious waste or resources.
ii) A process only requests for a resource when it has none or it has to release all
resources before getting a resource.
Disadvantages of these protocols
Starvation is possible – A process that needs several popular resources may have to wait
indefinitely because at least one of the resources that it needs is always allocated to some
other process.
Resource utilization is low since many of the resources may be allocated but unused for a
long period.
Denying “No preemption” - If a process that is holding some resource requests another
resource that cannot be immediately allocated to it (i.e. it must wait) then all resources
currently being held are preempted. The process will be started only when it can regain
its old resources as well as the new ones that it is requesting.
Denying Circular wait - All resources are uniquely numbered and processes must request
resources in linear ascending order. It has been implemented in many OS but has some
difficulties i.e.
- Addition of new resources required rewriting of existing program so as to give the
unique numbers.
- Jobs requiring resources in a different order from that one implemented by o/s; it
required resources to be acquired and held possibly long before they are actually
used (leads to waste)
- Affects a user‟s ability to freely and easily write applications code.
Deadlock avoidance
Dijkstra‟s Bankers Algorithm is the widely used deadlock avoidance technique. Its goal is to
improve less stringent (constraining) conditions than in deadlock prevention in an attempt to get
better resource utilization. Avoidance does not precondition the system to remove all possibility
of deadlock to loom, but whenever a deadlock is approached it is carefully side-stepped.
NB: it is used for same type of resources e.g. tape drives or printers.
Dijkstra‟s Bankers Algorithm says allocation of a resource is only done when it results in a safe
state rather than in unsafe states. A safe state is only in which the total resource situation is such
that all users would eventually be able to finish. An unsafe state is one that might eventually lead
to a deadlock.
Assume a system with 12 equivalent tape drives and 3 users sharing the drives:
Table 6: State I
State I
The state is „safe‟ because it is still possible for all 3 users to finish i.e. the remaining 2 drives
may be given to users (B) who may run to completion after which six tapes would be released for
user (A) and user (C). Thus, the key to a state being safe is that there is at least one way for all
user to finish.
Table 7: State II
Users Current Maximum
Loan Need
User (A) 8 10
User (B) 2 5
User (C) 1 3
Available 1
A 3-way deadlock could occur if indeed each process needs to request at least one more drive
before releasing any drives to the pool.
NB: An unsafe state does not imply the existence of a deadlock of a deadlock. What an unsafe
state does imply is simply that some an unfortunate of events might lead to a deadlock.
User (B) 4 6
User (C) 6 8
Available 1
Therefore, State IV is not necessarily deadlocked but the state has gone from a safe one to an
unsafe one. Thus, Dijkstra‟s Bankers Algorithm, the mutual exclusion, wait-for and No-
preemption conditions are allowed but processes do not claim exclusive use of the resources they
require.
Some serious weaknesses that might cause a designer to choose another approach to the deadlock
problem.
Non-preemptive policy
Process with shortest expected processing time is selected next
Short process jumps ahead of longer processes
7.2.2.7 References
Abraham, P. a. (2013). Operating Systems Concepts. United States of America: Wiley.
Abraham, P. G. (2013). Operating Systems Concepts . Hoboken: John Wiley & Sons, Inc.
Maccabe, A., Bridges, P., Brightwell, R., & Riesen, R. (n.d.). Recent Trends in Operating
Systems and their Applicability to.