OS Chapter 3 (2020)
OS Chapter 3 (2020)
Process Concept
Process Scheduling
Operations on Processes
Multiple programming requires firmer control which gave rise to the concept of
process which is a unit of work in a modern time-sharing system.
A Program does nothing unless its instructions are executed by a CPU. A program in
execution is called a process. In order to accomplish its task, process needs the
computer resources.
There may exist more than one process in the system which may require the same
resource at the same time. Therefore, the operating system has to manage all the
processes and the resources in a convenient and efficient way.
Some resources may need to be executed by one process at one time to maintain
the consistency otherwise the system can become inconsistent and deadlock may
occur.
Program VS Process
A program is a passive entity, for example, a file accommodating a group of instructions to
be executed (executable file). It is so called because it doesn’t perform any action by itself, it
has to be executed to realize the actions specified in it.
The address space of a program is composed of the instruction, data and stack. Assume P is
the program we are writing, to realize execution of P, the operating system allocates
memory to accommodate P’s address space.
PROCESS
Process is an execution of a program. It is considered as an active entity and
realizes the actions specified in a program.
The nature of the program is passive as it does nothing until it gets executed whereas a
process is dynamic or active in nature as it is an instance of executing program and
perform the specific action.
A program has a longer lifespan because it is stored in the memory until it is not manually
deleted while a process has a shorter and limited lifespan because it gets terminated
after the completion of the task.
The resource requirement is much higher in case of a process; it could need processing,
memory, I/O resources for the successful execution. In contrast, a program just requires
memory for storage.
PROCESS
A stack contains temporary
data such as function calls,
local variables etc.
Waiting- Process moves into the waiting state if it needs to wait for a
resource, such as waiting for user input, or waiting for a file to become
available.
Program Counter- Program Counter is a pointer to the address of the next instruction to
be executed for this process.
CPU registers- Various CPU registers where process need to be stored for execution for
running state.
CPU Scheduling Information- Process priority and other scheduling information which is
required to schedule the process.
Accounting information- This includes the amount of CPU used for process execution,
time limits, execution ID etc.
Overview:
The process scheduling is the activity of the process manager that handles the removal
of the running process from the CPU and the selection of another process on the basis of
a particular strategy.
Job queue − This queue keeps all the processes in the system.
It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing.
It selects processes from the queue and loads them into memory for execution. Process
loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as
I/O bound and processor bound. It also controls the degree of multiprogramming. If the
degree of multiprogramming is stable, then the average rate of process creation must be
equal to the average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-
sharing operating systems have no long term scheduler. When a process changes the
state from new to ready, then there is use of long-term scheduler.
Short-Term Scheduler
It is also called as CPU scheduler. Its main objective is to increase system performance
in accordance with the chosen set of criteria.
It is the change of ready state to running state of the process. CPU scheduler selects a
process among the processes that are ready to execute and allocates CPU to one of
them.
Short-term schedulers, also known as dispatchers, make the decision of which process
to execute next. Short-term schedulers are faster than long-term schedulers.
Medium-term Scheduler
Medium-term scheduling is a part of swapping. It removes the processes from the
memory. It reduces the degree of multiprogramming.
2 Speed is less than short term scheduler Speed is very fast Speed is in between both
3 It controls degree of multiprogramming Less control over degree of Reduce the degree of
multiprogramming multiprogramming.
4 Absent or minimal in time sharing Minimal in time sharing system. Time sharing system use
system. medium term scheduler.
5 It select processes from pool and load It select from among the processes Process can be reintroduced
them into memory for execution. that are ready to execute. into memory and its execution
can be continued.
7 Select a good process, mix of I/O bound Select a new process for a CPU -
and CPU bound. quite frequently.
Context Switch
CS 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 switches are computationally intensive since register and memory state must be
saved and restored. To avoid the amount of context switching time, some hardware
systems employ two or more sets of processor registers.
When the process is switched, the following information(PCB) is stored for later use.
● Program Counter ● Scheduling information ● Base and limit register value ● Currently used
register ● Changed State ● I/O State information ● Accounting information
Diagram showing CPU switch from process0
to process1
Operations on Process
There are many operations that can be performed on processes.
Some of these are:
Process creation
process preemption
process blocking
process termination.
Process creation
The sched process also creates init process which serves as root parent for all user
processes.
inetd is responsible for networking services like telnet and ftp and dtlogin is process
representing a user login screen.
When a user logs in, dtlogin creates an X-windows session which in turn creates the
sdt_shel process. Below it a C-shell or csh is created in which the user can invoke various
processes using ls and cat commands.
There is another process csh with pid 7778 representing a user who has logged onto the
system using telnet. This user has started the Netscape browser(pid of 7785) and the
emacs editor(pid of 8105).
Process Creation
Parent process create children processes, which, in turn create other processes,
forming a tree of processes
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
Process creation
Processes need to be created in the system for different operations. This can be done by the
following events −
A child process can have only one parent but a parent process may have many children.
Process creation
Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process).
After a new child process is created, both processes will execute the next instruction
following the fork() system call.
FORK()
It takes no parameters and returns an integer value. Below are different values returned by
fork().
#include <stdio.h>
#include <unistd.h>
int main()
{
// make two process which run same statements after this instruction
fork();
printf("Hello world!\n");
return 0;
}
Output:
Hello world!
Hello world!
//Program when child and parent execute different code
#include <stdio.h>
#include <unistd.h>
int main()
{
int n= fork();
If(n<0)
{cout<<“fork failed”;}
else if (n==0)
{
cout<<“child process”;
cout<<getpid();
}
else
{ cout<<“parent process”;
cout<<getpid();
}
return 0;
}
//Program when parent will wait until child finishes its execution
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int n= fork();
If(n<0)
{cout<<“fork failed”;}
else if (n==0)
{
cout<<“child process”;
cout<<getpid();
}
else
{ wait(NULL);
cout<<“parent process”;
cout<<getpid();
}
return 0;
}
//program to execute different programs after exec()
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
execlp(“bin/ls”,”ls”,NULL);
execlp(“./f1”, ””,NULL);
return 0;
}
EXAMPLE OF FORK()
Q2. Calculate number of times hello is printed:
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
The number of times ‘hello’ is printed is equal to number of process created. Total Number
of Processes = 2n, where n is number of fork system calls. So here n = 3, 2 3 = 8
EXAMPLE OF FORK()
Let us put some label names for the three lines:
So there are total eight processes (new child processes and one original process).
EXAMPLE OF FORK()
If we want to represent the relationship between the processes as a tree hierarchy it would
be the following:
The main process: P0
Processes created by the 1st fork: C1
Processes created by the 2nd fork: C2, C3
Processes created by the 3rd fork: C4, C5, C6, C7
void forkexample()
{ Here, global variable change in one process
int x = 1; does not affected two other processes
because data/state of two processes are
if (fork() == 0) different. And also parent and child run
printf("Child has x = %d\n", ++x); simultaneously so two outputs are possible.
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
EXERCISE 1:
A process executes the following code
for (i = 0; i < n; i++)
fork();
The total number of child processes created is: (GATE-CS-2008)
(A) n
(B) 2^n – 1
(C) 2^n
(D) 2^(n+1) – 1;
If we sum all levels of above tree for i = 0 to n-1, we get 2n - 1. So there will be 2n – 1 child
processes. On the other hand, the total number of process created are (number of child
processes)+1.
Fork() Vs Exec()
Fork() Exec()
Creates a new process by duplicating tha It replaces the current process image
calling process with a new process image
After fork system calls both child and Control is never transferred to the
parent are executed simultaneously original process, unless there is an error
Child and parent have unique process-id New program is loaded in the current
process space & process Id remain same
-fork() -execl(), execv()
fork() vs exec()
The fork system call creates a new process. The new process created by fork() is a copy
of the current process except for the returned value.
The exec() system call replaces the current process with a new program.
The exec family of functions replaces the current running process with a new process.
It comes under the header file unistd.h. There are many members in the exec family
which are shown below with examples.
The exec() system call is also used to create processes. But there is one big difference
between fork() and exec() calls. The fork() call creates a new process while preserving
the parent process. But, an exec() call replaces the address space, text segment, data
segment etc. of the current process with the new process.
It means, after an exec() call, only the new process exists. The process which made the
system call, wouldn't exist.
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
Output data from child to parent (via wait)
Process’ resources are deallocated by operating system
The parent is exiting and the operating system does not allow a child to continue if its
parent terminates. In this case, all its children are also terminated. This phenomenon is
known as cascading termination.
All the resources of the process- including physical and virtual memory, open files, and
I/O buffers are deallocated by the operating system.
Other circumstances of termination:
The child has exceeded its usage of some of the resources that it has been allocated.
The parent is exiting and the operating system does not allow a child to continue if its
parent terminates. In this case, all its children are also terminated. This phenomenon is
known as cascading termination.
Priority 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
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.
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.
Example 1. For the given problem draw the Gantt chart and calculate average
waiting and turnaround time using Non Pre-emptive Priority scheduling. Here
we are considering 1 is the lowest priority.
Process Arrival time Burst Time Priority TAT=Exit time Waiting
Id. –waiting time=TAT-BT
time(ET-WT)
P1 0 4 2 15-0=15 15-4=11
P2 1 3 3 12-1=11 11-3=8
P3 2 1 4 3-2=1 1-1=0
P4 3 5 5 8-3=5 655=0
P5 4 2 5 10-4=6 6-2=4
P1 P2 P3 P4 P5 P2 P1
0 1 2 3 8 10 12 15