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

Unit 2 Os

Concurrent processes allow multiple tasks to be performed simultaneously using separate processors. This improves performance over sequential processing with a single processor. The operating system must carefully manage shared resources and ensure processes do not interfere with each other. It provides mechanisms for processes to synchronize access to shared data and resources. Deadlock can occur when multiple processes block while waiting for resources held by each other. Mutual exclusion techniques like semaphores and locks are used to coordinate access to critical sections of code and shared memory to avoid inconsistencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Unit 2 Os

Concurrent processes allow multiple tasks to be performed simultaneously using separate processors. This improves performance over sequential processing with a single processor. The operating system must carefully manage shared resources and ensure processes do not interfere with each other. It provides mechanisms for processes to synchronize access to shared data and resources. Deadlock can occur when multiple processes block while waiting for resources held by each other. Mutual exclusion techniques like semaphores and locks are used to coordinate access to critical sections of code and shared memory to avoid inconsistencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT 2

CONCURRENT PROCESSES:
Concurrent processing is a computing model in which multiple processors execute
instructions simultaneously for better performance. Concurrent means something that
happens at the same time as something else. Tasks are broken down into subtasks that are
then assigned to separate processors to perform simultaneously, instead of sequentially as
they would have to be carried out by a single processor. Concurrent processing is sometimes
said to be synonymous with parallel processing.
In a multiprogramming environment, processes executing concurrently are either
competing for the CPU and other global resources, or cooperating with each other for
sharing some resources. An OS deals with competing processes by carefully allocating
resources and properly isolating processes from each other. For cooperating processes,
on the other hand, the OS provides mechanisms to share some resources in certain ways as
well as allowing processes to properly interact with each other. Cooperation is either
by sharing or by communication.
PROCESS SYNCHRONIZATION:-
Process Synchronization means sharing system resources by processes in a such a way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent
data. Maintaining data consistency demands mechanisms to ensure synchronized execution of
cooperating processes.
Process Synchronization was introduced to handle problems that arose while multiple process
executions.
PROCESS CONCEPT:-
A process is a running program; OS keeps track of running programs in form of processes
and their data. A process is made of multiple threads.
 A process is an instance of a program in execution.
 Batch systems work in terms of "jobs". Many modern process concepts are still
expressed in terms of jobs, ( e.g. job scheduling ), and the two terms are often used
interchangeably.
SOME DESIRED PROPERTIES OF CONCURRENT PROGRAMS:-
There are some characteristics that a concurrent program must possess. They can be either a
safety or a liveness property.

Safety properties assert that nothing bad will ever happen during a program execution.
Examples of safety property are:
 Mutual exclusion
 No deadlock
 Partial correctness
 Fairness (weak)
 Reliable communication
 Total correctness

THREADS:-
The need to write concurrent applications introduced threads. In other words, threads are
processes that share a single address space. Each thread has its own program counter and
stack.
Threads are often called lightweight processes as N threads have 1 page table, 1 address
space and 1 PID while N processes have N page tables, N address spaces and N PIDs.
Therefore, a sequence of executing instructions is called a thread that runs independently of
other threads and yet can share data with other threads directly. A thread is contained inside a
process. There can exist multiple threads within a process that share resources like memory,
while different processes do not share these resources.

RACE CONDITION:-
A race condition occurs when multiple processes access and manipulate the same data
concurrently, and the outcome of the execution depends on the particular order in which the
access takes place.
It is not so easy to detect race condition during program execution if it is observed that the
value of shared variables is unpredictable, it may be caused because of race condition. In
concurrent programming there are more than one legal possible thread executions hence order
of thread execution cannot be predicted.
PROCESS STATE:-
Processes may be in one of 5 states, as shown in Figure 3.2 below.
 New - The process is in the stage of being created.
 Ready - The process has all the resources available that it needs to run, but the CPU is
not currently working on this process's instructions.
 Running - The CPU is working on this process's instructions.
 Waiting - The process cannot run at the moment, because it is waiting for some
resource to become available or for some event to occur. For example the process may
be waiting for keyboard input, disk access request, inter-process messages, a timer to
go off, or a child process to finish.
 Terminated - The process has completed.
PROCESS CONTROL BLOCK:-
For each process there is a Process Control Block, PCB, which stores the following ( types of
) process-specific information,
 Process State - Running, waiting, etc., as discussed above.
 Process ID, and parent process ID.
 CPU registers and Program Counter - These need to be saved and restored when
swapping processes in and out of the CPU.
 CPU-Scheduling information - Such as priority information and pointers to
scheduling queues.
 Memory-Management information - E.g. page tables or segment tables.
 Accounting information - user and kernel CPU time consumed, account numbers,
limits, etc.
 I/O Status information - Devices allocated, open file tables, etc.

