Unit 2 Process.pptx (1)
Unit 2 Process.pptx (1)
(23CA2306)
MODULE 2
▪ The fundamental task of any operating system is process
management.
▪ OS must allocate resources to processes, enable sharing of
information, protect resources, and enable synchronization among
processes.
▪ In many modern OS the problems of process management is
compounded by introduction of threads.
What is a process?
• A process is simply a program in execution: an instance of a program
execution.
• Unit of work individually schedulable by an operating system.
• OS keeps track of all the active processes and allocates system resources to
them according to policies devised to meet design performance objectives.
• To meet process requirements OS must maintain many data structures
efficiently.
• The process abstraction is a fundamental OS means for management of
concurrent program execution. Example: instances of process co-existing.
Major requirements
• OS must interleave the execution of a number of processes to
maximize processor use while providing reasonable response time.
• OS must allocate resources to processes in conformance with a
specific policy. Example: (i) higher priority, (ii) avoid deadlock.
• Support user creation of processes and IPC(Inter process
communication) both of which may aid in the structuring of
applications.
Process creation
Process creation in an operating system is a fundamental concept that
involves creating a new process, which is essentially an instance of a program
in execution. When process is creating fork() system call is executed
Four common events that lead to a process creation are:
1) When a new batch-job is presented for execution.
2) When an interactive user logs in.
3) When OS needs to perform an operation (usually IO) on behalf of a user
process, concurrently with that process.
4) To exploit parallelism an user process can spawn a number of processes.
==> concept of parent and child processes.
Process Hierarchies
• Parent creates a child process, child processes can create its own
process
• Forms a hierarchy
• UNIX calls this a "process group"
• Windows has no concept of process hierarchy
• all processes are created equal
Termination of a process
• Normal completion, time limit exceeded, memory unavailable
• Bounds violation, protection error, arithmetic error, invalid instruction
• IO failure, Operator intervention, parent termination, parent request
• A number of other conditions are possible.
• Segmentation fault : usually happens when you try write/read
into/from a non-existent array/structure/object component. Or
access a pointer to a dynamic data before creating it. (new etc.)
• Bus error: Related to function call and return. You have messed up
the stack where the return address or parameters are stored.
State Transition Diagram
Event Event
Occurs Wait
BLOCKED
Five states: New, Ready, Running, Blocked, Exit
• New : A process has been created but has not yet been admitted to
the pool of executable processes.
• Ready : Processes that are prepared to run if given an opportunity.
That is, they are not waiting on anything except the CPU availability.
• Running: The process that is currently being executed. (Assume single
processor for simplicity.)
• Blocked : A process that cannot execute until a specified event such
as an IO completion occurs.
• Exit: A process that has been released by OS either after normal
termination or after abnormal termination (error).
TRANSISTION OF PROCESS FROM ONE STATE TO
ANOTHER
• New to Ready: When a process is created, it is in a new state. It moves to the ready state when the
operating system has allocated resources to it and it is ready to be executed.
• Ready to Running: When the CPU becomes available, the operating system selects a process from
the ready queue depending on various scheduling algorithms and moves it to the running state.
• Running to Blocked: When a process needs to wait for an event to occur (I/O operation or system
call), it moves to the blocked state. For example, if a process needs to wait for user input, it moves
to the blocked state until the user provides the input.
• Running to Ready: When a running process is preempted by the operating system, it moves to the
ready state. For example, if a higher-priority process becomes ready, the operating system may
preempt the running process and move it to the ready state.
• Blocked to Ready: When the event a blocked process was waiting for occurs, the process moves to
the ready state. For example, if a process was waiting for user input and the input is provided, it
moves to the ready state.
• Running to Terminated: When a process completes its execution or is terminated by the operating
system, it moves to the terminated state.
PROCESS CONTROL BLOCK
• A Process Control Block (PCB) is a data structure used by the
operating system to manage information about a process. The
process control keeps track of many important pieces of
information needed to manage processes efficiently.
• As the operating system supports multi-programming, it needs
to keep track of all the processes. For this task, the process
control block (PCB) is used to track the process’s execution
status. Each block of memory contains information about the
process state, program counter, stack pointer, status of opened
files, scheduling algorithms, etc.
• Pointer: It is a stack pointer that is required to be saved when the process is switched from one
state to another to retain the current position of the process.
• Process state: It stores the respective state of the process.
• Process number: Every process is assigned a unique id known as process ID or PID which stores
the process identifier.
• Program counter: Program Counter stores the counter, which contains the address of the next
instruction that is to be executed for the process.
• Register: Registers in the PCB, it is a data structure. When a processes is running and it’s time slice
expires, the current value of process specific registers would be stored in the PCB and the process
would be swapped out. When the process is scheduled to be run, the register values is read from
the PCB and written to the CPU registers. This is the main purpose of the registers in the PCB.
• Memory limits: This field contains the information about memory management system used by
the operating system. This may include page tables, segment tables, etc.
• List of Open files: This information includes the list of files opened for a process.
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.
• 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.
CPU SCHEDULING
• CPU scheduling is a basis of multiprogrammed OS. By switching CPU among
processes, the OS makes the computer more productive. In multiprogrammed OS,
some process has to keep running all the time in CPU without keeping it idle. This
will lead to maximum CPU utilization.
• 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-firstout queue. There are various types like shortest
job first, round robin etc.
There are two types of multiprogramming:
• When an interrupt occurs, the system needs to save the current context of
the process currently running on the CPU so that it can restore that
context when its processing is done, essentially suspending the process
and then resuming it.
• The context is represented in the PCB of the process; it includes the value
of CPU registers, the process state, and memory management information.
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
SCHEDULING CRETERIA
Different scheduling algorithms have different properties to support different types of
processes. There are many criteria to compare CPU scheduling algorithms as given below:
• CPU Utilization: CPU must be as busy as possible. CPU utilization may range from 0% to
100%. The real time systems have CPU utilization as 40% to 90%.
• Throughput: The number of processes completed per time unit is called as throughput.
• Turnaround time: The interval from the time of submission of a process to the time of
completion is the turnaround time. Turnaround time is the sum of the duration spent
waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing
I/O.
• Waiting Time: The CPU-scheduling algorithm does not affect the amount of time during
which a process executes or does I/O; it affects only the amount of time that a process
spends waiting in the ready queue. Waiting time is the sum of the durations spent waiting in
the ready queue.
• Response Time: The time duration from the submission of a request till the first response
received is known as response time.
SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU
FIRST COME FIRST SERVE SCHEDULING
FCFS is the simplest algorithm which is managed by a simple
FIFO queue. When a process enters the ready queue, its PCB is
linked onto the tail of the queue. When the CPU is free, it is
allocated to the process at the head of the queue. The running
process is then removed from the queue. Usually, the average
waiting time for FCFS will be more
Example-1: Consider the following table of arrival time and burst
time for five processes P1, P2, P3, P4 and P5.
P1 0 8 3 0 1 0 17
10 17 9
P2 1 4 2 1 3 0 9
8 10 5
P3 2 9 4 17 26 15 24
P4 3 5 1 3 8 0 5
Thus, the Gantt chart would be –
• CPU will be given to the process for one unit of time quantum. After that, the process will be
preempted and added back to ready queue.
• The ready queue will behave as a circular queue. The scheduler will go around the ready queue
and allocates CPU for each process for a time interval of one time quantum. The value of time
quantum here can be 10ms to 100ms.
There are two possibilities:
o The process will not be completed in a one time quantum. Then, the context switch will happen and
the current process is kept back at the tail of the ready queue. And, the next process in a ready queue
is picked and allotted a CPU.
o The process will be completed within the duration of one time quantum. Then the process will give
up the CPU voluntarily. Then, next process in a ready queue will be picked for execution.
• 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:
Process Burst
Time
P1 24
P2 3
P3 3
• 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:
Process Burst
Time
P1 53
P2 17
P3 68
P4 24
Waiting time for P1 = 0 (first pickup) + {77 (next pickup) – 20 (previous release)}
+ {121(next pick up) – 97 (previous release)}
= 81
Waiting time for P2 = 20 (first pickup)
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
Assume Time Quantum TQ = 5
RACE CONDITION
• WHEN SEVERAL PROCESSES ACCESS AND MANIPULATE THE SAME
DATA CONCURRENTLY, THE FINAL OUTCOME OF THE EXECUTION IS
NOT PREDICTABLE. THE FINAL OUTCOME DEPENDS ON THE
PARTICULAR ORDER IN WHICH THE ACCESS TAKES PLACE. THIS IS
CALLED RACE CONDITION.
CRITICAL SECTION PROBLEM
• Consider a system of n processes {P0, P1,…..Pn}. Each process has a segment of code called a
CRITICAL SECTION, in which the process may be changing common variables, updating a record
• When one process is executing in its critical section, no other process is to be allowed to
• It is used to design a protocol that the processes can use to cooperate with each other.
• PROGRESS: if no process is executing its critical section, and some processes wish
to enter their critical section, then only those processes that are not executing in
their remainder section can participate in the decision on which will enter its
critical section next, and this selection cannot be postponed.
• BOUNDED WAITING: there exists a bound or limit, on the number of times that
other processes are allowed to enter their critical section, after a process has
made a request to enter its critical section and before that request is granted. So
after the limit is reached, system must grant process the permission to enter into
its critical section
SOLUTION TO CRITICAL SECTION PROBLEM
1) Mutual Exclusion - If process Pi is executing in its critical section, then no other processes
can be executing in their critical sections
2) Progress - If no process is executing in its critical section and there exist some processes
that wish to enter their critical section, then the selection of the processes that will enter
the critical section next cannot be postponed indefinitely
3) Bounded Waiting - A bound must exist 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
TWO PROCESS SOLUTION FOR CRITICAL
SECTION PROBLEM
• In algorithm-1, the processes Pi and Pj do
share a common integer variable called
“turn” to control access to the shared {
resource. while ( turn != i)
• The variable turn indicates whose turn is do no-op; //do nothing;
it to enter critical section.
critical section
• If turn=i, then it means the process Pi is
executing its critical section. turn = j;
• If turn=j, process Pj is executing its critical remainder section
section.
} while (1);
• Initially turn=0, if turn=i, then Pi can
enter its critical section
DISADVANTAGES OF ALGORITHM 1
• It does not satisfy the progress requirement, since it requires strict alternation of
processes in the execution of the critical section. For example, if turn=0 and P1 is
ready to enter its critical section, P1 cannot do so even though P0 may not be in its
critical section.
• It does not retain sufficient information about the state of the process; it
• Peterson’s algorithm uses both the variables turn and flag to be shared between the processes.
int turn;
boolean flag[2];
Initially turn=0 and flag [0] = flag [1] = false;
• To enter the critical section, process Pi first sets flag [i] to be true.
• It then sets turn to the value j, asserting that if other process wishes to enter the critical section, it
can do so.
• If both processes try to enter at the same time, turn will be set to both i and j roughly at the same
time. But only one of these assignments will lasts; the other will be overwritten immediately.
• The final value of turn decides which of the two processes will enter its critical section first.
• This algorithm satisfies all the three requirements of the critical section problem.
do
{
flag [ i ] = TRUE ;
turn = j ;
while ( flag [ j ] && turn = j) do
no-op; // do nothing
critical section ;
flag [i] = FALSE ;
remainder section ;
} while (1);
SYNCHRONIZATION HARDWARE
• Many systems provide hardware support for critical section code.
• Uniprocessors – could disable interrupts, then, currently running code would execute without
preemption
• This solution does not work on multiprocessor systems because: Disabling interrupts can be
time-consuming as the message has to be passed to all processors which delays entry into critical
section.
a) Either test memory word and set value (Test and Set)
• It is used for setting a value, but first performs some test (such as, the value is
equal to another given value).
lock = false;
return rv:
remainder section ;
}
} while (TRUE);
You can use test and set to implement locks as follows:
• The process that finds lock equals 0, can only enter its critical section.
• All other processes trying to enter their critical section must wait (in a while loop since lock=1).
• When a process leaves its critical section, it resets the value of lock to 0.
• Now, only one of the waiting processes is granted access to its critical section.
• If two TestAndSet( ) instructions are executed simultaneously, each on a different CPU, they will be
executed sequentially in some manner.
SWAP()
If the machine supports the swap( ) instruction, then mutual exclusion
can be provided as follows:
• A Shared Boolean variable lock is initialized to false.
• Each process has a local Boolean variable key initialized to true.
• Only the process that finds lock=false can enter its critical section. It
excludes all the other processes from the critical section by setting
the lock=true by swapping it with the contents of the variable key.
• The process resets the lock=false after executing its critical section,
allowing other processes to execute their critical section.
► The swap() operates on the contents of two words automatically.
do
void swap (boolean *a, boolean *b) {
{ key = TRUE;
boolean temp = *a ; while (key == TRUE)
*a = *b ; Swap( &lock, &key);
*b = *temp ; // critical section
} lock = FALSE;
// remainder section
} while (TRUE);
SEMAPHORES
• It is a new and very significant technique for managing concurrent
processes by using the value of a simple integer variable to
synchronize the progress of interacting processes.
• This integer variable is called semaphore.
• It is a synchronizing tool and is accessed only through two standard
atomic operations, wait and signal designated by P() and V()
respectively.
• A semaphore S is an integer variable with non negative values,
which can be accessed only through two standard indivisible
(atomic) operations: wait() and signal()
DEFINITION OF WAIT()
}
DEFINITION OF SIGNAL()
The wait( ) and the signal( ) operations must be executed indivisibly. That
is, when one process modifies the semaphore value, no other process can
simultaneously modify the same semaphore value simultaneously.
TYPES OF SEMAPHORES
There are two types of semaphores-
• Binary Semaphore
• Counting Semaphore
BINARY SEMAPHORE
• They can be used to solve critical-section problem for multiple Signal ( mutex );
• Operations
• create a new mailbox (port)
• send and receive messages through mailbox
• destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Indirect Communication
• Mailbox sharing
• P1, P2, and P3 share mailbox A
• P1, sends; P2 and P3 receive
• Who gets the message?
• Solutions
• Allow a link to be associated with at most two processes
• Allow only one process at a time to execute a receive
operation
• Allow the system to select arbitrarily the receiver. Sender
is notified who the receiver was.
Types of Atomic Operations
• There are two types of atomic operations, which small atomic operations and large
atomic operations. Let us see the difference between them:
2. Large Atomic Operations: This type of atomic operation consists of a group of small
atomic operations. This entire group of operations can’t be interrupted by any other
process. For example: Calling a method in a programming language.
Why Atomic Operations are Needed?
• Initially, considered any variable let’s say A, which has initial value as 0.
• That process increments the value of A by 10, which turns the current value of A as 11.
• Now when inter-leaved operation at point 2 starts executing again from its stopping
point, the value of A must be 1 which is now 11, this generated inaccurate results.