You are on page 1of 20

GROW MORE FACULTY OF ENGINEERING

UNIT – 2 MATERIAL
SUB : OS

◼ Difference between Program and Processing


Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
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
◦ There are mainly Five states of process
Suspended Processes

Reasons for Suspension of process

1. Swapping 2. Timing 3.Interactive User Request 4. Parent Process Request


Process Control Block(PCB)
Process Control Block is a data structure that contains information of the process related to it. The
process control block is also known as a task control block, entry of the process table, etc.
It is very important for process management as the data structuring for processes is done in terms
of the PCB. It also defines the current state of the operating system.
While creating a process the operating system performs several operations. To identify these
process, it must identify each process, hence it assigns a process identification number (PID) to
each process. As the operating system supports multi-programming, it needs to keep track of all
the processes. For this task, the process control block (PCB) is used to track the process’s execution
status. Each block of memory contains information about the process state, program counter, stack
pointer, status of opened files, scheduling algorithms, etc. All these information is required and
must be saved when the process is switched from one state to another. When the process made
transitions from one state to another, the operating system must update informationin the process’s
PCB.
A process control block (PCB) contains information about the process, i.e. registers, quantum,
priority, etc. The process table is an array of PCB’s, that means logically contains a PCB for all of
the current processes in the system.

• Process state – It stores the respective state of the


process.
• Pointer – It is a stack pointer which is required tobe
saved when the process is switched from one state to
another to retain the current position of the process.
• Process number – Every process is assigned with a
unique id known as process ID or PID which stores
the process identifier.
• Program counter – It stores the counter which
contains the address of the next instruction that is to
be executed for the process.
• Register – These are the CPU registers which
includes: accumulator, base, registers and general
purpose registers.
• Memory limits – This field contains theinformation
about memory management system used by
operating system. This may include thepage tables,
segment tables etc.

• Open files list – This information includes the list of files opened for a process.
• Miscellaneous accounting and status data – This field includes information about the
amount of CPU used, time constraints, jobs or process number, etc.

The process control block stores the register content also known as execution content of
the processor when it was blocked from running. This execution content architecture
enables the operating system to restore a process’s execution context when the process
returns to the running state. When the process made transitions from one state to another,
the operating system update its information in the process’s PCB. The operating system
maintains pointers to each process’s PCB in a process table so that it can access the PCB
quickly.

OPERATING SYSTEM - PROCESS SCHEDULING

Definition:
The process scheduling is the activity of the process manager that handles the removal of
therunning process from the CPU and the selection of another process on the basis of a
particularstrategy.Process scheduling is an essential part of a Multiprogramming operating system.
Such operatingsystems allow more than one process to be loaded into the executable memory at a
time andloaded process shares the CPU using time multiplexing.

Scheduling Queues :
Scheduling queues refers to queues of processes or devices. When the process enters into
thesystem, then this process is put into a job queue. This queue consists of all processes in the
system.The operating system also maintains other queues such as device queue. Device queue is
a queuefor which multiple processes are waiting for a particular I/O device. Each device has its
own devicequeue.This figure shows the queuing diagram of process scheduling.Queue is
represented by rectangular box.The circles represent the resources that serve the queues.The
arrows indicate the process flow in the system.Queues are of two typesReady queueDevice queueA
newly arrived process is put in the ready queue. Processes waits in ready queue for allocatingthe
CPU. Once the CPU is assigned to a process, then that process will execute. While executing
theprocess, any one of the following events can occur.

The process could issue an I/O request and then it would be placed in an I/O queue.The process
could create new sub process and will wait for its termination.The process could be removed
forcibly from the CPU, as a result of interrupt and put back inthe ready queue.

Two-State Process Model


Two-state process model refers to running and non-running states which are described below −
Schedulers
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

Long Term Scheduler


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. The medium-term scheduler is in-charge of handling the
swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled out.
Swapping may be necessary to improve the process mix.

Comparison among 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.

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

Scheduling Criteria
Scheduling can be defined as a set of policies and mechanisms which controls the order in which
the work to be done is completed. The scheduling program which is a system software concerned
with scheduling is called the scheduler and the algorithm it uses is called the scheduling algorithm.
Various criteria or characteristics that help in designing a good scheduling algorithm are:
• CPU Utilization − A scheduling algorithm should be designed so that CPU remains busy
as possible. It should make efficient use of CPU.
• Throughput − Throughput is the amount of work completed in a unit of time. In other
words throughput is the processes executed to number of jobs completed in a unit of time.
The scheduling algorithm must look to maximize the number of jobs processed per time
unit.
• Response time − Response time is the time taken to start responding to the request. A
scheduler must aim to minimize response time for interactive users.
• Turnaround time − Turnaround time refers to the time between the moment of submission
of a job/ process and the time of its completion. Thus how long it takes to execute a process
is also an important factor.
Turnaround Time= Waiting Time + Burst Time(Execution Time)
• Waiting time − It is the time a job waits for resource allocation when several jobs are
competing in multiprogramming system. The aim is to minimize the waiting time.
Waiting time = Response Time- Arrival Time
• Fairness − A good scheduler should make sure that each process gets its fair share of the
CPU.
Scheduling Algorithms
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold. The main task of CPU scheduling is to make sure that whenever the
CPU remains idle, the OS at least select one of the processes available in the ready queue for
execution. The selection process will be carried out by the CPU scheduler. It selects one of the
processes in memory
that are

Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task holds for some time and resumes when the
higher priority task finishes its execution.
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy will release the CPU either by switching context or terminating. It is the
only method that can be used for various hardware platforms. That's because it doesn't need special
hardware (for example, a timer) like preemptive scheduling.