PRINCIPLE OF CONCURRENCY:-
MUTUAL EXCLUSION
“Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent
programming to avoid the simultaneous use of a common resource, such as a global variable,
by pieces of computer code called critical sections.” (Wikipedia)
-CRITICAL REGION (CR)
A part of code that is always executed under mutual exclusion is called a critical region. Due
to this, the compiler instead of the programmer is supposed to check that the resource is
neither being used nor referred to outside its critical regions. While programming, critical
section resides when semaphores are used. CRs are needed only if the data is writeable.
It consists of two parts:
Variables: These must be accessed under mutual exclusion.
New language statement: It identifies a critical section that has access to variables.
There are two processes namely A and B that contain critical regions i.e. the code where
shared data is readable and writable.
-SEMAPHORES:
Semaphores are mechanisms which protect critical sections and can be used to implement
condition synchronization. Semaphore encapsulates the shared variable and using semaphore,
only allowed set of operations can be carried out. It can suspend or wake processes.
The two operations performed using semaphores are wait and signal, also known as P and V
respectively. When a process performs P operation it notifies semaphore that it wants to use
the shared resource, if the semaphore is free the process gains access to the shared variable
and semaphore is decremented by one else the process is delayed.
If V operation is performed, then the process notifies the semaphore that it has finished using
shared variable and semaphore value is incremented by one.
By using semaphores, we attempt to avoid other multi-programming problem of Starvation.
There are two kinds of Semaphores:
Binary semaphores: Control access to a single resource, taking the value of 0 (resource is in
use) or 1 (resource is available).

Counting semaphores: Control access to multiple resources, thus assuming a range of


nonnegative values.
-Locks
The most common way to implement mutex is using locks. A lock can be either locked or
unlocked. The concept is analogues to locks we use in our doors; a person enters the room,
locks the door and starts working and leaves the room after finishing the job, if another
person wants to enter the room when one person is already inside, he has to wait until the
door gets unlocked.
Subtasks in a parallel program are often called threads. Smaller, lightweight versions of
threads are known as fibres, which are used by some parallel computer architecture and
bigger versions are called as processes. Many times threads need to change the value of
shared variable, instruction interleaving between programs could be in any order For
example, consider the following program:
Thread A
Thread B
1A -Read variable X
1B – Read variable X
2A – Increment value of X by 1
2B – Increment value of X by 1
3A – Write back to variable X
3B – Write back to variable X
As we can see in the example both the threads are carrying out same steps which are to read
the shared variable, increment its value and write back its value to the same variable. It is
clear how vital it is to execute these instructions in correct order, for instance if instruction 1A
is executed between 1B and 3B it will generate an incorrect output. If locks are used by one
thread, another thread cannot read, write the shared variable. Following example explains
usage of locks:
Thread A
Thread B
1A – Lock variable X
1B – Lock variable X
2A – Read variable X
2B – Read variable X
3A – Increment value of X by 1
3B – Increment value of X by 1
4A – Write back to variable X
4B – Write back to variable X
5A – Unlock variable X
5B – Unlock variable X
Whichever thread locks the variable first, uses that variable exclusively, any other thread will
not be able to gain access to shared variable until it is unlocked again. Locks are useful for
correct execution but on the other hand they slow down the program.
DEADLOCK:
Deadlock is defined as the permanent blocking of a set of processes that either compete for
global resources or communicate with each other. it occurs when each process in the set is
blocked awaiting an event that can be triggered only by another blocked process in the set.
Consider Figure, in which both processes P and Q need both resources A and B
simultaneously to be able to proceed. Thus P has the form get A, ... get B, , release A, ... ,
release B, and Q has the form get B, ... get A, ... , release B, , release A.
The six paths depicted in above correspond to the following.
1. Q acquires both resources, then releases them. P can operate freely later.
2. Q acquires both resources, then P requests A. P is blocked until the resources are released,
but can then operate freely.
3. Q acquires B, then P acquires A, then each requests the other resource. Deadlock is now
inevitable.
4. P acquires A. then Q acquires B, then each requests the other resource. Deadlock is now
inevitable.
5. P acquires both resources. then Q requests B. Q is blocked until the resources are released,
but can then operate freely.
6. P acquires both resources, then releases them. Q can operate freely later.
Contrast the above example with the situation where P does not need the resources
simultaneously: P has the form get A. ... release A. ... get B, ... release B, and Q is defined as
before.
Methods to handle Deadlock
1. Deadlock prevention
Deadlock prevention is to ensure that one of the four necessary conditions for deadlock can
never hold in following ways:
1.Mutual exclusion: allocate one resource to only one process at a time.
2. Hold and wait: It requires a process to request and be allocated its resources before it
begins its execution, or allow process to request a resource only when process has none. This
may lead to low resource utilization. It also may give rise to ‘starvation’ problem, a process
may be held for a long time waiting for all its required resources. The application need to be
aware of all the resources it requires, if it needs additional resources it releases all the
resources held and then requests for all those it needs.
3. No pre-emption: If a process is holding some resources and requests for another resource
held by some other process that cannot be allocated to it, then it releases all the resources
currently held.
The state of pre-empted resource has to be saved and later restored.
4. Circular wait: To make this condition fail, we can impose a total ordering on all resources
types. It is also required that each process requests resources in strict increasing order.
Resources from the same resource type have to be requested together.
2. Deadlock avoidance
In deadlock avoidance, the system checks if granting a request is safe or not .
The system needs additional prior information regarding overall potential use of each
resource for each process i.e. maximum requirement of each resource has to be stated in
advance by each process.
3. Deadlock detection:
It is important to know if there exists a deadlock situation in the system hence an algorithm is
needed to periodically check existence deadlock.
RECOVERY FROM DEADLOCK:
To recover from deadlock, the process can be terminated or we can pre-empt the resource. In
terminating processes method we can terminate all the processes at once or terminate one
process and then again check for deadlock.
Similarly there are mechanisms like fair scheduling that can be used to avoid starvation of
resources.
-Fair scheduling
Fair scheduling is to allow multiple processes to fairly share the resources. The main idea is
to ensure each thread gets equal CPU time and to minimize resource starvation.
-First in first out (FIFO)
FIFO or First Come, First Served (FCFS) is the simplest scheduling algorithm that queues
processes in the order they arrive in the ready queue.
Scheduling overhead is minimal because context switches occur only when process
terminates and re-organization of the process queue is not required.
In this scheme, completion of every process is possible, hence no starvation.
-Shortest remaining time
With this scheduling scheme, processes with least processing time are arranged as the next
process in the queue. To achieve this, prior knowledge of completion time is required.
Consider a scenario where a shorter process arrives when another process is running, in this
case the current process is stopped and is divided into two parts. This results in additional
context switching overhead.
-Fixed priority pre-emptive scheduling
The operating system gives a fixed priority rank to every process, and the processes are
arranged in the ready queue based on their priority this results in higher priority processes
interrupting lower priority processes.
Waiting and response times are inversely proportional to priority of the process.
If there are more high priority processes than low priority processes, it may result into
starvation of the latter processes.
-Round-robin scheduling
In this scheduling algorithm, each process is allotted a fixed time unit. There could be extra
overhead if time unit per process allotted is very small. Round robin has better average
response time than rest of the scheduling algorithms. There cannot be starvation since
processes are queued based on any priority.

