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

Unit 2 Process.pptx (1)

The document discusses the fundamental concepts of operating systems, focusing on process management, including definitions of processes, their states, and transitions. It covers process creation, hierarchies, termination, and the structure of Process Control Blocks (PCBs), as well as CPU scheduling algorithms like FCFS, SJF, and priority scheduling. Additionally, it explains context switching, the role of the dispatcher, and various scheduling criteria and algorithms to optimize CPU utilization.

Uploaded by

bharathsekar9009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit 2 Process.pptx (1)

The document discusses the fundamental concepts of operating systems, focusing on process management, including definitions of processes, their states, and transitions. It covers process creation, hierarchies, termination, and the structure of Process Control Blocks (PCBs), as well as CPU scheduling algorithms like FCFS, SJF, and priority scheduling. Additionally, it explains context switching, the role of the dispatcher, and various scheduling criteria and algorithms to optimize CPU utilization.

Uploaded by

bharathsekar9009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

OPERATING SYSTEMS

(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

Admit Dispatch Release


READY RUNNING EXIT
NEW
Time-out

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:

• Preemption – Process is forcefully removed from CPU.


Pre-emption is also called time sharing or multitasking.

• Non-Preemption – Processes are not removed until they


complete the execution. Once control is given to the CPU for a
process execution, till the CPU releases the control by itself,
control cannot be taken back forcibly from the CPU.
CONTEXT SWITCHING
• Interrupts cause the operating system to change CPU from its current task
and run a kernel routine. Such operations happen frequently on
general-purpose systems.

• 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.

PROCESS ID ARRIVAL TIME BRUST


TIME
P1 0 4
P2 1 3
P3 2 1
P4 3 2
P5 4 5
Example-1: Consider the following table of arrival time and burst
time for five processes P1, P2, P3, P4 and P5.
PROCESS ID ARRIVAL TIME BRUST PROCESS ARRIVAL BRUST COMPLETI TURN WAITING
TIME ID TIME(AT) TIME(B ON AROUND TIME
P1 0 4 T) TIME(CT) TIME (WT=TAT
(TAT=CT – AT) –BT)
P2 1 3
P1 0 4 4 4 0
P3 2 1
P2 1 3 7 6 3
P4 3 2
P3 2 1 8 6 5
P5 4 5
P4 3 2 10 7 5
P5 4 5 15 11 6
Average Turn around Time =
Total TAT / no. of process
=34/5 =6.8 unit time

Average waiting time =


Total WT / no. of process
= 19/5=3.8 unit time
Through put =
No . of process/time taken to complete all process
=5/15 = 0.33
SHORTEST JOB FIRST(SJF)
In SJF scheduling, the process with a shortest CPU-burst is
allocated the CPU first. If two processes have equal CPU-burst
value, then FCFS is used to break a tie.
• 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.
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Waiting time for P1 = 3
Waiting time for P2 = 16
Waiting time for P3 = 9
Waiting time for P4 = 0
Average waiting time = (3+16+9+0)/4 = 7 milliseconds
Turnaround time for P1 = 9
Turnaround time for P2 =24
Turnaround time for P3 = 16
Turnaround time for P4 = 3
Average turnaround time = (9 + 24 + 16 + 3)/4 = 13 milliseconds
Throughput = 4/24 = 0.1667
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.
AWT=4
ATT=8
Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5.

PROCES ARRIVAL BRUST


S ID TIME(sec) TIME(sec)
P1 2 6
P2 5 2
P3 1 8
P4 0 3
P5 4 4
Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5.

PROCES ARRIVAL BRUST PROCE ARRIVAL BRUST COMPLETI TURN WAITING


S ID TIME(sec) TIME(sec) SS ID TIME(sec) TIME(sec) ON AROUND TIME(sec)
TIME(sec) TIME(sec)
P1 2 6
P1 2 6 9 7 1
P2 5 2
P2 5 2 11 6 4
P3 1 8
P3 1 8 23 22 14
P4 0 3
P4 0 3 3 3 0
P5 4 4
P5 4 4 15 11 7

Average TAT = 49/5 = 40.2sec

Average WT = 26/5 = 5.2sec

Through put = 5/23 = 0.217sec


Priority Scheduling
• Priority scheduling is a special case of general priority scheduling algorithm. A priority is associated
with each process, and the CPU is allocated to the process with the highest priority. Equal-priority
processes are scheduled in FCFS order.
• Example 1: There are five processes P1, P2, P3, P4 and P5 and have arrived at the time 0in that
order. The priority and burst time are as given below –

Process Burst Time Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Now, the Gantt chart would be –

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.

Process Arrival Burst Priority


time Time
P1 0 8 3
P2 1 4 2
P3 2 9 4
P4 3 5 1
Note that, the process P1 with priority 3 arrived at the time 0 and no other process is there in a ready
queue.
Hence, it will be given a CPU. But, just after 1 millisecond, the process P2 with higher priority than P1
arrives and so, P1 has to be preempted and P2 will get CPU.
When P2 is under execution, P3 arrives at the time 2. But, its priority is lower than P2. Hence, P2 will
continue to execute.
Later, P4 arrives at the time 3, which has higher priority than the currently executing P2.
So, P2 will be preempted at P4 gets the CPU.
Thus, P4 will finish execution before all other processes. Now, the remaining processes will be
executed as per their priorities
AT BT PRIORITY S E W T

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 –

Waiting time for P1 = 10 – 1 (completed duration) – 0 (arrival time) = 9


Waiting time for P2 = 8 – 2 (completed duration) – 1 (arrival time) = 5
Waiting time for P3 = 17 – 2 (arrival time) = 15
Waiting time for P4 = 0
Average waiting time = (9+5+15+0)/4 = 7.25ms

Turnaround time for P1 = 17 – 0 (arrival time) = 17


Turnaround time for P2 = 10 – 1 (arrival time) = 9
Turnaround time for P3 = 26 – 2 (arrival time) = 24
Turnaround time for P4 = 8 – 3 (arrival time) = 5
Average Turnaround time = (17+9+24+5)/4 = 13.75ms
Throughput = 4/26 = 0.1538
• 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.
Round-Robin Scheduling
• This algorithm is designed for time-sharing systems. It is similar to FCFS, but a time quantum is
introduced.

• 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

in a table, writing a file and so on.

• When one process is executing in its critical section, no other process is to be allowed to

execute in its critical section.

• It is used to design a protocol that the processes can use to cooperate with each other.

• A process must request permission to enter its critical section.


Each process must request permission to
enter its critical section.
The section of code implementing this request
is entry section.
The critical section is followed by an exit
section.
The remaining code is remainder section.
REQUIREMENTS TO CRITICAL SECTION
PROBLEM
• MUTUAL EXCLUSION: if process pi is executing in its critical section, then no other
processes can be executing in their critical sections.
At a time only one process can be executed in its critical section.

• 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

remembers only which process is allowed to enter its critical section.


ALGORITHM 2

• To avoid strict turn-taking and overdependence do


on other processes, algorithm-2 replaces the
variable turn with the flag array. {
• The flag array is used to indicate if a process is flag [i] = TRUE;
ready to enter its critical section. For example, if
flag [i] is true, it indicates that Pi is ready to enter while (flag [j] )
its critical section.
do no-op; //do nothing
• Shared variables
a) boolean flag[2]; critical section;
b) initially flag [i] = flag [j] = false flag [i] = FALSE;
c) flag [i]=true => Pi is ready to enter its critical remainder_section;
section
} while (1);S
PETERSON’S ALGORITHM
• Peterson's algorithm is the simplest algorithm for mutual exclusion problem that allows two

