Skip to main content

Unit 2 Operating System

 Unit- 2  Process and Process Scheduling

2.1 Introduce Process, Program and Process Life Cycle

 Process

Process is something that is currently under execution. So, an active program can be called a Process. Examples:

      Opening a web browser to search something on the internet — the browser becomes a process.

      Launching a music player to enjoy your favorite tunes — the music player is also a process.

In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity.

Modern operating systems support multithreading, meaning a process can have multiple threads running concurrently.

 

A Process has various attributes associated with it. Some of the attributes of a Process are:

        Process Id: Every process will be given a unique id that identifies the process from the other processes.

        Process state: Each and every process has some states associated with it at a particular instant of time. This is denoted by process state. It can be ready, waiting, running, etc.

        CPU scheduling information: Each process is executed by using some process scheduling algorithms like FCSF, Round-Robin, SJF, etc.

        I/O information: Each process needs some I/O devices for their execution. So, the information about device allocated and device need is crucial.

 

Program

A program is a piece of code which may be a single line or multiple  lines. A computer program is usually written by a computer programmer in a programming language. For example, here is a simple program written in C programming language −

#include <stdio.h>

 

int main() {

   printf("Hello, World! \n");

   return 0;

}

A computer program is a collection of instructions that performs a specific task when executed 

by a computer. When we compare a program with a process, we can conclude that a process

 is a dynamic instance of a computer program.


Process Life Cycle

The Process Life Cycle refers to the sequence of states that a process goes through from its creation to termination during its lifetime.

A process state represents the current status of a process — or what the process is doing at 

a particular moment during its execution.

 

A process can have one of the following five states at a time.

S.N.

State & Description

1

New

This is the initial state when a process is first started/created.(You click "Print" → New)

2

Ready

The process is waiting to be assigned to a processor. Ready processes are waiting to have 

the processor allocated to them by the operating system so that they can run. Process may 

come into this state after Start(new) state or while running it by but interrupted by the

 scheduler to assign CPU to some other process.

 (It enters the print queue → Ready)

3

Running

Once the process has been assigned to a processor by the OS scheduler, the process state is set to running and the processor executes its instructions.

(Printer starts printing → Running)

4

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.

(Pauses because paper is out → Waiting)

5

Terminated or Exit

Once the process finishes its execution, or it is terminated by the operating system, it is moved

 to the terminated state where it waits to be removed from main memory.

(Printing completes → Terminated)



2.2 Process Control Block (PCB)

A Process Control Block is a data structure maintained by the Operating System for every process. The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to keep track of a process as listed below in the table −

S.N.

Information & Description

1

Process State

The current state of the process i.e., whether it is ready, running, waiting, or whatever.

2

Process privileges

This is required to allow/disallow access to system resources.

3

Process ID

Unique identification for each of the process in the operating system.

4

Pointer

A pointer to parent process.

5

Program Counter

Program Counter is a pointer to the address of the next instruction to be executed for this 

process.

6

CPU registers

Various CPU registers where process need to be stored for execution for running state.

7

CPU Scheduling Information

Process priority and other scheduling information which is required to schedule the process.

8

Memory management information

This includes the information of page table, memory limits, Segment table depending on 

memory used by the operating system.

9

Accounting information

This includes the amount of CPU used for process execution, time limits, execution ID 

etc.

10

IO status information

This includes a list of I/O devices allocated to the process.



The PCB is maintained for a process throughout its lifetime, and is deleted once the process terminates.


Introduction to Process Scheduling

Process scheduling is an operating system function that decides which process will use the CPU next. It manages processes in states such as ready, running, and waiting. The scheduler removes the currently running process from the CPU and selects another process according to a scheduling algorithm. It is an important part of multiprogramming operating systems, where multiple processes share the CPU using time-sharing. Process scheduling helps keep the CPU busy, improves CPU utilization, and provides better response time and system performance.


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.


Types of Process Scheduling

Process Scheduling handles the selection of a process for the processor on the basis of a scheduling algorithm and also the removal of a process from the processor. It is an important part of multiprogramming operating system.