PROCESS SYNCHRONIZATION:
Process Synchronization means sharing system resources by processes in a such a way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent
data. Maintaining data consistency demands mechanisms to ensure synchronized execution of
cooperating processes.
Process Synchronization was introduced to handle problems that arose while multiple process
executions. Some of the problems are discussed below.
CRITICAL SECTION PROBLEM:-
A Critical Section is a code segment that accesses shared variables and has to be executed as
an atomic action. It means that in a group of cooperating processes, at a given point of time,
only one process must be executing its critical section. If any other process also wants to
execute its critical section, it must wait until the first one finishes.
Solution to Critical Section Problem:-
A solution to the critical section problem must satisfy the following three conditions:
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a
given point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical
section then any one of these threads must be allowed to get into its critical section.
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for how
many other processes can get into their critical section, before this process's request is
granted. So after the limit is reached, system must grant the process permission to get into its
critical section.
Synchronization Hardware:-
Many systems provide hardware support for critical section code. The critical section
problem could be solved easily in a single-processor environment if we could disallow
interrupts to occur while a shared variable or resource is being modified.
In this manner, we could be sure that the current sequence of instructions would be allowed to
execute in order without pre-emption. Unfortunately, this solution is not feasible in a
multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time consuming as the message
is passed to all the processors.

INTRODUCTION TO SEMAPHORES:-
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. So it is basically a synchronizing tool and is accessed only
through two low standard atomic operations, wait and signal designated by P(S) and V(S)
respectively.
In very simple words, semaphore is a variable which can hold only a non-negative Integer
value, shared between all the threads, with operations wait and signal, which work as follow:
P(S): if S ≥ 1 then S := S - 1
else <block and enqueue the process>;
V(S): if <some process is blocked on the queue>
then <unblock a process>
else S := S + 1;
The classical definitions of wait and signal are:
 Wait: Decrements the value of its argument S, as soon as it would become non-
negative(greater than or equal to 1).
 Signal: Increments the value of its argument S, as there is no more process blocked
