Os Unit Ii
Os Unit Ii
Os Unit Ii
Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
A process is defined as an entity which represents the basic unit of work to be implemented in
the system.
To put it in simple terms, we write our computer programs in a text file and when we execute
this program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into
four sections ─ stack, heap, text and data. The following image shows a simplified layout of a
process inside main memory −
Stack
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
Heap
This is dynamically allocated memory to a process during its run time.
Text
This includes the current activity represented by the value of Program Counter and the
contents of the processor's registers.
Data
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. A computer
program is a collection of instructions that performs a specific task when executed by a computer.
When we compare a program with a process, we can conclude that a process is a dynamic instance of
a computer program.
In general, a process can have one of the following five states at a time.
Start
This is the initial state when a process is first started/created.
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to have the
processor allocated to them by the operating system so that they can run. Process may come into this
state after Start state or while running it by but interrupted by the scheduler to assign CPU to some
other process.
Running
Once the process has been assigned to a processor by the OS scheduler, the process state is
set to running and the processor executes its instructions.
Waiting
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user
input, or waiting for a file to become available.
Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it is
moved to the terminated state where it waits to be removed from main memory.
Process Control Block (PCB)
A Process Control Block is a data structure maintained by the Operating System for every
process. The PCB is identified by an integer process ID (PID). A PCB keeps all the information
needed to keep track of a process as listed below in the table −
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a simplified diagram of a PCB −
Process State
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
Process privileges
This is required to allow/disallow access to system resources.
Process ID
Unique identification for each of the process in the operating system.
Pointer
A pointer to parent process.
Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for
this process.
CPU registers
Various CPU registers where process need to be stored for execution for running
state.
Accounting information
This includes the amount of CPU used for process execution, time limits, execution
ID etc.
IO status information
This includes a list of I/O devices allocated to the process.
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
Operations on processes
There are many operations that can be performed on processes. Some of these are
process creation, process preemption, process blocking, and process termination. These are
given in detail as follows −
Process Creation
Processes need to be created in the system for different operations. This can be done
by the following events −
• User request for process creation
• System initialization
• Execution of a process creation system call by a running process
• Batch job initialization
A process may be created by another process using fork(). The creating process is
called the parent process and the created process is the child process. A child process can
have only one parent but a parent process may have many children. Both the parent and child
processes have the same memory image, open files, and environment strings. However, they
have distinct address spaces.
A diagram that demonstrates process creation using fork() is as follows −
Process Preemption
An interrupt mechanism is used in preemption that suspends the process executing
currently and the next process to execute is determined by the short-term scheduler.
Preemption makes sure that all processes get some CPU time for execution.
A diagram that demonstrates process preemption is as follows −
Process Termination
After the process has completed the execution of its last instruction, it is terminated.
The resources held by a process are released after it is terminated.
A child process can be terminated by its parent process if its task is no longer
relevant. The child process sends its status information to the parent process before it
terminates. Also, when a parent process is terminated, its child processes are terminated as
well as the child processes cannot run if the parent processes are terminated.
Inter-process communication
Interprocess communication is the mechanism provided by the operating system that
allows processes to communicate with each other. This communication could involve a
process letting another process know that some event has occurred or the transferring of data
from one process to another.
A diagram that illustrates interprocess communication is as follows −
Sockets
Sockets facilitate communication between two processes on the same machine or
different machines. They are used in a client/server framework and consist of the IP address
and port number. Many application protocols use sockets for data connection and data
transfer between a client and a server.
Socket communication is quite low-level as sockets only transfer an unstructured byte
stream across processes. The structure on the byte stream is imposed by the client and server
applications.
A diagram that illustrates sockets is as follows −
Multithreaded Programming
What is Thread?
A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers which hold its
current working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data
segment and open files. When one thread alters a code segment memory item, all other
threads see that.
A thread is also called a lightweight process. Threads provide a way to improve
application performance through parallelism. Threads represent a software approach to
improving performance of operating system by reducing the overhead thread is equivalent to
a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process.
Each thread represents a separate flow of control. Threads have been successfully used in
implementing network servers and web server. They also provide a suitable foundation for
parallel execution of applications on shared memory multiprocessors. The following figure
shows the working of a single-threaded and a multithreaded process.
Difference between Process and Thread
S.No Process Thread
1 Process is heavy weight or resource Thread is light weight, taking lesser resources
intensive. than a process.
2 Process switching needs interaction with Thread switching does not need to interact
operating system. with operating system.
3 In multiple processing environments, All threads can share same set of open files,
each process executes the same code but child processes.
has its own memory and file resources.
4 If one process is blocked, then no other While one thread is blocked and waiting, a
process can execute until the first process second thread in the same task can run.
is unblocked.
5 Multiple processes without using threads Multiple threaded processes use fewer
use more resources. resources.
6 In multiple processes each process One thread can read, write or change another
operates independently of the others. thread's data.
Advantages of Threads
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater scale and
efficiency.
Types of Thread
Threads are implemented in following two ways −
• User Level Threads − User managed threads.
• Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.
User Level Threads
In this case, the thread management kernel is not aware of the existence of threads.
The thread library contains code for creating and destroying threads, for passing message
and data between threads, for scheduling thread execution and for saving and restoring
thread contexts. The application starts with a single thread.
Advantages
• Thread switching does not require Kernel mode privileges.
• User level thread can run on any operating system.
• Scheduling can be application specific in the user level thread.
• User level threads are fast to create and manage.
Disadvantages
• In a typical operating system, most system calls are blocking.
• Multithreaded application cannot take advantage of multiprocessing.
Disadvantages
• Kernel threads are generally slower to create and manage than the user threads.
• Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread
facility. Solaris is a good example of this combined approach. In a combined system,
multiple threads within the same application can run in parallel on multiple processors and a
blocking system call need not block the entire process. Multithreading models are three
types
• Many to many relationship.
• Many to one relationship.
• One to one relationship.
Process Scheduling
Basic concepts
Definition
The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another process on the
basis of a particular strategy.
Process scheduling is an essential part of a Multiprogramming operating systems.
Such operating systems allow more than one process to be loaded into the executable
memory at a time and the loaded process shares the CPU using time multiplexing.
Process Scheduling Queues
The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a
separate queue for each of the process states and PCBs of all processes in the same
execution state are placed in the same queue. When the state of a process is changed, its
PCB is unlinked from its current queue and moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
• Job queue − This queue keeps all the processes in the system.
• Ready queue − This queue keeps a set of all processes residing in main memory,
ready and waiting to execute. A new process is always put in this queue.
• Device queues − The processes which are blocked due to unavailability of an I/O
device constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin,
Priority, etc.). The OS scheduler determines how to move processes between the ready and
run queues which can only have one entry per processor core on the system; in the above
diagram, it has been merged with the CPU.
Two-State Process Model
Two-state process model refers to running and non-running states which are
described below −
S.No State & Description
1
Running
When a new process is created, it enters into the system as in the running state.
2
Not Running
Processes that are not running are kept in queue, waiting for their turn to
execute. Each entry in the queue is a pointer to a particular process. Queue is
implemented by using linked list. Use of dispatcher is as follows. When a
process is interrupted, that process is transferred in the waiting queue. If the
process has completed or aborted, the process is discarded. In either case, the
dispatcher then selects a process from the queue to execute.
Types of Schedulers
Schedulers are special system software which handle process scheduling in various
ways. Their main task is to select the jobs to be submitted into the system and to decide
which process to run. Schedulers are of three types −
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
Long Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs
are admitted to the system for processing. It selects processes from the queue and loads them
into memory for execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such
as I/O bound and processor bound. It also controls the degree of multiprogramming. If the
degree of multiprogramming is stable, then the average rate of process creation must be
equal to the average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-
sharing operating systems have no long term scheduler. When a process changes the state
from new to ready, then there is use of long-term scheduler.
• Throughput –
A measure of the work done by CPU is the number of processes being executed and
completed per unit time. This is called throughput. The throughput may vary depending upon the
length or duration of processes.
• Turnaround time –
For a particular process, an important criteria is how long it takes to execute that process. The
time elapsed from the time of submission of a process to the time of completion is known as the
turnaround time. Turn-around time is the sum of times spent waiting to get into memory, waiting
in ready queue, executing in CPU, and waiting for I/O.
• Waiting time –
A scheduling algorithm does not affect the time required to complete the process once it starts
execution. It only affects the waiting time of a process i.e. time spent by a process waiting in the
ready queue.
• Response time –
In an interactive system, turn-around time is not the best criteria. A process may produce
some output fairly early and continue computing new results while previous results are being
output to the user. Thus another criteria is the time taken from submission of the process of
request until the first response is produced. This measure is called response time.
Scheduling algorithms
A Process Scheduler schedules different processes to be assigned to the CPU based on
particular scheduling algorithms. There are six popular process scheduling algorithms
There are various CPU Scheduling algorithms such as-
• First Come First Served (FCFS)
• Shortest Job First (SJF)
• Longest Job First (LJF)
• Priority Scheduling
• Round Robin (RR)
• Shortest Remaining Time First (SRTF)
• Longest Remaining Time First (LRTF)
First Come First Serve (FCFS)
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.
Process Arrival Time Execution Time Priority Service Time
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
Waiting time of each process is as follows −
Process Waiting Time
P0 0-0=0
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6
Multiprocessor Scheduling:
Multiple processor scheduling or multiprocessor scheduling focuses on
designing the system's scheduling function, which consists of more than one processor.
Multiple CPUs share the load (load sharing) in multiprocessor scheduling so that various
processes run simultaneously. In general, multiprocessor scheduling is complex as compared
to single processor scheduling. In the multiprocessor scheduling, there are many processors,
and they are identical, and we can run any process at any time.
The multiple CPUs in the system are in close communication, which shares a
common bus, memory, and other peripheral devices. So we can say that the system is tightly
coupled. These systems are used when we want to process a bulk amount of data, and these
systems are mainly used in satellite, weather forecasting, etc.
There are cases when the processors are identical, i.e., homogenous, in terms of their
functionality in multiple-processor scheduling. We can use any processor available to run any
process in the queue.
Multiprocessor systems may be heterogeneous (different kinds of CPUs)
or homogenous (the same CPU). There may be special scheduling constraints, such as
devices connected via a private bus to only one CPU.
There is no policy or rule which can be declared as the best scheduling solution to a
system with a single processor. Similarly, there is no best scheduling solution for a system
with multiple processors as well.
• Asymmetric Multiprocessing: It is used when all the scheduling decisions and I/O
processing are handled by a single processor called the Master Server. The other
processors execute only the user code. This is simple and reduces the need for data
sharing, and this entire scenario is called Asymmetric Multiprocessing.
Thread Scheduling
As mentioned briefly in the previous section, many computer configurations have a single
CPU. Hence, threads run one at a time in such a way as to provide an illusion of concurrency.
Execution of multiple threads on a single CPU in some order is called scheduling. The Java runtime
environment supports a very simple, deterministic scheduling algorithm called fixed-priority
scheduling. This algorithm schedules threads on the basis of their priority relative to other Runnable
threads.
When a thread is created, it inherits its priority from the thread that created it. You also can
modify a thread's priority at any time after its creation by using the setPriority method. Thread
priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY (constants defined in
the Thread class). The higher the integer, the higher the priority. At any given time, when multiple
threads are ready to be executed, the runtime system chooses for execution the Runnable thread that
has the highest priority. Only when that thread stops, yields, or becomes Not Runnable will a lower-
priority thread start executing. If two threads of the same priority are waiting for the CPU, the
scheduler arbitrarily chooses one of them to run. The chosen thread runs until one of the following
conditions is true:
• A higher priority thread becomes runnable.
• It yields, or its run method exits.
• On systems that support time-slicing, its time allotment has expired.
Then the second thread is given a chance to run, and so on, until the interpreter exits.
The Java runtime system's thread scheduling algorithm is also preemptive. If at any time a
thread with a higher priority than all other Runnable threads becomes Runnable, the runtime system
chooses the new higher-priority thread for execution. The new thread is said to preempt the other
threads.
Inter-process Communication:
"Inter-process communication is used for exchanging useful information between numerous
threads in one or more processes (or programs)."
• Race conditions: A race condition is an undesirable situation that occurs when a device or
system attempts to perform two or more operations at the same time, but because of the nature
of the device or system, the operations must be done in the proper sequence to be done
correctly.
A race condition is an undesirable situation that occurs when a device or system
attempts to perform two or more operations at the same time, but because of the nature of the
device or system, the operations must be done in the proper sequence to be done correctly.
In computer memory or storage, a race condition may occur if commands to read and
write a large amount of data are received at almost the same instant, and the machine attempts
to overwrite some or all of the old data while that old data is still being read. The result may
be one or more of the following:
• The computer crashes or identifies an illegal operation of the program
• Errors reading the old data
• Errors writing the new data
A race condition can also occur if instructions are processed in the incorrect order.
There are a few types of race conditions. Two categories that define the impact of the race condition
on a system are referred to as critical and noncritical:
• A critical race condition will cause the end state of the device, system or program to change. For
example, if flipping two light switches connected to a common light at the same time blows the
circuit, it is considered a critical race condition. In software, a critical race condition is when a
situation results in a bug with unpredictable or undefined behavior.
• A noncritical race condition does not directly affect the end state of the system, device or
program. In the light example, if the light is off and flipping both switches simultaneously turns
the light on and has the same effect as flipping one switch, then it is a noncritical race condition.
In software, a noncritical race condition does not result in a bug.
Critical and noncritical race conditions aren't limited to electronics or programming. They can
occur in many types of systems where race conditions happen.
Secondary
• Bounded Waiting
We should be able to predict the waiting time for every process to get into the critical
section. The process must not be endlessly waiting for getting into the critical section.
• Architectural Neutrality
Our mechanism must be architectural natural. It means that if our solution is working
fine on one architecture then it should also run on the other ones as well.
Semaphores:
Semaphores are integer variables that are used to solve the critical section problem by
using two atomic operations, wait and signal that are used for process synchronization. The
definitions of wait and signal are as follows − Wait. The wait operation decrements the value of its
argument S, if it is positive.
Semaphore can be implemented using test operations and interrupts, which should be
executed using file descriptors.
Binary Semaphore –
This is also known as mutex lock. It can have only two values – 0 and 1. Its value is
initialized to 1. It is used to implement the solution of critical section problems with multiple
processes.
The binary semaphores are quite similar to counting semaphores, but their value is
restricted to 0 and 1. In this type of semaphore, the wait operation works only if semaphore =
1, and the signal operation succeeds when semaphore= 0. It is easy to implement than
counting semaphores.
Counting Semaphore –
Its value can range over an unrestricted domain. It is used to control access to a
resource that has multiple instances.
This type of Semaphore uses a count that helps task to be acquired or released
numerous times. If the initial count = 0, the counting semaphore should be created in the
unavailable state.
However, If the count is > 0, the semaphore is created in the available state, and the
number of tokens it has equals to its count.
Wait and Signal Operations in Semaphores
Both of these operations are used to implement process synchronization. The goal of this semaphore
operation is to get mutual exclusion.
This type of semaphore operation helps you to control the entry of a task into the critical section.
However, If the value of wait is positive, then the value of the wait argument X is decremented. In the
case of negative or zero value, no operation is executed. It is also called P(S) operation.
After the semaphore value is decreased, which becomes negative, the command is held up until the
required conditions are satisfied.
Signal operation
This type of Semaphore operation is used to control the exit of a task from a critical section. It helps
to increase the value of the argument by 1, which is denoted as V(S).
Advantages of Semaphores
As there is busy waiting in semaphore, there is never a wastage of process time and resources.
They are machine-independent, which should be run in the machine-independent code of the
microkernel.
Disadvantage of semaphores
The operating system has to keep track of all calls to wait and signal semaphore.
In order to avoid deadlocks in semaphore, the Wait and Signal operations require to be executed in the
correct order.
Semaphore programming is a complicated, so there are chances of not achieving mutual exclusion.
It is also not a practical method for large scale use as their use leads to loss of modularity.
Summary:
Counting semaphore uses a count that helps task to be acquired or released numerous times.
The binary semaphores are quite similar to counting semaphores, but their value is restricted to 0 and
1.
Wait operation helps you to control the entry of a task into the critical section
Signal semaphore operation is used to control the exit of a task from a critical section
Counting Semaphore has no mutual exclusion whereas Binary Semaphore has Mutual exclusion
Semaphore allows more than one thread to access the critical section
Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a unique
name at the start of a program. The Mutex is a locking mechanism that makes sure only one thread
can acquire the Mutex at a time and enter the critical section. This thread only releases the Mutex
when it exits the critical section.
Mutex is also known as the object of mutual exclusion. Mutex is a mechanism that locks a specified
thread for a specified time. It enables only one thread to acquire it for a limited time. This specific
time is called a critical section.
The developers use mutex inside the threads to avoid any drastic outcome of inefficient resource
utilization, whether it is a variable, a function, an array, or a file. All the mutex objects have a unique
name given to them since the beginning of the program.
This is shown with the help of the following example −
wait (mutex);
…..
Critical Section
…..
signal (mutex);
4.thread to thread mutex is used but process to process locking mechanism semaphore is used.
Monitors: The monitor is one of the ways to achieve Process synchronization. The monitor is
supported by programming languages to achieve mutual exclusion between processes. For example
Java Synchronized methods. Java provides wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined together in a special kind of
module or a package.
2. The processes running outside the monitor can’t access the internal variable of the monitor but can
call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.
Condition Variables:
Two different operations are performed on the condition variables of the monitor.
Wait.
signal.
Wait operation
x.wait() : Process performing wait operation on any condition variable are suspended. The suspended
processes are placed in block queue of that condition variable.
Signal operation
x.signal(): When a process performs signal operation on condition variable, one of the blocked
processes is given chance.
Advantages of Monitor:
Monitors have the advantage of making parallel programming easier and less error prone than using
techniques such as semaphore.
Disadvantages of Monitor:
Monitors have to be implemented as part of the programming language . The compiler must generate
code for them. This gives the compiler the additional burden of having to know what operating system
facilities are available to control access to critical sections in concurrent processes. Some languages
that do support monitors are Java, C#, Visual Basic, Ada and concurrent Euclid.
Message passing: In computer science, message passing is a technique for invoking behavior
(i.e., running a program) on a computer. The invoking program sends a message to a process (which
may be an actor or object) and relies on that process and its supporting infrastructure to then select
and run some appropriate code.
Process communication is the mechanism provided by the operating system that allows
processes to communicate with each other. This communication could involve a process letting
another process know that some event has occurred or transferring of data from one process to
another. One of the models of process communication is the message passing model.
Message passing model allows multiple processes to read and write data to the message queue
without being connected to each other. Messages are stored on the queue until their recipient retrieves
them. Message queues are quite useful for interprocess communication and are used by most
operating systems.
1. In message-passing systems, processors communicate with one another by sending and receiving
messages over a communication channel. So how the arrangement should be done?
2. The pattern of the connection provided by the channel is described by some topology systems.
4. So by the definition of distributed systems, we know that they are geographically set of computers.
So it is not possible for one computer to directly connect with some other node.
7. The data is only fully communicated after the destination worker decides to receive the data.
Example when another person receives your call and starts to reply to you.
8. There is no time barrier. It is in the hand of a receiver after how many rings he receives your call.
He can make you wait forever by not picking up the call.
9. For successful network communication, it needs active participation from both sides.
Algorithm:
1. Let us consider a network consisting of n nodes named p0, p1, p2……..pn-1 which are bidirectional
point to point channels.
2. Each node might not know who is at another end. So in this way, the topology would be arranged.
3. Whenever the communication is established and whenever the message passing is started then only
the processes know from where to where the message has to be sent.
1. Easier to implement.
3. Data transfer usually requires cooperative operations which can be difficult to achieve.
4. It is difficult for programmers to develop portable applications using this model because message-
passing implementations commonly comprise a library of subroutines that are embedded in source
code. Here again, the programmer has to do everything on his own.
The dining philosopher demonstrates a large class of concurrency control problems hence it's
a classic synchronization problem.
do {
wait( chopstick[i] );
wait( chopstick[ (i+1) % 5] );
..
. EATING THE RICE
.
signal( chopstick[i] );
signal( chopstick[ (i+1) % 5] );
.
. THINKING
.
} while(1);
The design of the problem was to illustrate the challenges of avoiding deadlock, a
deadlock state of a system is a state in which no progress of system is possible. Consider a
proposal where each philosopher is instructed to behave as follows:
o The philosopher is instructed to think till the left fork is available, when it is available,
hold it.
o The philosopher is instructed to think till the right fork is available, when it is
available, hold it.
o The philosopher is instructed to eat when both forks are available.
o then, put the right fork down first
o then, put the left fork down next
o repeat from the beginning.
Let's understand with an example - If two or more than two readers want to access the
file at the same point in time there will be no problem. However, in other situations like when
two writers or one reader and one writer wants to access the file at the same point of time,
there may occur some problems, hence the task is to design the code in such a manner that if
one reader is reading then no writer is allowed to update at the same point of time, similarly,
if one writer is writing no reader is allowed to read the file at that point of time and if one
writer is updating a file other writers should not be allowed to update the file at the same
point of time. However, multiple readers can access the object at the same time.
The solution of readers and writers can be implemented using binary semaphores.
We use two binary semaphores "write" and "mutex", where binary semaphore can be defined
as:
Semaphore: A semaphore is an integer variable in S, that apart from initialization is
accessed by only two standard atomic operations - wait and signal, whose definitions are as
follows:
1. 1. wait( S )
2. {
3. while( S <= 0) ;
4. S--;
5. }
6.
7. 2. signal( S )
8. {
9. S++;
10. }
while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);