Process Management in Operating Systems
Process Management in Operating Systems
UNIT-2
Process Concept: Process scheduling, Operations on processes, Inter-
process communication, Communication in client server systems.
Multithreaded Programming: Multithreading models, Thread libraries,
Threading issues.
Process Scheduling: Basic concepts, Scheduling criteria, Scheduling
algorithms, Multiple processor scheduling, Thread scheduling.
Inter-process Communication: Race conditions, Critical Regions, Mutual
exclusion with busy waiting, Sleep and wakeup, Semaphores, Mutexes,
Monitors, Message passing, Barriers.
Classical IPC Problems - Dining philosophers problem, Readers and writers
problem.
Q) Write a short note on Process Concept?
A process is a program in execution.
A process generally also includes the process stack, which contains
temporary data (such as function parameters, return addresses, and local
variables), and a data section, which contains global variables. The structure
of a process in memory is shown in Figure
1|
KITS/CAI OS-UNIT-2
2|
KITS/CAI OS-UNIT-2
Note: The Random Access Memory (RAM) can be logically divided into
two distinct regions namely - the kernel space and the user space.
PCB is unique for every process which consists of various attributes
such as process ID, priority, registers, program counters, process states, list
of open files, etc.
Structure of Process Control Block
The process control block contains many attributes such as process ID,
process state, process priority, accounting information, program counter,
CPU registers`, etc for each process.
3|
KITS/CAI OS-UNIT-2
1. Process ID:
When a new process is created by the user, the operating system
assigns a unique ID i.e a process
process-ID
ID to that process. This ID helps
h the
process to be distinguished from other processes existing in the system.
2. Process states:
3. Process Priority:
Process priority is a numeric value that represents the priority of each
process. The lesser the value, the greater the priority of that process. This
priority is assigned at the time of the creation of the PCB and may depend
4|
KITS/CAI OS-UNIT-2
on many factors like the age of that process, the resources consumed, and
so on. The user can also externally assign a priority to the process.
4. Process Accounting Information:
This attribute gives the information of the resources used by that
process in its lifetime. For Example: CPU time connection time, etc.
5. Program Counter:
The program counter is a pointer that points to the next instruction in
the program to be executed. This attribute of PCB contains the address of
the next instruction to be executed in the process.
6. CPU registers:
A CPU register is a quickly accessible small-sized location available to
the CPU. These registers are stored in virtual memory(RAM).
6. Context Switching:
A context switching is a process that involves switching the CPU from one
process or task to another. It is the process of storing the state of a process
so that it can be restored and resume execution at a later point. This allows
multiple processes to share a single CPU and is an essential feature of a
multitasking operating system.
So, whenever context switching occurs in the code execution then the
current state of that process is stored temporarily in CPU registers. This
helps in the fast execution of the process by not wasting time-saving and
retrieving state information from the secondary memory (hard disk).
8. PCB pointer:
This field contains the address of the next PCB, which is in ready state.
This helps the operating system to hierarchically maintain an easy control
flow between parent processes and child processes.
5|
KITS/CAI OS-UNIT-2
6|
KITS/CAI OS-UNIT-2
Positive value - The process ID of the child process to the parent. The
returned process ID is type pid_t defined in sys/types.h. Usually, the process
ID is an integer.
2. Process Termination
There are two methods a process can terminate:
Normal termination – A process finishes executing its final statement. All the
resources allocated to it are freed by the operating system.
Forced Termination – a parent process can terminate its child process by
invoking the appropriate system call. The paren
parentt can terminate the child due
to the following reasons:
Child exceeds its usage of resources
Task assigned to the child is no longer required
Parent exits and OS does not allow child to run if parent terminates
Example: kii and exit system calls can be use
usedd for process forced
termination
Q) Discus about the IPC( Inter process communication)?
communication)
Interprocess communication is the mechanism provided by the operating
system that allows processes to communicate with each other.
There are several reasons for providing an environment that allows process
cooperation:
Here, are the reasons for using the interprocess communication protocol for
information sharing:
It helps to speedup modularity
Computational
7|
KITS/CAI OS-UNIT-2
Privilege separation
Convenience
Helps operating system to communicate with each other and
synchronize their actions.
There are two fundamental models of interprocess communication:
(1) Shared memory and
(2) Message passing.
(1) Shared memory:
Shared memory system is one of the fundamental models of interprocess
communication. In the shared memory system, the cooperating processes
communicate with each other by establishing the shared memory region, in
its address space. Shared memory model allows the fastest interprocess
communication.
Working
In Shared Memory system, the cooperating processes communicate, to
exchange the data or the information with each other. For this, the
cooperating processes establish a shared region in their memory. The
processes share data by reading and writing the data in the shared segment
of the processes.
Consider a scenario, there are two cooperating processes P1 and P2. Both
the processes P1 and P2, have their different address spaces. Now, P1 wants
to share some data with P2. So, P1 and P2 will have to perform the following
steps.
Step 1: As the process P1 has some data, to share with process P2. Process
P1 has to take the initiative and establish a shared memory region in its own
address space and store the data or information to be shared in it’s shared
memory region.
8|
KITS/CAI OS-UNIT-2
9|
KITS/CAI OS-UNIT-2
In this calls, the sender and receiver processes address each other by
names. Mode of communication between two process can take place through
two methods
1) Direct Addressing
2) Indirect Addressing
Direct Addressing:
In this type that two processes need to name other to communicate. This
become easy if they have the same parent.
Example
If process A sends a message to process B, then
send(B, message);
Receive(A, message);
By message passing a link is established between A and B. Here the receiver
knows the Identity of sender message destination. This type of arrangement
in direct communication is known as Symmetric Addressing.
Indirect addressing:
In this message send and receive from a mailbox. A mailbox can be
abstractly viewed as an object into which messages may be placed and from
10 |
KITS/CAI OS-UNIT-2
11 |
KITS/CAI OS-UNIT-2
12 |
KITS/CAI OS-UNIT-2
13 |
KITS/CAI OS-UNIT-2
Kernel threads are supported within the kernel of the OS itself. All modern
OSs support kernel-level threads, allowing the kernel to perform multiple
simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the
following strategies:
1. Many to One Model
2. One to One Model
3. Many to Many Model
Many to One Model
In the many to one model, many user-level threads are all mapped onto a
single kernel thread.
14 |
KITS/CAI OS-UNIT-2
15 |
KITS/CAI OS-UNIT-2
16 |
KITS/CAI OS-UNIT-2
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:
Preemptive Scheduling is a CPU scheduling technique that works by
dividing time slots of CPU to a given process. The time slot given might be
able to complete the whole process or might not be able to it.
Non-preemptive Scheduling is a CPU scheduling technique the process
takes the resource (CPU time) and holds it till the process gets terminated or
is pushed to the waiting state. No process is interrupted until it is completed,
and after that processor switches to another process.
Q) Explain the different types Scheduling Queues?
All processes, upon entering into the system, are stored in the Job Queue.
Processes in the “Ready” state are placed in the Ready Queue.
Processes waiting for a device to become available are placed in Device
Queues. There are unique device queues available for each I/O device.
A new process is initially put in the Ready queue. It waits in the ready
queue until it is selected for execution (or dispatched). Once the process is
assigned to the CPU and is executing, one of the following several events
can occur:
The process could issue an I/O request, and then be placed in the I/O
queue.
The process could create a new sub process and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
17 |
KITS/CAI OS-UNIT-2
1. Every new process first put in the Ready queue .It waits in the ready
queue until it is finally processed for execution. Here, the new process
is put in the ready queue and wait until it is selected for execution or it
is dispatched.
2. One of the processes is allocated the CPU and it is executing
3. The process should issue an I/O request
4. Then, it should be placed in the I/O queue.
5. The process should create a new sub process
6. The process should be waiting for its termination.
7. It should remove forcefully from the CPU, as a result interrupt. Once
interrupt is completed, it should be sent back to ready queue.
Types of Schedulers
There are three types of schedulers available:
Long Term Scheduler
Short Term Scheduler
Medium Term Scheduler
Let's discuss about all the different types of Schedulers in detail:
18 |
KITS/CAI OS-UNIT-2
below diagram:
19 |
KITS/CAI OS-UNIT-2
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 lat
later
er 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
execut
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.
20 |
KITS/CAI OS-UNIT-2
21 |
KITS/CAI OS-UNIT-2
Process Termination
By making the exit(system call), typically returning an integer, processes
may request their own termination. This int is passed along to the parent if it
is doing a wait(), and is typically zero on successful completion and some
non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons,
including :
The inability of the system to deliver the necessary system resources.
In response to a KILL command or other unhandled process interrupts.
A parent may kill its children if the task assigned to them is no longer
needed.
Q) Explain the various scheduling algorithms?
There are various CPU scheduling algorithms are available. They are
1. First Come First Serve:
FCFS considered to be the simplest of all operating system scheduling
algorithms. First come first serve scheduling algorithm states that the
process that requests the CPU first is allocated the CPU first and is
implemented by using FIFO queue.
Characteristics of FCFS:
FCFS supports non-preemptive and preemptive CPU scheduling algorithms.
Tasks are always executed on a First-come, First-serve concept.
FCFS is easy to implement and use.
This algorithm is not much efficient in performance, and the wait time is
quite high.
Advantages of FCFS:
Easy to implement
First come, first serve method
Disadvantages of FCFS:
22 |
KITS/CAI OS-UNIT-2
23 |
KITS/CAI OS-UNIT-2
24 |
KITS/CAI OS-UNIT-2
If two jobs having the same priority are READY, it works on a FIRST
COME, FIRST SERVED basis.
In priority scheduling, a number is assigned to each process that
indicates its priority level.
Lower the number, higher is the priority.
In this type of scheduling algorithm, if a newer process arrives, that is
having a higher priority than the currently running process, then the
currently running process is preempted.
Interprocess communications:
Race conditions:
Each of the processes has some sharable resources and some non-shareable
resources. The sharable resources can be shared among the cooperating
processes.
The non-cooperating processes don’t need to share the resources. Then
what is the race condition. When we synchronize the processes and the
synchronization is not proper then the race condition occurs.
How to Identify Race Conditions:
Before we understand how to identify possible race scenarios, let us
understand what a critical section of a code is:
Critical Section
A critical section of a code is that part that is executed by multiple threads.
Different orders of execution across threads that run concurrently can lead
to different outputs, which make the critical section susceptible to a race
condition.
For example:
Consider the operation of adding money to your bank account. Let us say
you are doing so using two different apps simultaneously. The following
steps take place while doing so:
The app reads your current balance
25 |
KITS/CAI OS-UNIT-2
26 |
KITS/CAI OS-UNIT-2
27 |
KITS/CAI OS-UNIT-2
28 |
KITS/CAI OS-UNIT-2
29 |
KITS/CAI OS-UNIT-2
(2) Then, the consumer will remove a widget from the buffer. The consumer
will never try to remove a widget from an empty buffer because it will not
wake up until the buffer is full.
(3) If the buffer was full before it removed the widget, the consumer will
wake the producer.
(4) Finally, the consumer will consume the widget. As was the case with the
producer, an interrupt could occur between any of these steps, allowing the
producer to run.
Pseudo code for above problem:
Semaphores:
Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for
process synchronization.
In semaphore there are two types of operations down and up. In down
operation we perform decrement operation and up operation performs
increment operation.
30 |
KITS/CAI OS-UNIT-2
Types of semaphores:
There are 2 types of semaphores.
1. Counting semaphore
2. Binary semaphore
Counting Semaphores:
Counting semaphore can be used to manage the resources for allotting the
number of processes.
There are the scenarios in which more than one processes need to execute
in critical section simultaneously. However, counting semaphore can be used
when we need to have more than one process in the critical section at the
same time.
Consider there are P1, P2, P3, P4 and P5 processes wants to resources
R1,R2 and R3.
R1 R2 R3
DOWN UP
As per the schedule concept we assign the process “P1” assign to R1,
And it perform down operation.
In the same way R2 assigned to P2, R3 assigned P3, both process
performing down operation.
Let us assume the structure of semaphore as follows:
struct Semaphore
{
int value; // processes that can enter in the critical section simultaneously.
queue type L; // L contains set of processes which get blocked
}
31 |
KITS/CAI OS-UNIT-2
Down (Semaphore S)
{
[Link] = [Link] - 1; //semaphore's value will get decreased when a new
//process enter in the critical section
if ([Link]< 0)
{
put_process(PCB) in L; //if the value is negative then
//the process will get into the blocked state.
Sleep();
}
else
return;
}
up (Semaphore s)
{
[Link] = [Link]+1; //semaphore value will get increased when
//it makes an exit from the critical section.
if([Link]<=0)
{
select a process from L; //if the value of semaphore is positive
//then wake one of the processes in the blocked queue.
wake-up();
}
}
}
32 |
KITS/CAI OS-UNIT-2
perform the up operation it check is there any process wait in waiting queue.
If there is process, then resources allotted it. Otherwise it performs the up
operation.
Binary semaphore:
BS contains only 2 values (0,1) use to deal with critical section for multiple
process. it performs two atomic operations they are down and up. BS can be
used to deal with critical section problems. Let us consider
P1P1
Process P2 also wants try to enter into critical section it performs the "down"
operation in which we check the value of BS. If its “value” is "1" then we
allow it in CRITICAL SECTION. Otherwise we place it into waiting queue.
Process P3 also wants try to enter into critical section it performs the "down"
operation in which we check the value of BS. If its value is "1" then we allow
it in CRITICAL SECTION. Otherwise we place it into waiting queue.
Process P4 also wants try to enter into critical section it performs the “down”
operation in which we check the value of BS. If its value is "1" then we allow
it in CRITICAL SECTION. Otherwise we place it into waiting queue.
33 |
KITS/CAI OS-UNIT-2
struct BSemaphore
{
enum value{0,1}
}s;
DOWN(struct BSemaphore s)
{
if([Link]==1)
{
[Link]=0
}
else
{
put the process in S.L(waiting queue) & block it
}
}
UP(struct BSemaphore s)
{
if(S.L is empty)
{
[Link]=1;
}
else
{
select a process from S.L & wakeup()
}
}
34 |
KITS/CAI OS-UNIT-2
void producer(void)
{
int itemp,in=0;
while(1)
{
Produce_item(itemp);
down(empty);
/* requesting for buffer to place item. N=100-1=99*/
down(mutex);
/* mutex value changed to 1 to 0*/
buffer[in]=itemp;
in=(in+1) mod N;
35 |
KITS/CAI OS-UNIT-2
up(mutex);
/* mutex value changed to 0 to 1*/
up(full);
/* full value changed full=full+1, 0+1=1
}
}
void consumer(void)
{
int itempc,out=0;
while(1)
{
down(full);
/* reqeusting for consuming the item*/
full=full-1=1-1=0
down(mutex);
/* mutex value changed to 1 to 0
itemc=buffer[out];
out=(out+1)mod N
up(mutex)
/* mutex value changed to 0 to 1
up(empty);
/* empty value incremented so 99+1=100*/
process_item(itemc);
}
}
36 |
KITS/CAI OS-UNIT-2
37 |
KITS/CAI OS-UNIT-2
38 |
KITS/CAI OS-UNIT-2
Monitors
Monitors are used for process synchronization. With the help of
programming languages, we can use a monitor to achieve mutual exclusion
among the processes. Example of monitors: Java Synchronized
methods such as Java offers notify() and wait() constructs.
Characteristics of Monitors.
1. Inside the monitors, we can only execute one process at a time.
2. Monitors are the group of procedures, and condition variables that are
merged together in a special type of module.
3. If the process is running outside the monitor, then it cannot access the
monitor’s internal variable. But a process can call the procedures of
the monitor.
4. Monitors offer high-level of synchronization
5. Monitors were derived to simplify the complexity of synchronization
problems.
6. There is only one process that can be active at a time inside the
monitor.
Components of Monitor
There are four main components of the monitor:
1. Initialization
2. Private data
3. Monitor procedure
4. Monitor entry queue
Initialization: - Initialization comprises the code, and when the monitors
are created, we use this code exactly once.
Private Data: - Private data is another component of the monitor. It
comprises all the private data, and the private data contains private
procedures that can only be used within the monitor. So, outside the
monitor, private data is not visible.
39 |
KITS/CAI OS-UNIT-2
Condition Variables
There are two types of operations that we can perform on the condition
variables of the monitor:
1. Wait
2. Signal
Suppose there are two condition variables
condition a, b // Declaring variable
Wait Operation
[Link](): - The process that performs wait operation on the condition
variables are suspended and locate the suspended process in a block queue
of that condition variable.
Signal Operation
[Link]() : - If a signal operation is performed by the process on the
condition variable, then a chance is provided to one of the blocked
processes.
Advantages of Monitor
It makes the parallel programming easy, and if monitors are used, then
there is less error-prone as compared to the semaphore.
40 |
KITS/CAI OS-UNIT-2
Example:
41 |