on the queue.
Properties of Semaphores:-
 It's simple and always have a non-negative Integer value.
 Works with many processes.
 Can have many different critical sections with different semaphores.
 Each critical section has unique access semaphores.
 Can permit multiple processes into the critical section at once, if desirable.
Types of Semaphores
Semaphores are mainly of two types:
1) Binary Semaphore:
 It is a special form of semaphore used for implementing mutual exclusion, hence it
is often called a Mutex. A binary semaphore is initialized to 1 and only takes the
values 0 and 1 during execution of a program.
2) Counting Semaphores:
 These are used to implement bounded concurrency.
Classical Problems of Concurrency/Synchronization:-
Semaphore can be used in other synchronization problems besides Mutual Exclusion.
Below are some of the classical problem depicting flaws of process synchronization in
systems where cooperating processes are present.
We will discuss the following three problems:
 Bounded Buffer (Producer-Consumer) Problem
 The Readers Writers Problem
 Dining Philosophers Problem
Bounded Buffer Problem(Producer-Consumer):-
The producer consumer problem is a synchronization problem. There is a fixed size buffer
and the producer produces items and enters them into the buffer. The consumer removes the
items from the buffer and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item
from the buffer and vice versa. So the buffer should only be accessed by the producer or
consumer at a time.
The producer consumer problem can be resolved using semaphores
 This problem is generalised in terms of the Producer Consumer problem, where a
finite buffer pool is used to exchange messages between producer and consumer
processes.
 Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
 Solution to this problem is, creating two counting semaphores "full" and "empty" to
keep track of the current number of full and empty buffers respectively.
What is the Problem Statement?
There is a buffer of n slots and each slot is capable of storing one unit of data. There are two
processes running, namely, producer and consumer, which are operating on the buffer.

A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove
data from a filled slot in the buffer. As you might have guessed by now, those two processes
won't produce the expected output if they are being executed concurrently.
There needs to be a way to make the producer and consumer work in an independent manner.
Here's a Solution
One solution of this problem is to use semaphores. The semaphores which will be used here
are:
 m, a binary semaphore which is used to acquire and release the lock.
 empty, a counting semaphore whose initial value is the number of slots in the buffer,
since, initially all slots are empty.
 full, a counting semaphore whose initial value is 0.
At any instant, the current value of empty represents the number of empty slots in the buffer
and full represents the number of occupied slots in the buffer.
The Producer Operation:
The pseudocode of the producer function looks like this:

 Looking at the above code for a producer, we can see that a producer first waits until
there is atleast one empty slot.
 Then it decrements the empty semaphore because, there will now be one less empty
slot, since the producer is going to insert data in one of those slots.
 Then, it acquires lock on the buffer, so that the consumer cannot access the buffer
until producer completes its operation.
 After performing the insert operation, the lock is released and the value of full is
incremented because the producer has just filled a slot in the buffer.
The Consumer Operation:
The pseudocode for the consumer function looks like this:

 The consumer waits until there is atleast one full slot in the buffer.
 Then it decrements the full semaphore because the number of occupied slots will be
decreased by one, after the consumer completes its operation.
 After that, the consumer acquires lock on the buffer.
 Following that, the consumer completes the removal operation so that the data from
one of the full slots is removed.
 Then, the consumer releases the lock.
 Finally, the empty semaphore is incremented by 1, because the consumer has just
