Module 2
Module 2
1
Notes for Operating Systems (16MCA24)
At any given point in time, while the program is executing, this process can be uniquely
characterized by a number of elements, including the following:
Identifier: A unique identifier associated with this process, to distinguish it from all
other processes.
State: If the process is currently executing, it is in the running state.
Priority: Priority level relative to other processes.
Program counter: The address of the next instruction in the program to be
executed.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 2
Notes for Operating Systems (16MCA24)
Memory pointers: Includes pointers to the program code and data associated with
this process, plus any memory blocks shared with other processes.
Context data: These are data that are present in registers in the processor while the
process is executing.
I/O status information: Includes outstanding I/O requests, I/O devices (e.g., disk
drives) assigned to this process, a list of files in use by the process, and so on.
Accounting information: May include the amount of processor time and clock time
used, time limits, account numbers, and so on.
The above information is stored in a data structure known as process control block as
shown in Figure 2.1. It is created and managed by OS.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 3
Notes for Operating Systems (16MCA24)
When a job (or process) enters a job pool, it will be in a not running state. Once it is ready
to execute and gets a processor, it will be into running state. When it has to wait for any
resources or for I/O, it will be paused and sent into not running state again. During this
time, another process may start running. Once the previous process is ready again, it will
be switched from not running to running. This task is shown in Figure 2.2(a).
In this model, there must be some information relating to each process like current state,
location in the memory etc. This will help OS to keep track of the process and is known as
process control block. Processes that are not running must be kept in a queue, waiting for
their turn to execute as shown in Figure 2.2(b). In this single queue, each entry is a pointer
to the process control block of a particular process. In other words, each block represents
one process here.
Process Creation: When a new process has to be added to the job pool, the OS builds
the data structures for managing this process and allocates address space in main
memory. These actions form the creation of a new process. The situations that will lead to
the process creation are listed in Table 2.1.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 4
Notes for Operating Systems (16MCA24)
Most of the times, the OS creates the processes depending on various situations. But,
sometimes, it creates a process based on the explicit request of another process. This is
called as process spawning. When one process spawns another, the first process is
called as parent process and the spawned process is called as child process.
Process Termination: Every process that is created will be terminated at some moment of
time. There are several reasons for a process to get terminated as listed in Table 2.2.
To avoid this problem, it is better to split not running state into two parts viz. ready and
blocked. This model, known as five-state model is shown in Figure 2.3. The five states of
this model are explained below:
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 5
Notes for Operating Systems (16MCA24)
Any process has to undergo transition between these states. The possible transitions are
listed below:
Null to New: A new process is created for execution. This event may occur due to
any reasons listed in Table 2.1.
New to Ready: The OS will move a process from New state to Ready when it is
ready to take additional process. Generally, there will be a limit for a number of
processes that can be kept on a queue so as to maintain the efficiency.
Ready to Running: A scheduler or dispatcher selects one ready process for
execution.
Running to Exit: Currently running process is terminated by OS. (Refer Table 2.2
for reasons for termination).
Running to Ready: Most of the OS have a limit for a time duration for which a
process can run continuously. After that, even if it is not completed, it has to leave
the processor for other waiting processes and has to enter a job queue.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 6
Notes for Operating Systems (16MCA24)
Running to Blocked: If a process requests for some resources, then it will be kept
in a blocked state.
Blocked to Ready: Once a process gets the requested resources and completes
the task using those resources, it can enter into ready state.
Ready to Exit: Though this state is not shown in the diagram, it may occur.
Sometimes, a child process may be in ready state, and its parent may exit. Then, the
child process also will be exited. Or, the parent itself may terminate the child.
Blocked to Exit: A process may not get the resources that it requested or it
exceeds the waiting time.
Figure 2.4 shows the queuing discipline maintained by processes with respect to the states
mentioned in Figure 2.3.
Assume an OS without virtual memory. Then all the processes to be executed must be
loaded into main memory. Most of the processes will require I/O and hence may get
blocked. As, I/O speed is much slower than that of processor, the processor would stay idle
again. Hence, when none of the processes in the main memory is in Ready state, the OS
can swap (move) one of the blocked processes into the disk. This will form a suspend
queue. Now, any other process can be loaded into the main memory and start execution.
As disk I/O is faster, the swapping will increase the performance. With the usage of
swapping, one more state will be added to the process behavior model as shown in Figure
2.5 (a).
When OS performs a swapping-out operation, it can load either a newly created process
into main memory or it can load a previously suspended process. But, a suspended
process might have been actually a blocked process (waiting for I/O). Hence, the OS has to
determine: Whether a process in suspended queue is
a process waiting on an event – that is, blocked or not
a process has been swapped out of main memory – that is suspended or not.
These states can be included in the state diagram as shown in Figure 2.5(b).
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 7
Notes for Operating Systems (16MCA24)
Some of the reason for process suspension is listed in Table 2.3. It also describes the
usages by suspending the process.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 8
Notes for Operating Systems (16MCA24)
Figure 2.6 Processes and Resources (Resource allocation at one moment of time)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 9
Notes for Operating Systems (16MCA24)
I/O Table: Used by OS to manage the I/O devices and the channels. At any given
moment of time, the OS needs to know
o Whether the I/O device is available or assigned
o The status of I/O operation
o The location in main memory being used as the source or destination of the
I/O transfer
File Table: Most of the times, these tables are maintained by file management
system. These tables provide information about:
o Existence of files
o Location on secondary memory
o Current Status
o Any other relevant attributes
Process Table: To manage processes the OS needs to know following details of the
processes
o Current state
o Process ID
o Location in memory
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 10
Notes for Operating Systems (16MCA24)
Process Location: Before checking where the process is located, we should understand
the process image. Process image is a collection of user program, data, stack and
process control block as shown in Table 2.4.
The process image is usually maintained as a contiguous block of memory and is stored on
the disk. As the entire process image has to be loaded into main memory for process
execution, the OS must know the exact location of process image on the disk.
The structure of location information can be understood using Figure 2.7. Here, the primary
process table has one entry for each process. Each entry has one pointer to a process
image.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 11
Notes for Operating Systems (16MCA24)
The OS and the important tables, process control blocks etc. must be protected from
interference of user program. Hence, two modes must be provided separately. Typical
functions of OS kernel are:
Process management
Memory management
I/O management
Support functions (like interrupt handling, accounting etc)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 13
Notes for Operating Systems (16MCA24)
2.5.3 Process-based OS
OS can be treated as collection of system processes as shown in Figure 2.9(c). As in the
other options, the software that is part of the kernel executes in a kernel mode. But, major
kernel functions are organized as separate processes.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 14
Notes for Operating Systems (16MCA24)
A key security issue in the design of any OS is to prevent anything (user or process) from
gaining unauthorized privileges on the system – especially from gaining root access.
Different types of threats to the OS and the respective countermeasures are discussed
below.
Malicious Software: These are very sophisticated types of threats that are
presented by programs and exploit vulnerabilities in the computing system.
2.6.2 Countermeasures
Following are the different ways of facing the security threats:
Intrusion Detection: Intrusion detection systems (IDS) are typically designed to
detect human intruder and malicious software behaviour. They may be host or
network based. Intrusion detection systems (IDS) typically comprise
o Sensors
o Analyzers
o User Interface
Authentication: User authentication is a basic building block for defense consisting
of two steps:
o Identification
o Verification
There are four ways of authenticating a user’s identity:
o Something the individual knows (like password)
o Something the individual possesses (like ID Card)
o Something the individual is (static biometrics like finger print, iris etc.)
o Something the individual does (dynamic biometrics like voice, gait etc.)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 15
Notes for Operating Systems (16MCA24)
2.7.1 Multithreading
The ability of OS to support multiple, concurrent paths of execution within a single process
is called as multithreading. The traditional approach of a single thread of execution per
process, in which the concept of a thread is not recognized, is referred to as a single-
threaded approach. MS-DOS is an example of OS which supports single process and
single thread. Some versions of UNIX support multiple processes, but single thread per
process. Java runtime environment supports multiple threads per single process. These are
depicted in Figure 2.10.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 16
Notes for Operating Systems (16MCA24)
Within a process, there may be one or more threads, each with the following:
An execution state (running, ready, etc.)
Saved thread context when not running
An execution stack
Some per-thread static storage for local variables
Access to the memory and resources of its process (all threads of a process share
this)
The difference between threads and processes from the view of process management is
shown in Figure 2.11. A single threaded process means there is no thread. In this model,
the process includes its control block, user address space and user/kernel stacks. In
multithreaded model, there will be a separate control block for each thread. And, all threads
of one process share the state and resources of that process.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 17
Notes for Operating Systems (16MCA24)
Thread States: The key states of threads are Running, Ready and Blocked. But, since all
the threads share the same address space of the process, they will be swapped out when a
process is swapped out. Hence, suspending a single thread makes no sense.
There are four basic thread operations associated with a change in thread state:
Spawn: When a new process is spawned, a thread for that process is also
spawned. Also, a thread within a process may spawn another thread within the
same process.
Block: When thread needs to wait for an event, it will be blocked.
Unblock: When an event for which a thread was blocked occurs, the thread is
unblocked and moved into ready queue.
Finish: When a thread completes, its registers and stacks are deallocated.
Thread Synchronization: All the threads of a process share the same address space and
other resources like files. So, any alteration of a resource by one thread affects the
behavior of other threads in the same process. Hence, it is necessary to synchronize the
activities of the various threads so that they do not interfere with each other.
User – level Threads: In ULT, all work of thread management is done by the application
and the kernel is not aware of the existence of threads. It is shown in Figure 2.12 (a). Any
application can be programmed to be multithreaded by using a threads library, which a
package of routines for ULT management.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 18
Notes for Operating Systems (16MCA24)
Usually, an application begins with a single thread and begins running in that thread. This
application and its thread are allocated to a single process managed by the kernel. The
application may spawn a new thread within the same process during its execution. But,
kernel is not aware of this activity.
Kernel – level Threads: In pure KLT model, all work of thread management is done by
the kernel. Thread management code will not be in the application level. This model is
shown in Figure 2.12(b). The kernel maintains context information for the process as a
whole and for individual threads within the process. So, there are certain advantages of
KLT :
The kernel can simultaneously schedule multiple threads from the same process on
multiple processors.
If one thread in a process is blocked, the kernel can schedule another thread of the
same process.
Kernel routines themselves can be multithreaded.
But, there is a disadvantage as well: The transfer of control from one thread to another
within the same process requires a mode switch to the kernel.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 19
Notes for Operating Systems (16MCA24)
Based on the communication among processors, MIMD can be further divided. If every
processor has a dedicated memory, then each processing element is a self-contained
computer. Communication among the computers is either via fixed path or via some
network. Such a system is known as a cluster. If the processors share a common memory,
it is known as shared-memory multiprocessor. This again can be further divided into
master/slave architecture and SMP. The master/slave architecture has disadvantages:
A failure of the master brings down the whole system
As master has to do all scheduling and process management, the performance may
slow down.
But, in SMP, the kernel can execute on any processor and it allows portions of kernel to
execute in parallel. Here, each processor does self-scheduling from the pool of available
process or threads.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 21
Notes for Operating Systems (16MCA24)
But, the layered approach had problems: Change in one layer could affect the working of
adjacent layers. To solve such problems, the concept of microkernels has been
proposed. Here, only essential core OS functions will be in kernel as shown in Figure
2.15(b). And, the less important functions/services/applications are built on the microkernel
and execute in user mode.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 23
Notes for Operating Systems (16MCA24)
Whenever the CPU becomes idle, the OS must select one of the processes in the ready
queue to be executed. The selection process is carried out by the short-term scheduler
(or CPU scheduler). The processed picked from ready queue need not be first-come-first-
out queue. There are various types like shortest job first, round robin etc.
(a) (b)
Figure 2.17(a) Alternating sequence of CPU & I/O bursts (b) Histogram of CPU burst times
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 24
Notes for Operating Systems (16MCA24)
Under non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching to
the waiting state. Preemptive scheduling incurs a cost. Consider the case of two processes
sharing data. One may be in the midst of updating the data when it is preempted and the
second process is run. The second process may try to read the data, which are currently in
an inconsistent state.
2.10.3 Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler. This function involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to resume that program
The dispatcher should be as fast as possible, because it will be invoked during every
process switch.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 25
Notes for Operating Systems (16MCA24)
Example 1: The three processes P1 , P2 , and P3 arrive at a time 0 with the CPU burst time
given as below. Calculate average waiting time and average turnaround time.
Solution: Suppose, the processes arrive in the order P1, P2 , P3, then the Gantt Chart for
the schedule is –
P1 P2 P3
0 24 27 30
Turnaround time is the duration from submission of the process till its completion. Hence,
turnaround time for P1= 24
Turnaround time for P2 = 27
Turnaround time for P3 = 30
Average turnaround time = (24+27+30)/3 = 27 milliseconds
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 26
Notes for Operating Systems (16MCA24)
Throughput is total number of processes completed per one unit of time. Here, 30 time
units were for completing 3 processes. So, for 1 time unit, the number of processes that
can be completed is 3/30. That is,
Throughput = 3/30 = 0.1
Example 2:
Assume, in the above example, the processes arrive in the order P2 , P3 , P1 then the Gantt
Chart for the schedule is –
P2 P3 P1
0 3 6 30
Now,
Waiting time for P1 = 6
Waiting time for P2 = 0
Waiting time for P3 = 3
Thus, Average waiting time = (6 + 0 + 3)/3 = 3 milliseconds
Average turnaround time = (30 + 3 + 6)/3 = 13 milliseconds
Here also, throughput = 3/30=0.1
NOTE: We can observe that average waiting time in FCFS vary substantially if there is a
much variation in CPU burst time of the processes.
Disadvantages:
FCFS is non-preemptive, hence the average waiting time can be more.
Troublesome in time-sharing systems, where each user needs to get a share of
CPU. But, FCFS scheme will keep CPU for a longer duration.
Non-preemptive SJF Scheduling: Here, once the CPU is given to the process, it
cannot be preempted until completes its CPU burst.
Example 1: There are four processes P1 to P4 all arrived at the time 0 and burst time
is as given below. Compute average waiting time and average turnaround time.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 27
Notes for Operating Systems (16MCA24)
P4 P1 P3 P2
0 3 9 16 24
Note that, if we would have used FCFS here, the average waiting time would have been
10.25 milliseconds.
Example 2: There are four processes P1 to P4 which arrived at different times as given
below. Compute average waiting time and average turnaround time.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 28
Notes for Operating Systems (16MCA24)
SJF algorithms usually give minimum average-waiting time and hence an optimal
algorithm. But, the problem is – in a dynamic situation, knowing the length of next CPU
burst will be difficult.
Preemptive SJF Scheduling: When a new process enters a ready queue while
another process is still executing, there will be a choice. If a new process arrives with
CPU burst length less than remaining time of current executing process, preempt. This
scheme is also known as the Shortest-Remaining-Time-First (SRTF). In this scheme,
the arrival time of every process plays a key role.
Example: There are four processes P1 to P4 arrived at different time as given below.
Compute average waiting time and average turnaround time.
Solution: Here, at the time 0, only process P1 is in a ready queue and hence will start
executing. But, after 2 milliseconds, the process P2 arrives with a burst time as 4. The
remaining time for P1 is 5, which is greater than time required for P2. Hence, P1 is
preempted and P2 gets CPU. After 2 more milliseconds (that is, at the time 4), P3
arrives. The burst time of P3 (1) is lesser than the remaining time of P2(2). Hence, P2 is
preempted and P3 is given a CPU. When P3 completes, the remaining time of other
three processes are –
P1 -- 5
P2 – 2
P4 – 4
Now, the shortest job will be getting the CPU. Hence, the Gantt chart would be –
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 29
Notes for Operating Systems (16MCA24)
Example 1: There are five processes P1, P2, P3, P4 and P5 and have arrived at the time 0
in that order. The priority and burst time are as given below –
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 30
Notes for Operating Systems (16MCA24)
0 1 6 16 18 19
The average waiting time = (6+0+16+18+1)/5 = 8.2 milliseconds
The average turnaround time = (16+1+18+19+6)/5 = 12 ms
Throughput = 5/19 = 0.2632
Example 2: Assume there are four processes whose arrival time, burst time and priority
have been given as below. Compute average waiting time and turnaround time.
P1 P2 P4 P2 P1 P3
0 1 3 8 10 17 26
NOTE: The priority scheduling has one drawback: the lower priority processes may never
gets executed. This problem is known as starvation. As higher priority processes keeps
getting added to ready queue, the lower priority processes will find indefinite delay in
getting the CPU. In such a situation, either the process will run only when the system
becomes free, or eventually, it may crash with all un-finished processes. To solve this
problem, there is a remedy called – aging. Here, the priority of the process will be
increased as the time passes by. Hence, an initially lower priority process will eventually
becomes higher priority process and gets the CPU.
Example 1: Consider the processes P1, P2 and P3 which are arrived at the time 0. Let the
time quantum be 4 milliseconds. The burst time is given as below:
0 4 7 10 14 18 22 26 30
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 32
Notes for Operating Systems (16MCA24)
Note that, the burst time of processes P2 and P3 are lesser than the actual time quantum
and hence, they will give up the CPU immediately after their completion.
Waiting time for P1 = 0 (first pickup) + {10 (next pickup) – 4 (previous CPU release )} = 6
Waiting time for P2 = 4
Waiting time for P3 = 7
Average waiting time = (6+4+7)/3 = 5. 67ms
Average turnaround time = (30 +7+10)/3 = 15.67ms
Throughput = 3/30 = 0.1
Example 2: Consider the processes P1, P2, P3 and P4 which are arrived at the time 0. Let
the time quantum be 20 milliseconds. The burst time is given as below:
Waiting time for P1 = 0 (first pickup) + {77 (next pickup) – 20 (previous release)}
+ {121(next pick up) – 97 (previous release)}
= 81
Waiting time for P3 = 37 (first pickup) + {97 (next pickup) – 57 (previous release)} +
{134 (next pickup) – 117 (previous release)} +
{154 (next pickup) - 154 (previous release)}
= 94
Waiting time for P4 = 57 (first pickup) + {117 (next pickup) – 77 (previous release)}
= 97
Average waiting time = (81+20+94+97))/4 = 73 ms
Average turnaround time = (134+37+162+121)/4 =113.5 ms
Throughput = 4/162 = 0.0247
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 33
Notes for Operating Systems (16MCA24)
These two types have different response – time requirements and different scheduling
needs. And, foreground processes may have higher priority over background processes.
A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues as shown in Figure 2.18 . Each queue has its own scheduling algorithm. Normally,
foreground queue will have Round Robin algorithm whereas, the background queue has
FCFS algorithm.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 34
Notes for Operating Systems (16MCA24)
The OS design is concerned more about management of processes and threads with
respect to multiprogramming, multiprocessing and distributed processing. Concurrency is a
fundamental issue for all these areas. Concurrency includes design issues like
communication among processes, sharing of and competing for resources, synchronization
of the activities of multiple processes, and allocation of processor time to processes.
Concurrency arises in three different contexts:
Multiple applications: Multiprogramming was invented to allow processing time to
be dynamically shared among a number of active applications.
Structured applications: As an extension of the principles of modular design and
structured programming, some applications can be effectively programmed as a set
of concurrent processes.
Operating system structure: The same structuring advantages apply to systems
programs, and we have seen that operating systems are themselves often
implemented as a set of processes or threads.
In this chapter, the importance of concurrency is discussed in detail. Some of the important
key words in the study of concurrency are listed below.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 35
Notes for Operating Systems (16MCA24)
One can understand the meaning of concurrency with the definition: concurrency is the
property of program, algorithm, or problem decomposability into order-independent
or partially-ordered components or units.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 36
Notes for Operating Systems (16MCA24)
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
It is easily observed that chin and chout are global variables. The function echo()
reads one character from the keyboard and stores into chin. It is then assigned to chout.
And then it is displayed. The echo() function is global and available to all applications.
Hence, only a single copy of echo() is loaded into main memory. Sharing of such global
variables will cause certain problems in both single-processor and multi-processor systems.
To solve this problem, only one process should be allowed to invoke echo() function for a
given moment of time. That is, when P1 has invoked the function and got interrupted, P2
may try to invoke the same function. But, P2 must be suspended and should be made to
wait. Only after P1 comes out of the echo() function, P2 must be resumed.
Thus, it is necessary to control the code for protecting shared global variable (or any other
global resources).
In a multiprocessor system, similar problem occurs. Consider there are two processes
P1 and P2 executing on two different processors. The execution of instructions of echo()
function for both P1 and P2 are as shown below. Note that, the instructions on same line
executes in parallel.
Process P1 Process P2
1 ------------------------- ------------------------
2 chin = getchar(); ------------------------
3 ------------------------- chin = getchar();
4 chout=chin; chout=chin;
5 putchar(chout); -----------------------
6 ------------------------- putchar(chout);
7 ------------------------ -----------------------
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 37
Notes for Operating Systems (16MCA24)
One can observe that, the global variable chin, initially read through P1, is overwritten by
P2. Here also, to avoid the problem, control the access of shared resources.
Example 1: Assume that two processes P1 and P2 are sharing a global variable a. The
process P1 has an instruction to update the value of a as –
a= 1;
The process P2 also has an instruction –
a= 2;
Both P1 and P2 will be executing their own set of instructions. Now, the value of a depends
on the order of execution of above statements. In other words, the loser of the race (the
process which executes last) will decide the value of a.
Example 2: Let there are two processes P3 and P4 sharing two global variables b and c.
Initially,
b=1 and c = 2
The process P3 has a statement –
b=b+c;
whereas, the process P4 has a statement –
c=b+c;
One can easily make out that, the final value of b and c depends on order of execution of
both of these statements. If P3 executes first,
b =3 and
c=5
On the other hand, if P4 executes first,
b= 4 and
c=3
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 38
Notes for Operating Systems (16MCA24)
The ways in which processes are interacted is depending on how/whether they are aware
of each other’s existence. There are three ways as listed in Table 2.6. Now, let us discuss
pros and cons of various types of relationships among processes.
The enforcement of mutual exclusion will lead to two other control problems: deadlock
and starvation. Assume, process P1 is holding the resource R1 and P2 is holding the
resource R2. Now, P1 is waiting for R2 for its completion, whereas P2 is waiting for R1
for its completion. None of P1 and P2 releases the respective resources and hence it is
a deadlock. Consider another situation: The processes P1, P2 and P3 require periodic
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 39
Notes for Operating Systems (16MCA24)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 40
Notes for Operating Systems (16MCA24)
enabling interrupts. So, a process should not be interrupted till it completes its critical
section. But, by doing so, the efficiency of OS will slow-down, as OS cannot switch
between the processes. Moreover, this approach does not guarantee mutual exclusion in
case of multi-processor systems.
The use of a special machine instruction to enforce mutual exclusion has a number of
advantages:
It is applicable to any number of processes on either a single processor or multiple
processors sharing main memory.
It is simple and therefore easy to verify.
It can be used to support multiple critical sections; each critical section can be
defined by its own variable.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 41
Notes for Operating Systems (16MCA24)
2.15 SEMAPHORES
As discussed earlier, apart from hardware-enabled mechanism for concurrency, there are
OS and programming language mechanisms as well. Semaphore is one such mechanism
for providing concurrency. Semaphore is an integer value used for signaling among
processes. Only three operations may be performed on a semaphore, all of which are
atomic: initialize, decrement, and increment. The decrement operation is for blocking of a
process, and the increment operation is for unblocking of a process.
The fundamental principle is: Two or more processes can cooperate by means of simple
signals, such that a process can be forced to stop at a specified place until it has received a
specific signal. For signaling, special variables called semaphores are used. To transmit a
signal via semaphore s, a process executes the primitive semSignal(s). To receive a signal
via semaphore s, a process executes the primitive semWait(s). If the corresponding signal
has not yet been transmitted, the process is suspended until the transmission takes place.
To achieve the desired effect, we can view the semaphore as a variable that has an integer
value upon which only three operations are defined:
1. A semaphore may be initialized to a nonnegative integer value.
2. The semWait operation decrements the semaphore value. If the value becomes
negative, then the process executing the semWait is blocked. Otherwise, the
process continues execution.
3. The semSignal operation increments the semaphore value. If the resulting value is
less than or equal to zero, then a process blocked by a semWait operation, if any, is
unblocked.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 42
Notes for Operating Systems (16MCA24)
The formal definition of semaphores can be understood using the code segment given in
Figure 2.21.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 43
Notes for Operating Systems (16MCA24)
A concept related to the binary semaphore is the mutex. The process that locks the mutex
(sets the value to zero) must be the one to unlock it (sets the value to 1). In contrast, it is
possible for one process to lock a binary semaphore and for another to unlock it.
For both counting semaphores and binary semaphores, a queue is used to hold processes
waiting on the semaphore. Normally, the process that has been blocked the longest is
released from the queue first – that is, in a FIFO manner. A semaphore whose definition
includes this policy is called a strong semaphore. A semaphore that does not specify the
order in which processes are removed from the queue is a weak semaphore.
Consider n processes, P(i), all of which need access the same resource. Each process has
a critical section used to access the resource. In each process, a semWait(s) is executed
just before its critical section. If the value of s becomes negative, the process is blocked. If
the value is 1, then it is decremented to 0 and the process immediately enters its critical
section; because s is no longer positive, no other process will be able to enter its critical
section.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 44
Notes for Operating Systems (16MCA24)
The semaphore is initialized to 1. Thus, the first process that executes a semWait will be
able to enter the critical section immediately, setting the value of s to 0. Any other process
attempting to enter the critical section will find it busy and will be blocked, setting the value
of s to –1.
Let us assume that the buffer is of infinite size. Consider the code segments for producer
and consumer as given below:
Producer Consumer
while (true) while (true)
{ {
/* produce item v */; while (in <= out)
b[in] = v; /* do nothing */;
in++;
} w = b[out];
out++;
/* consume item w */;
}
The Figure 2.24 illustrates the structure of buffer b. The producer can generate items and
store them in the buffer at its own speed. Each time, an index in is incremented. The
consumer proceeds in a similar fashion but must make sure that it does not attempt to read
from an empty buffer. Hence, the consumer makes sure that the producer is ahead of
consumer (in > out).
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 45
Notes for Operating Systems (16MCA24)
If we add a realistic restriction on this problem, it can be solved easily using semaphore.
The restriction is: instead of taking infinite buffer, consider a finite buffer of size n having a
circular storage as shown in Figure 2.25.
Since, it will work in circular manner, the pointers in and out will be considered with modulo
n. Now, the situations would be –
Block on Unblock on
Producer: insert in full buffer Consumer: item inserted
Consumer: remove from empty buffer Producer: item removed
Producer Consumer
while (true) while (true)
{ {
/*produce item v */ while(in==out)
while((in+1)%n==out) //do nothing
//do nothing w=b[out];
b[in]=v; out=(out+1)%n;
in=(in+1)%n; /*consume item w */
} }
2.16 MONITORS
The monitor is a programming-language construct that provides equivalent functionality to
that of semaphores and that is easier to control. The monitor construct has been
implemented many programming languages like Concurrent Pascal, Pascal-Plus, Java etc.
It has also been implemented as a program library. This allows programmers to put a
monitor lock on any object. For example, we can lock all linked list with one lock, or one
lock for each list or one lock for each element of every list.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 46
Notes for Operating Systems (16MCA24)
Monitor must include synchronization tools for helping concurrency. For example, suppose
a process invokes the monitor and, while in the monitor, it must be blocked until some
condition is satisfied. A facility is needed by which the process is not only blocked but
releases the monitor so that some other process may enter it. Later, when the condition is
satisfied and the monitor is again available, the process needs to be resumed and allowed
to reenter the monitor at the point of its suspension.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 47
Notes for Operating Systems (16MCA24)
A monitor supports synchronization by the use of condition variables that are contained
within the monitor and accessible only within the monitor. Condition variables are a special
data type in monitors, which are operated on by two functions:
cwait(c) : Suspend execution of the calling process on condition c . The monitor is
now available for use by another process.
csignal(c) : Resume execution of some process blocked after a cwait on the same
condition. If there are several such processes, choose one of them; if there is no
such process, do nothing.
2.17.1 Synchronization
When a message is being passed from one process to the other, there must be some level
of synchronization. That is, wee need to specify what happens to a process after it issues a
send or receive primitive. Three situations may arise now:
Blocking send, blocking receive: Both the sender and receiver are blocked until
the message is delivered; this is sometimes referred to as a rendezvous. This
combination allows for tight synchronization between processes.
Nonblocking send, blocking receive: The sender may continue, but the receiver is
blocked until the requested message arrives. This is probably the most useful
combination. It allows a process to send one or more messages to a variety of
destinations as quickly as possible. A process that must receive a message before it
can do useful work needs to be blocked until such a message arrives.
Nonblocking send, nonblocking receive: Neither party is required to wait.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 48
Notes for Operating Systems (16MCA24)
The non-blocking send is most generic and useful in many situations. Still, it has a problem:
an error could lead to a situation in which a process repeatedly generates messages.
Because there is no blocking to discipline the process, these messages could consume
system resources, including processor time and buffer space. Also, the nonblocking send
places the burden on the programmer to determine that a message has been received:
Processes must employ reply messages to acknowledge receipt of a message.
For receive primitive, blocking seems to be good idea. But, if the message is lost (say, in
a distributed system setup) before it is being received, then the process may remain
blocked for indefinite time.
2.17.2 Addressing
It is necessary to specify in the send primitive that to which process the message is being
sent. Similarly, a receive primitive may get to know from where it is receiving the
message. To maintain this information, two types of addressing are used.
Direct Addressing: Here, the send primitive includes the identifier (or address) of
the destination process. The receive primitive may include the information about
the source process. That is, the receiving process has prior knowledge about the
sending process. But, in some cases, it is impossible to know the sender. For
example, a printer connected to a networked system will not know from where it is
receiving the print command before receiving the actual command. Hence, after
receiving the message, the receiver can acknowledge the sender.
Indirect Addressing: Here, messages are not sent directly from sender to receiver
but rather are sent to a shared data structure consisting of queues that can
temporarily hold messages. Such queues are generally referred to as mailboxes.
Thus, for two processes to communicate, one process sends a message to the
appropriate mailbox and the other process picks up the message from the mailbox.
The indirect addressing facilitates decoupling of sender and receiver and hence allows
greater flexibility. The relationship between sender and receiver may be (Figure 2.27):
one-to-one: allows a private communications link to be set up between two
processes. There won’t be any interference.
one-to-many: allows for one sender and multiple receivers; it is useful for
applications where a message or some information is to be broadcast to a set of
processes.
many-to-one: is useful for client/server interaction; one process provides service to
a number of other processes. In this case, the mailbox is often referred to as a port.
many-to-many: A many-to-many relationship allows multiple server processes to
provide concurrent service to multiple clients.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 49
Notes for Operating Systems (16MCA24)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 50
Notes for Operating Systems (16MCA24)
Thus, we can make out that, a writer wants every other process to be blocked for its
execution; whereas, a reader need not block other processes.
The problem can be solved with the help of semaphores. Here, there are two ways: readers
having the priority and writers having priority.
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 51
Notes for Operating Systems (16MCA24)
As both solutions using semaphore will lead to starvation, another solution using message
passing can be adopted. In this case, there is a controller process that has access to the
shared data area. Other processes wishing to access the data area send a request
message to the controller, are granted access with an “OK” reply message, and indicate
completion of access with a “finished” message. The controller is equipped with three
mailboxes, one for each type of message (i.e. – a request, an OK, a finished) that it may
receive. The controller process services write request messages before read request
messages to give writers priority. In addition, mutual exclusion must be enforced.
2. Draw the Gantt Chart and compute average waiting time, average turnaround time and
throughput using the following algorithms:
(i) FCFS (ii) SJF (ii) RR (Quantum= 10ms)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]
This document can be downloaded from www.chetanahegde.in with most recent updates. 52
Notes for Operating Systems (16MCA24)
3. Draw the Gantt Chart and compute average waiting time, average turnaround time and
throughput using the following algorithms:
(i) FCFS (ii) SJF (preemptive) (ii) Priority(preemptive) (iv) RR (Q=4)
4. Draw the Gantt Chart and compute average waiting time, average turnaround time and
throughput using the following algorithms:
(i) FCFS (ii) SJF (ii) Priority (iv) RR (Q=1)
By: Dr. Chetana Hegde, Associate Professor, RNS Institute of Technology, Bangalore – 98
Email: [email protected]