Professional Documents
Culture Documents
UNIT 2 and UNIT 3 2021-22
UNIT 2 and UNIT 3 2021-22
UNIT 2 and UNIT 3 2021-22
PROCESS
A process is basically a program in execution. The execution of a process must progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be implemented in the system.
To put it in simple terms, we write our computer programs in a text file and when we execute this program, it
becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─
stack, heap, text and data. The following image shows a simplified layout of a process inside main memory −
PROCESS STATE
As process executes, it change state. The state of a process is defined in part by the current activity of that
process. Each process may be in one of the following states:
terminate
d
new
admitted interrupt exit
ready running
Scheduler/ dispatch
waiting
These state names are arbitrary, and they vary across operating systems. The states that they represent are found
on all systems, however. Certain operating systems more finely delineate process states. Only one process can
be running on any processor at any instant, although many processes may be ready and waiting.
Each processes is represented in the operating system by a process control block (PCB) - also called a task
control block. A PCB is shown in figure A. It contains many pieces of information associated with a specific
process, including these:
Process number
Program counter
Registers
2
Memory limits
List of open files
Process state: The state may be new, ready, running, waiting, halted, and so on.
Program counter: The counter indicates the address of the next instruction to be executed for this
process.
CPU registers: The registers vary in number and type, depending on the computer architecture. They
include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition-
code information. Along with the program counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly afterward (Figure B).
CPU-scheduling information: This information includes a process priority, pointers to scheduling
queues, and any other scheduling parameters.
Memory-management information: This information may include such information as the base and
limit registers, the page tables, or the segment tables, depending on the memory system used by the
operating system.
Accounting information: This information includes the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
I/O status information: The information includes the list of I/O devices allocated to this process, a list
of open files, and so on.
PROCESS SWITCHING
Switching from one process to another in a system requires saving the state of the old process and loading the
saved state for the new process. This task is known as context switching. The context of a process is
represented in the PCB of a process that includes various information such as the value of CPU registers, the
process state, memory management information, scheduling information etc. when a context switch occurs, the
kernel saves the context of old process in its PCB and load the context of next (new) process that is currently
scheduled to run. The switching time between two processes is pure overhead, because the system does no
useful work, while switching. Its speed varies from machine to machine, depending on the memory speed, the
number of registers that must be copies, and the existence of special instruction to store OR load registers.
Typical speed ranges from 1 to 1000 microseconds.
Some processors provide multiple sets of registers. Also, some operating systems are more complex. In these
situations, more work is required to be done during switching.
Switching can take place only when the operating system has control of the system. An operating system can
acquire control by:
Interrupt
3
Trap
System call
The main objective of process switching is to interrupt a running process and assign the control of an operating
system to a different process which wants to execute to accomplish task.
Executing
Save state into PCB0
: Idle
Idle executing
: Idle
Executing
Note: context switching has become such a performance bottleneck that programmes are using new structures
(Threads) to avoid it whenever possible.
4
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.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems allow
more than one process to be loaded into the executable memory at a time and the loaded process shares the CPU
using time multiplexing.
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.
As processes enter the system, they are put into a job queue. This queue consists of all processes in the system.
The processes that are residing in main memory and are ready and waiting to execute are kept on a list called
the ready queue. This queue is generally stored as a linked list.
The operating system also has other queues. When a process is allocated the CPU, it executes for a while and
eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the completion of an
I/O request. In the case of an I/O request, such a request may be to a dedicated tape drive, or to a shared device,
such as a disk. Since the system has many processes, the disk may be busy with the I/O request of some other
process. The process therefore may have to wait for the disk. The list of processes waiting for a particular I/O
device is called a device queue. Each device has its own device queue.
A common representation of process scheduling is a queuing diagram, such as that in Figure C. each
rectangular box represents a queue. Two types of queues are present: the ready queue and a set of device
queues. The circles represent the resources that serve the queues, and the arrows indicate the flow of processes
in the system.
A new process is initially put in the ready queue. It waits in the ready queue until it is selected for execution.
Once the process is assigned to the CPU and is executing, one of several events could occur:
The process could issue an I/O request, and then be placed in an 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.
Exit
Job CPU
Ready queue
queue
5
Time slice expired
Child
Fork a child
execute
s
In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put
back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
Note: 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.
SCHEDULERS
A process migrates between the various scheduling queues throughout its lifetime. The operating system must
select, for scheduling purposes, processes from these queues in some fashion. The selection process is carried
out by the appropriate scheduler.
Schedulers are special system software which handle process scheduling in various ways. Their main task is to
select the jobs to be submitted into the system and to decide which process to run. Schedulers are of three types:
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
6
If all processes are CPU bound, the I/O waiting queue will almost always be empty, devices will go unused, and
again the system will be unbalanced. The system with the best performance will have a combination of CPU-
bound and I/O-bound processes.
The long-term scheduler also controls the degree of multiprogramming-the number of process in memory. 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.