removed data from an occupied slot, thus making it empty.
Readers Writer Problem:-
Readers writer problem is another example of a classic synchronization problem.
There is a shared resource which should be accessed by multiple processes. There are two
types of processes in this context. They are reader and writer. Any number of readers can read
from the shared resource simultaneously, but only one writer can write to the shared resource.
When a writer is writing data to the resource, no other process can access the resource. A
writer cannot write to the resource if there are non zero number of readers accessing the
resource at that time.
So we have 4 major problems occurs in reader writer problem.
R-W-Problem
W-R-Problem
W-W-Problem
R-R-No Problem
The Solution:-
Solution Code for READER
Int rc=0
Semaphore a=1;
Semaphore db=1;
Void reader (void)
{
While(true)
{
Down(a);
rc=rc+1;
if(rc==1)then down(db);
up(a)
access DATABASE
Solution Code for WRITER
Void write(void)
{
While(true)
{
Down(db);
Access DATABASE
Up(db);
}
Exit code
{
Down (a)
rc=rc-1;
if(rc==0) then up(db);
up(a)
process_data
exit
}
DINING PHILOSOPHERS PROBLEM:
The dining philosophers problem is another classic synchronization problem which is used to
evaluate situations where there is a need of allocating multiple resources to multiple
processes.
What is the Problem Statement?
Consider there are five philosophers sitting around a circular dining table. The dining table
has five chopsticks and a bowl of rice in the middle as shown in the below figure.

At any instant, a philosopher is either eating or thinking. When a philosopher wants to eat, he
uses two chopsticks - one from their left and one from their right. When a philosopher wants
to think, he keeps down both chopsticks at their original place.
Here's the Solution;
From the problem statement, it is clear that a philosopher can think for an indefinite amount
of time. But when a philosopher starts eating, he has to stop at some point of time. The
philosopher is in an endless cycle of thinking and eating.
An array of five semaphores, stick[5], for each of the five chopsticks.
The code for each philosopher looks like:

When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks
up that chopstick. Then he waits for the right chopstick to be available, and then picks it too.
After eating, he puts both the chopsticks down.
But if all five philosophers are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they will be waiting for another chopstick
forever. The possible solutions for this are:
 A philosopher must be allowed to pick up the chopsticks only if both the left and right
chopsticks are available.
 Allow only four philosophers to sit at the table. That way, if all the four philosophers
pick up four chopsticks, there will be one chopstick left on the table. So, one
philosopher can start eating and eventually, two chopsticks will be available. In this
way, deadlocks can be avoided.

INTER PROCESS COMMUNICATION:


A process can be of two type:
 Independent process.
 Co-operating process.
An independent process is not affected by the execution of other processes while a co-
operating process can be affected by other executing processes. Though one can think that
those processes, which are running independently, will execute very efficiently but in
practical, there are many situations when co-operative nature can be utilised for increasing
computational speed, convenience and modularity. Inter process communication (IPC) is a
mechanism which allows processes to communicate each other and synchronize their actions.
The communication between these processes can be seen as a method of co-operation
between them. Processes can communicate with each other using these two ways:
 Shared Memory
 Message passing
An operating system can implement both method of communication. First, we will discuss
the shared memory method of communication and then message passing. Communication
between processes using shared memory requires processes to share some variable and it
completely depends on how programmer will implement it. One way of communication using
shared memory can be imagined like this: Suppose process1 and process2 are executing
simultaneously and they share some resources or use some information from other process,
process1 generate information about certain computations or resources being used and keeps
it as a record in shared memory. When process2 need to use the shared information, it will
check in the record stored in shared memory and take note of the information generated by
process1 and act accordingly. Processes can use shared memory for extracting information as
a record from other process as well as for delivering any specific information to other
process.
Let’s discuss an example of communication between processes using shared memory method.

1) Shared Memory Method:


Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. Producer produces some item and
Consumer consumes that item. The two processes shares a common space or memory
location known as buffer where the item produced by Producer is stored and from where the
Consumer consumes the item if needed. There are two version of this problem: first one is
known as unbounded buffer problem in which Producer can keep on producing items and
there is no limit on size of buffer, the second one is known as bounded buffer problem in
which producer can produce up to a certain amount of item and after that it starts waiting for
consumer to consume it. We will discuss the bounded buffer problem. First, the Producer and
the Consumer will share some common memory, then producer will start producing items. If
the total produced item is equal to the size of buffer, producer will wait to get it consumed by
the Consumer.
2) Messaging Passing Method
Now, We will start our discussion for the communication between processes via message
passing. In this method, processes communicate with each other without using any kind of of
shared memory. If two processes p1 and p2 want to communicate with each other, they
proceed as follow:
Establish a communication link (if a link already exists, no need to establish it again.)
Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destinaion) or send(message)
– receive(message, host) or receive(message)

The message size can be of fixed size or of variable size. if it is of fixed size, it is easy for OS
designer but complicated for programmer and if it is of variable size then it is easy for
programmer but complicated for the OS designer. A standard message can have two parts:
header and body.
The header part is used for storing Message type, destination id, source id, message length
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
Message Passing through Communication Link.
Now, We will start our discussion about the methods of implementing communication link.
While implementing the link, there are some questions which need to be kept in mind like :
 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?
Direct Communication links are implemented when the processes use specific process
identifier for the communication but it is hard to identify the sender ahead of time.
For example: the print server.
In-directed Communication is done via a shred mailbox (port), which consists of queue of
messages. Sender keeps the message in mailbox and receiver picks them up.
Synchronous and Asynchronous Messages Passing:
Synchronous Messages Passing:

Synchronous messaging involves a client that waits for the server to respond to a message.
Messages are able to flow in both directions, to and from. Essentially it means that
synchronous messaging is a two way communication. i.e. Sender sends a message to receiver
and receiver receives this message and gives reply to the sender. Sender will not send another
message until it gets a reply from receiver.
Asynchronous Messages Passing:
Asynchronous messaging involves a client that does not wait for a message from the server.
An event is used to trigger a message from a server. So even if the client is down, the
messaging will complete successfully. Asynchronous Messaging means that, it is a one way
communication and the flow of communication is one way only.