The different schedulers that are used for process scheduling are −

Short-Term Scheduler (CPU Scheduler)

  • Also called CPU Scheduler

  • Selects processes from the ready queue

  • Runs very frequently

  • Works very fast

  • Allocates the CPU to the next ready process

  • Keeps the CPU busy

  • Improves CPU utilization

  • Improves response time and performance

Medium-Term Scheduler

  • Manages main memory usage

  • Performs swapping of processes

  • Moves processes from RAM to disk (swap out)

  • Brings processes back from disk to RAM (swap in)

  • Runs occasionally

  • Helps free memory space

  • Balances system load

  • Improves overall system performance

Long-Term Scheduler (Job Scheduler)

  • Also called Job Scheduler

  • Selects jobs from the job pool

  • Admits new processes into memory

  • Controls the number of processes in the system

  • Runs infrequently

  • Maintains the degree of multiprogramming

  • Keeps a balance between CPU-bound and I/O-bound processes

  • Ensures efficient use of system resources


Preemptive and non Preemptive Scheduling

Preemptive Scheduling

  • The operating system can interrupt a running process and assign the CPU to another process.

  • A process may be interrupted when a higher-priority process arrives or when its time slice expires.

  • It provides better response time and is suitable for multitasking and interactive systems.

  • It requires more context switching, which may increase overhead.

  • Examples: Round Robin, Shortest Remaining Time First (SRTF), Preemptive Priority Scheduling.

Non-Preemptive Scheduling

  • Once a process gets the CPU, it continues running until it finishes or enters the waiting state.

  • The operating system cannot forcibly remove the process from the CPU.

  • It is simpler and easier to implement than preemptive scheduling.

  • It involves less context switching and lower overhead.

  • It may cause longer waiting and response time for other processes.

  • Examples: First Come First Served (FCFS), Non-Preemptive SJF, Non-Preemptive Priority Scheduling.

Preemptive Scheduling

Non-Preemptive Scheduling

Resources are allocated according to the cycles for a limited time.

Resources are used and then held by the process until it gets terminated.

The process can be interrupted, even before the completion.

The process is not interrupted until its life cycle is complete.

Starvation may be caused, due to the insertion of priority process in the queue.

Starvation can occur when a process with large burst time occupies the system.

Maintaining queue and remaining time needs storage overhead.

No such overheads are required.


Thread

  • A thread is the smallest unit of execution inside a process.

  • It is also known as a lightweight process (LWP).

  • Each thread has its own program counter, registers, and stack.

  • Threads of the same process share code, data, and open files.

  • A thread cannot exist without a process.

  • Multiple threads allow a program to perform tasks concurrently.

  • Threads reduce the overhead of creating separate processes.

  • They help improve CPU utilization and application performance.

  • Each thread represents a separate flow of control within a process.

  • Threads are widely used in web servers, network servers, and parallel processing applications.


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.

Example of Thread: Word Processor

  •     Typing text in the document is one thread.
  • Automatic text formatting runs as another thread.

  • Spell checking works as another thread.

  • Auto-saving the file to disk runs as another thread.

All these threads work simultaneously inside the same word processor process, making the application faster and more responsive.

Life Cycle of Thread:

1. Born State: A thread that has just created.
2. Ready State: The thread is waiting for the processor (CPU).
3. Running: The System assigns the processor to the thread means that the thread is being executed.
4. Blocked State: The thread is waiting for an event to occur or waiting for an I/O device.
5. Sleep: A sleeping thread becomes ready after the designated sleep time expires.
6. Dead: The execution of the thread is finished.


Difference between Process and Thread

S.N.

Process

Thread

1

Process is heavy weight or resource intensive.

Thread is light weight, taking lesser resources than a process.

2

Process switching needs interaction with operating system.

Thread switching does not need to interact with operating system.

3

In multiple processing environments, each process executes the same code but has its own memory and file resources.

All threads can share same set of open files, child processes.

4

If one process is blocked, then no other process can execute until the first process is unblocked.

While one thread is blocked and waiting, a second thread in the same task can run.