process to share a resource without conflict.

• This algorithm is obtained by combining algorithm-1 and algorithm-2.

• 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.

• Modern machines provide special atomic hardware instructions (Atomic = non-interruptable)

a) Either test memory word and set value (Test and Set)

b) Or swap contents of two memory words (Swap)


TESTANDSET()
• It is an instruction used to both test and write to a memory location as part of a
single atomic (i.e. non-interruptible) operation.

• It is used for setting a value, but first performs some test (such as, the value is
equal to another given value).

• The value is modified if the test returns true.

• If the test fails, the value is not set.

• The entire TestAndSet( ) function is carried out atomically (without interruption).


do
boolean TestAndSet (boolean *target) {
{
while ( TestAndSet (&lock )) ;
boolean rv = *target;
//do nothing
*target = TRUE; critical section ;

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.

• According to the definition of TestAndSet( ) the lock is now set to 1.

• 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()

• The wait() operation was originally termed P (from


the Dutch proberen, “to test”).
• This operation is implemented to acquire a wait (S)
resource.
• In this the value S is tested and decremented {
without interruption.
• If the value becomes negative(S ≤ 0), then the while (S <= 0) do no-op ;
process executing the wait() is blocked.
S--;

}
DEFINITION OF SIGNAL()

• The signal() operation was originally called V (from signal (S)


verhogen, “to increment”). {
• It increments the value of the semaphore S. S++ ;
}
• It is used to release a resource.

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

• A semaphore whose variable is allowed to take only two


values of 0 (busy) and 1 (free) is called binary semaphore. do {

• Binary semaphores are known as mutex locks, as they wait ( mutex );

provide mutual exclusion. critical section ;

• They can be used to solve critical-section problem for multiple Signal ( mutex );

processes. All processes share a semaphore mutex which is remainder section ;


initialized to 1.
} while (TRUE);
COUNTING SEMAPHORE
• Counting Semaphores can be used to control access to a given resource
consisting of finite number of instances (copies).
• First the semaphore is initialized to the number of resources available.
• Each process that wishes to use a resource performs a wait () operation on
the semaphore (thereby decrementing the count).
• When the process releases the resources, it performs a signal () operation
(thereby incrementing the count).
• When the count for the semaphore becomes 0, all the resources are used
and any process requesting for the resource is blocked until the count
becomes greater than 0.
EXAMPLE OF SEMAPHORE
• Semaphores are also used to solve synchronization problems.
• For example, consider two concurrently running processes P1 and P2, P1 with a statement
S1and P2 with a statement S2. Suppose we want to execute S2 only after S1 has
completed. We can implement this scheme by letting P1 and P2 share a common
semaphore synch, initialized to 0, and by inserting the statements.
• In process P1
S1;
Signal(synch);
• In process P2
Wait(synch);
S2;
Initially synch=0, P2 will execute S2 only after P1 has invoked signal(synch), which is after the
statement S1 has been executed.
IMPLEMENTATION OF A SEMAPHORE
typedef struct • Each semaphore has an integer value and
a list of processes.
{
• When a process must wait ( ) on a
int value; semaphore, it is added to the list of
waiting processes.
struct process *list;
• A signal ( ) operation removes one
} semaphore; process from the list of waiting processes,
and awakens that process.
• The block ( ) operation places the
process that invokes it into the waiting
wait ( semaphore *S ) Signal (semaphore *S) queue.
{ { • The wakeup (P) operation removes one
of the process from the waiting queue
S→value--; s→value++ ; and places it in the ready queue.
• These two operations are provided by
If (S→value<0) if(S→value <= 0) the operating system as basic system
{ { calls.
• This implementation of semaphore can
add this process to S→list; remove a process P from have negative values. If the semaphore
S→list; is negative, its magnitude indicates the
block ( );
number of processes waiting on that
wakeup(P) ;
} semaphore. This is because of the
} decrement and the test instructions
} implemented in the wait ( ) operation.
}
CRITICAL REGIONS
• Though semaphores provide an effective mechanism for process
synchronization, their improper usage can result in deadlocks or
starvation which are difficult to detect.

• To overcome these problems, a number of high-level


synchronization constructs have been proposed like –
1. Critical regions
2. Monitors
• A critical region is a section of code that is always Example:
executed under mutual exclusion. Critical regions shift
the responsibility for enforcing mutual exclusion from
the programmer to the compiler. This is only pseudo-Pascal-FC
var
• Critical Regions consist of two parts: v : shared T;
1. Variables are accessed under mutual exclusion ...
2. Statements that identifies a critical region in
region v do
which the variables are accessed begin
...
end;
All critical regions that are ‘tagged’ with the ∙ Here process A can be executing inside its V2
same variable have compiler-enforced region while process B is executing inside its V1
mutual exclusion so that only one of them region.
can be executed at a time: ∙ But if they both want to execute inside their
respective V1 regions only one will be permitted
Process A: Process B: to proceed by the compiler.
region V1 do region V1 do ∙ Each shared variable (V1 and V2 above) has a
begin
Begin queue associated with it.
{ Do other stuff. }
{ Do some stuff } end; ∙ Once one process is executing code inside a
end; region tagged with a shared variable, any other
region V2 do processes that attempt to enter a region tagged
begin with the same variable are blocked and put in the
{ Do more stuff. } queue.
end;
MUTEX
• stands for Mutual Exclusion Object.
• Mutex is mainly used to provide mutual exclusion to a specific
portion of the code so that the process can execute and work
with a particular section of the code at a particular time.
• A mutex enforces strict ownership
• A mutex is a type of lock that can be acquired or released by a
thread or process to control the access to the resource
MONITORS
• All the solutions discussed so far make sure that at most one process is
allowed to access shared resource at any time. But sometimes the process
allowed to access the resource can mistakenly or maliciously corrupt it. To
overcome such problems, monitors are used.
• Monitor is an operating system structure mechanism used to provide data
abstraction along with concurrency control.
• A monitor is a collection of variables and procedures that operate on
these variables grouped together in a special kind of module or package.
Variables are usually private to the monitor and inaccessible outside of it,
whereas procedures may be private or public.
monitor monitor name
{
begin
declarations of private data; [local monitor variables]
………………………………..
procedure p1 ( formal parameters) {
{
procedure_body;
}
procedure p2 ( formal parameters) {
{
procedure_body;
}
.procedure pn (……. ) {
{
procedure_body;
}
initialization code ( . . . . .)
{
......
}
}
Interprocess Communication – Shared Memory
• An area of memory shared among the processes that wish
to communicate
• The communication is under the control of the users
processes not the operating system.
• Major issues is to provide mechanism that will allow the
user processes to synchronize their actions when they
access shared memory.
.
Interprocess Communication – Message Passing

• Mechanism for processes to communicate and to


synchronize their actions

• Message system – processes communicate with each other


without resorting to shared variables

• IPC facility provides two operations:


• send(message)
• receive(message)

• The message size is either fixed or variable


Message Passing (Cont.)

• If processes P and Q wish to communicate, they need to:


• Establish a communication link between them
• Exchange messages via send/receive
• Implementation issues:
• How are links established?
• Can a link be associated with more than two processes?
• How many links can there be between every pair of communicating
processes?
• What is the capacity of a link?
• Is the size of a message that the link can accommodate fixed or
variable?
• Is a link unidirectional or bi-directional?
Message Passing (Cont.)

• Implementation of communication link


• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
Direct Communication
• Processes must name each other explicitly:
• send (P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q
• Properties of communication link
• Links are established automatically
• A link is associated with exactly one pair of communicating
processes
• Between each pair there exists exactly one link
• The link may be unidirectional, but is usually bi-directional
Indirect Communication
• Messages are directed and received from mailboxes
(also referred to as ports)
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
• Properties of communication link
• Link established only if processes share a common mailbox
• A link may be associated with many processes
• Each pair of processes may share several communication
links
• Link may be unidirectional or bi-directional
Indirect Communication

• 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:

1. Small Atomic Operation: These types of atomic operations can be implemented


without any interruption of any other process. These operations can’t break down more
because they are at their smallest level. For example: Loading the value stored in
registers or storing the processed value.

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.

• An increment operation is performed over A, which turns its value to 1. However,


this operation is interleaved in between due to any other process, which uses the same
variable.

• 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.

• If the increment operation at point 2 was performed by an atomic operation, then it


doesn’t allow the inter-leaving process and ensures the accuracy of results generated.

You might also like