Process Generation:
A process may be created in the system for different operations. Some of the events that lead
to process creation are as follows:
User request for process creation
System Initialization
Batch job initialization
Execution of a process creation system call by a running process
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 Termination
 Process termination occurs when the process is terminated The exit() system call is
used by most operating systems for process termination.

 Some of the causes of process termination are as follows:


 A process may be terminated after its execution is naturally completed. This process
leaves the processor and releases all its resources.
 A child process may be terminated if its parent process requests for its termination.
 A process can be terminated if it tries to use a resource that it is not allowed to. For
example - A process can be terminated for trying to write into a read only file.
 If an I/O failure occurs for a process, it can be terminated. For example - If a process
requires the printer and it is not working, then the process will be terminated.
 In most cases, if a parent process is terminated then its child processes are also
terminated. This is done because the child process cannot exist without the parent
process.
 If a process requires more memory than is currently available in the system, then it is
terminated because of memory scarcity.

PROCESS SCHEDULING:
The act of determining which process is in the ready state, and should be moved to the
running state is known as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to
deliver minimum response time for all programs. For achieving this, the scheduler must apply
appropriate rules for swapping processes IN and OUT of CPU.
Scheduling fell into one of the two general categories:
 Non Pre-emptive Scheduling: When the currently executing process gives up the
CPU voluntarily.
 Pre-emptive Scheduling: When the operating system decides to favour another
process, pre-empting the currently executing process.
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:
Running:-When a new process is created, it enters into the system as in the running state.
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:
There are three types of schedulers available:
 Long Term Scheduler
 Short Term Scheduler
 Medium Term Scheduler
Long Term Scheduler:-
Long term scheduler runs less frequently. Long Term Schedulers decide which program must
get into the job queue. From the job queue, the Job Processor, selects processes and loads
them into the memory for execution. Primary aim of the Job Scheduler is to maintain a good
degree of Multiprogramming. An optimal degree of Multiprogramming means the average
rate of process creation is equal to the average departure rate of processes from the execution
memory.
Short Term Scheduler:-
This is also known as CPU Scheduler and runs very frequently. The primary aim of this
scheduler is to enhance CPU performance and increase process execution rate.
Medium Term Scheduler:-
This scheduler removes the processes from memory (and from active contention for the
CPU), and thus reduces the degree of multiprogramming. At some later time, the process can
be reintroduced into memory and its execution van be continued where it left off. This
scheme is called swapping. The process is swapped out, and is later swapped in, by the
medium term scheduler.
Context Switch:-
A context switch is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a
later time. Using this technique, a context switcher enables multiple processes to share a
single CPU. Context switching is an essential part of a multitasking operating system
features.
When the scheduler switches the CPU from executing one process to execute another, the
state from the current running process is stored into the process control block. After this, the
state for the process to run next is loaded from its own PCB and used to set the PC, registers,
etc. At that point, the second process can start executing.

CPU SCHEDULING:
CPU scheduling is a process which allows one process to use the CPU while the execution of
another process is on hold(in waiting state) due to unavailability of any resource like I/O etc,
thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient,
fast and fair.
Whenever the CPU becomes idle, the operating system 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 scheduler selects from among the processes in memory that are
ready to execute, and allocates the CPU to one of them.
Types of CPU Scheduling:
CPU scheduling decisions may take place under the following four circumstances:
1) When a process switches from the running state to the waiting state(for I/O request or
invocation of wait for the termination of one of the child processes).
2) When a process switches from the running state to the ready state (for example, when
an interrupt occurs).
3) When a process switches from the waiting state to the ready state(for example,
completion of I/O).
4) When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A new process(if one
exists in the ready queue) must be selected for execution. There is a choice, however in
circumstances 2 and 3.
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling
scheme is non-preemptive; otherwise the scheduling scheme is preemptive.
Non-Preemptive Scheduling:-
Under non-preemptive scheduling, once the CPU has been allocated to a process, the process
keeps the CPU until it releases the CPU either by terminating or by switching to the waiting
state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh
operating systems.
It is the only method that can be used on certain hardware platforms, because It does not
require the special hardware(for example: a timer) needed for preemptive scheduling.
Preemptive Scheduling:-
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is
necessary to run a certain task that has a higher priority before another task although it is
running. Therefore, the running task is interrupted for some time and resumed later when the
priority task has finished its execution.
SCHEDULING CRITERIA:-
There are many different criterias to check when considering the "best" scheduling algorithm,
they are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be working
most of the time(Ideally 100% of the time). Considering a real system, CPU usage should
range from 40% (lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit time or rather say total amount of work
done in a unit of time. This may range from 10/second to 1/hour depending on the specific
processes.
Turnaround Time

It is the amount of time taken to execute a particular process, i.e. The interval from time of
submission of the process to the time of completion of the process(Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been
waiting in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get
into the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is
produced. Remember, it is the time till the first response and not the completion of process
execution(final response).
In general CPU utilization and Throughput are maximized and other factors are reduced for
proper optimization.
Below are different time with respect to a process.
 Arrival Time: Time at which the process arrives in the ready queue.
 Completion Time: Time at which process completes its execution.
 Burst Time: Time required by a process for CPU execution.
 Turn Around Time: Time Difference between completion time and arrival time.
 Turn Around Time = Completion Time - Arrival Time

 Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time - Burst Time

SCHEDULING ALGORITHMS:
To decide which process to execute first and which process to execute last to achieve
maximum CPU utilisation, computer scientists have defined some algorithms, they are:
1) First Come First Serve(FCFS) Scheduling
2) Shortest-Job-First(SJF) Scheduling
3) Priority Scheduling
4) Round Robin(RR) Scheduling
First Come First Serve Scheduling
In the "First come first serve" scheduling algorithm, as the name suggests, the process which
arrives first, gets executed first, or we can say that the process which requests the CPU first,
gets the CPU allocated first.
 First Come First Serve, is just like FIFO(First in First out) Queue data structure,