5

Multiple processes without using threads use more resources.

Multiple threaded processes use fewer resources.

6

In multiple processes each process operates independently of the others.

One thread can read, write or change another thread's data.



Life Cycle of a Threat

A threat in an OS is any potential danger (like malware or hackers) that can harm a system by exploiting vulnerabilities. The threat life cycle involves several key stages from detection to recovery:

Life Cycle of a Threat steps

1. Detection

A threat is first noticed by the system using tools like antivirus, firewall, or monitoring software.

2. Exploitation

The attacker tries to use a weakness in the system to get access or cause harm.

3. Propagation (Spread)

If the threat is able to, it spreads to other systems through networks or by tricking users.

4. Execution (Attack Happens)

The threat starts doing damage such as stealing data, locking files, or crashing the system.

5. Detection of Impact

The system or users realize something is wrong, like errors, slow speed, or strange activity.

6. Containment

Steps are taken quickly to stop the threat from spreading, such as disconnecting from the internet or blocking access.

7. Eradication

The threat is fully removed by deleting harmful files and fixing the security holes.

8. Recovery

The system is brought back to normal, files are restored, and it’s checked to make sure everything is safe.

9. Post-Incident Analysis

The incident is studied to find out how it happened and to improve the system’s security for the future.



         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

       1. First Come First Serve (FCFS)

·         This is the oldest and simplest scheduling technique.

·         The process that arrives first gets the CPU first.

·         It’s like standing in a queue — the first person gets served first.

·         Non-preemptive (once a process starts, it runs till it finishes).

Key Characteristics:

·         Easy to implement.

·         Fair in order, but not always efficient.

·         Can lead to long waiting times, especially if a long process comes first.

·         Convoy Effect: One long job can delay many short ones.

Formulae:

Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time

 2. Shortest Job First (SJF)

·         Picks the process with the shortest burst time (execution time).

·         Like finishing smaller tasks first to reduce total waiting.

·         It’s efficient but needs to know how long each task will take.

Non-preemptive (process runs until it finishes).

 Key Characteristics:

·         Minimizes average waiting time.

·         Can lead to starvation (long jobs wait too long).

·         Needs accurate burst time in advance.

3. Shortest Remaining Time First (SRTF)

·         This is the preemptive version of SJF.

·         Always runs the process with the shortest remaining burst time.

·         A new process with a shorter time can interrupt the current one.

·         Preemptive (switches between processes if needed).

 Key Characteristics:

·         Gives better turnaround and response time.

·         Can also cause starvation of long processes.

·         Needs to predict or know the burst time ahead of time



Comments

Popular posts from this blog

Unit-1 Introduction to C#.NET (Class 12)

  What is .NET Framework? The .NET Framework is a software development platform developed by Microsoft. It provides tools and libraries to build and run Windows applications, web services, and web apps. It gives tools and libraries that make it easier to write programs. It also helps the computer run those programs safely and efficiently. Microsoft started working on .NET Framework in the late 1990s . The first version, .NET Framework 1.0 , was released in 2002 .   The .NET Framework is made up of several important components: 1.       Common Language Runtime (CLR) is the core engine that runs your program. It converts your code into machine code that the computer can understand, manages memory, handles errors, and ensures that your program runs safely. 2.       The Class Library is a large collection of ready-made code that helps you perform common tasks like working with files, databases, graphics, the i...

Introduction to Software Engineering (Class 12)

 Introduction to Software Engineering Software engineering is the branch of computer science that deals with the design, development, testing, and maintenance of software applications. Software engineers apply engineering principles and knowledge of programming languages to build software solutions for end users. IEEE, in its standard 610.12-1990, defines software engineering as the application of a systematic, disciplined, which is a computable approach for the development, operation, and maintenance of software. Boehm defines software engineering, which involves, ‘the practical application of scientific knowledge to the creative design and building of computer programs. It also includes associated documentation needed for developing, operating, and maintaining them.’ Importance of Software Engineering Reduces complexity Large software systems are divided into smaller, manageable modules. Each module is developed and solved independently, ...