2
Types of CPU scheduling Algorithm There are mainly six types
of process scheduling algorithms
1. First Come First Serve (FCFS)
2. Shortest-Job-First (SJF) Scheduling
3. Shortest Remaining Time
4. Priority Scheduling
5. Round Robin Scheduling
6. Multilevel Queue Scheduling

◼ Threads

What is Thread?
A thread is a flow of execution through the process code, with its own program counter that keeps
track of which instruction to execute next, system registers which hold its current working
variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment and open
files. When one thread alters a code segment memory item, all other threads see that.
A thread is also called a lightweight process. Threads provide a way to improve application
performance through parallelism. Threads represent a software approach to improvingperformance
of operating system by reducing the overhead thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread
represents a separate flow of control. Threads have been successfully used in implementing
network servers and web server. They also provide a suitable foundation for parallel execution of
applications on shared memory multiprocessors. The following figureshows the working of a
single-threaded and a multithreaded process.
Difference between Process and Thread
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.

Types of Thread
Threads are implemented in following two ways −
• User Level Threads − User managed threads.
• Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.

1) User Level Threads


In this case, the thread management kernel is not aware of the existence of threads. The thread
library contains code for creating and destroying threads, for passing message and data between
threads, for scheduling thread execution and for saving and restoring thread contexts. The
application starts with a single thread.

Advantages
• Thread switching does not require Kernel mode privileges.
• User level thread can run on any operating system.
• Scheduling can be application specific in the user level thread.
• User level threads are fast to create and manage.

Disadvantages
• In a typical operating system, most system calls are blocking.
• Multithreaded application cannot take advantage of multiprocessing.

2) Kernel Level Threads


In this case, thread management is done by the Kernel. There is no thread management code in the
application area. Kernel threads are supported directly by the operating system. Any application
can be programmed to be multithreaded. All of the threads within an application are supported
within a single process.
The Kernel maintains context information for the process as a whole and for individuals threads
within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread
creation, scheduling and management in Kernel space. Kernel threads are generally slower to
create and manage than the user threads.

Advantages
• Kernel can simultaneously schedule multiple threads from the same process on multiple
processes.
• If one thread in a process is blocked, the Kernel can schedule another thread of the same
process.
• Kernel routines themselves can be multithreaded.

Disadvantages
• Kernel threads are generally slower to create and manage than the user threads.
• Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.

Multi threading Models


Some operating system provide a combined user level thread and Kernel level thread facility.
Solaris is a good example of this combined approach. In a combined system, multiple threads
within the same application can run in parallel on multiple processors and a blocking system call
need not block the entire process. Multi threading models are three types
• Many to many relationship.
• Many to one relationship.
• One to one relationship.

Many to Many Model


The many-to-many model multiplexes any number of user threads onto an equal or smaller number
of kernel threads.
The following diagram shows the many-to-many threading model where 6 user level threads are
multiplexing with 6 kernel level threads. In this model, developers can create as many user threads
as necessary and the corresponding Kernel threads can run in parallel on a multiprocessormachine.
This model provides the best accuracy on concurrency and when a thread performs a blocking
system call, the kernel can schedule another thread for execution.

Many to One Model


Many-to-one model maps many user level threads to one Kernel-level thread. Thread management
is done in user space by the thread library. When thread makes a blocking system call, the entire
process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a way that the
system does not support them, then the Kernel threads use the many-to-one relationship modes.
One to One Model
There is one-to-one relationship of user-level thread to the kernel-level thread. This model
provides more concurrency than the many-to-one model. It also allows another thread to run when
a thread makes a blocking system call. It supports multiple threads to execute in parallel on
microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding Kernel thread.
OS/2, windows NT and windows 2000 use one to one relationship model.
Thread Life Cycle

• New − A new thread begins its life cycle in the new state. It remains in this state until the
program starts the thread. It is also referred to as a born thread.
• Runnable − After a newly born thread is started, the thread becomes runnable. A thread in
this state is considered to be executing its task.
• Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for
another thread to perform a task. A thread transitions back to the runnable state only
when another thread signals the waiting thread to continue executing.
• Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that time
interval expires or when the event it is waiting for occurs.
• Terminated (Dead) − A runnable thread enters the terminated state when it completes its
task or otherwise terminates.
• Blocked State: The thread is waiting for an event to occur or waiting for an I/O device.
• Sleep: A sleeping thread becomes ready after the designated sleep time expires.

You might also like