where the data element which is added to the queue first, is the one who leaves the
queue first.
 This is used in Batch Systems.
 It's easy to understand and implement programmatically, using a Queue data structure,
where a new process enters through the tail of the queue, and the scheduler selects
process from the head of the queue.
 A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
Calculating Average Waiting Time
For every scheduling algorithm, Average waiting time is a crucial parameter to judge it's
performance.
AWT or Average waiting time is the average of the waiting times of the processes in the
queue, waiting for the scheduler to pick them for execution.
Lower the Average Waiting Time, better the scheduling algorithm.
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the
same order, with Arrival Time 0, and given Burst Time, let's find the average waiting time
using the FCFS scheduling algorithm.

The average waiting time will be 18.75 ms


For the above given proccesses, first P1 will be provided with the CPU resources,
 Hence, waiting time for P1 will be 0
 P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms
 Similarly, waiting time for process P3 will be execution time of P1 + execution time
for P2, which will be (21 + 3) ms = 24 ms.
 For process P4 it will be the sum of execution times of P1, P2 and P3.
The GANTT chart above perfectly represents the waiting time for each process
Problems with FCFS Scheduling
Below we have a few shortcomings or problems with the FCFS scheduling algorithm:

 It is Non Pre-emptive algorithm, which means the process priority doesn't matter.
 If a process with very least priority is being executed, more like daily routine backup
process, which takes more time, and all of a sudden some other high priority process
arrives, like interrupt to avoid system crash, the high priority process will have to
wait, and hence in this case, the system will crash, just because of improper process
scheduling.
 Not optimal Average Waiting Time.
 Resources utilization in parallel is not possible, which leads to Convoy Effect, and
hence poor resource(CPU, I/O etc) utilization.
Convoy Effect:
Convoy Effect is a situation where many processes, who need to use a resource for short time
are blocked by one process holding that resource for a long time.
SHORTEST JOB FIRST(SJF) SCHEDULING:-
Shortest Job First scheduling works on the process with the shortest burst time or duration
first.
 This is the best approach to minimize waiting time.
 This is used in Batch Systems.
 It is of two types:
1) Non Pre-emptive
2) Pre-emptive
 To successfully implement it, the burst time/duration time of the processes should be
known to the processor in advance, which is practically not feasible all the time.
 This scheduling algorithm is optimal if all the jobs/processes are available at the same
time. (either Arrival time is 0 for all, or Arrival time is same for all)
Non Pre-emptive Shortest Job First:
Consider the below processes available in the ready queue for execution, with arrival time as
0 for all and given burst times.

As you can see in the GANTT chart above, the process P4 will be picked up first as it has the
shortest burst time, then P2, followed by P3 and at last P1.
We scheduled the same set of processes using the First come first serve algorithm got average
waiting time to be 18.75 ms, whereas with SJF, the average waiting time comes out 4.5 ms.
Problem with Non Pre-emptive SJF
If the arrival time for processes are different, which means all the processes are not available
in the ready queue at time 0, and some jobs arrive after some time, in such situation,
sometimes process with short burst time have to wait for the current process's execution to
finish, because in Non Pre-emptive SJF, on arrival of a process with short duration, the
existing job/process's execution is not halted/stopped to execute the short job first.
This leads to the problem of Starvation, where a shorter process has to wait for a long time
until the current longer process gets executed. This happens if shorter jobs keep coming, but
this can be solved using the concept of aging.
Pre-emptive Shortest Job First
In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as they arrive, but
as a process with short burst time arrives, the existing process is preempted or removed from
execution, and the shorter job is executed first.

As you can see in the GANTT chart above, as P1 arrives first, hence it's execution starts
immediately, but just after 1 ms, process P2 arrives with a burst time of 3 ms which is less
than the burst time of P1, hence the process P1(1 ms done, 20 ms left) is preemptied and
process P2 is executed.
As P2 is getting executed, after 1 ms, P3 arrives, but it has a burst time greater than that of
P2, hence execution of P2 continues. But after another millisecond, P4 arrives with a burst
time of 2 ms, as a result P2(2 ms done, 1 ms left) is preemptied and P4 is executed.
After the completion of P4, process P2 is picked up and finishes, then P2 will get executed
and at last P1.
The Pre-emptive SJF is also known as Shortest Remaining Time First, because at any given
point of time, the job with the shortest remaining time is executed first.
Priority Scheduling:
 Priority is assigned for each process.
 Process with highest priority is executed first and so on.
 Processes with same priority are executed in FCFS manner.
 Priority can be decided based on memory requirements, time requirements or any
other resource requirement.

Round Robin Scheduling

 A fixed time is allotted to each process, called quantum, for execution.


 Once a process is executed for given time period that process is preemptied and other
process executes for given time period.
 Context switching is used to save states of preemptied processes.

MULTIPLE-PROCESSOR SCHEDULING:
In multiple-processor scheduling multiple CPU’s are available and hence Load Sharing
becomes possible. However multiple processor scheduling is more complex as compared to
single processor scheduling. In multiple processor scheduling there are cases when the
processors are identical i.e. HOMOGENEOUS, in terms of their functionality, we can use
any processor available to run any process in the queue.
Approaches to Multiple-Processor Scheduling –
One approach is when all the scheduling decisions and I/O processing are handled by a single
processor which is called the Master Server and the other processors executes only the user
code. This is simple and reduces the need of data sharing. This entire scenario is called
Asymmetric Multiprocessing.
A second approach uses Symmetric Multiprocessing where each processor is self scheduling.
All processes may be in a common ready queue or each processor may have its own private
queue for ready processes. The scheduling proceeds further by having the scheduler for each
processor examine the ready queue and select a process to execute.
Processor Affinity –
Processor Affinity means a processes has an affinity for the processor on which it is currently
running.
When a process runs on a specific processor there are certain effects on the cache memory.
The data most recently accessed by the process populate the cache for the processor and as a
result successive memory access by the process are often satisfied in the cache memory. Now
if the process migrates to another processor, the contents of the cache memory must be
invalidated for the first processor and the cache for the second processor must be repopulated.
most of the SMP(symmetric multiprocessing) systems try to avoid migration of processes
from one processor to another and try to keep a process running on the same processor. This
is known as PROCESSOR AFFINITY.
There are two types of processor affinity:

 Soft Affinity – When an operating system has a policy of attempting to keep a


process running on the same processor but not guaranteeing it will do so, this situation
is called soft affinity.
 Hard Affinity – Some systems such as Linux also provide some system calls that
support Hard Affinity which allows a process to migrate between processors
Load Balancing –
Load Balancing is the phenomena which keeps the workload evenly distributed across all
processors in an SMP system. Load balancing is necessary only on systems where each
processor has its own private queue of process which are eligible to execute. Load balancing
is unnecessary because once a processor becomes idle it immediately extracts a runnable
process from the common run queue. On SMP(symmetric multiprocessing), it is important to
keep the workload balanced among all processors to fully utilize the benefits of having more
than one processor else one or more processor will sit idle while other processors have high
workloads along with lists of processors awaiting the CPU.
There are two general approaches to load balancing :
 Push Migration – In push migration a task routinely checks the load on each
processor and if it finds an imbalance then it evenly distributes load on each
processors by moving the processes from overloaded to idle or less busy processors.
 Pull Migration – Pull Migration occurs when an idle processor pulls a waiting task
from a busy processor for its execution.
Multicore Processors –
In multicore processors multiple processor cores are places on the same physical chip. Each
core has a register set to maintain its architectural state and thus appears to the operating
system as a separate physical processor. SMP systems that use multicore processors are faster
and consume less power than systems in which each processor has its own physical chip.
There are two ways to multithread a processor :
 Coarse-Grained Multithreading – In coarse grained multithreading a thread
executes on a processor until a long latency event such as a memory stall occurs,
because of the delay caused by the long latency event, the processor must switch to
another thread to begin execution. The cost of switching between threads is high as
the instruction pipeline must be terminated before the other thread can begin
execution on the processor core. Once this new thread begins execution it begins
filling the pipeline with its instructions.
 Fine-Grained Multithreading – This multithreading switches between threads at a
much finer level mainly at the boundary of an instruction cycle. The architectural
design of fine grained systems include logic for thread switching and as a result the
cost of switching between threads is small.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy