0% found this document useful (0 votes)
11 views38 pages

Unit 2 OS

The document provides an overview of processes and threads in operating systems, detailing their definitions, states, and the differences between them. It discusses process control blocks, context switching, and various scheduling algorithms, including First-Come, First-Served (FCFS) and Shortest Job First (SJF). Additionally, it compares preemptive and non-preemptive scheduling, highlighting their characteristics and implications for CPU utilization and process management.

Uploaded by

Helping Hand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Unit 2 OS

The document provides an overview of processes and threads in operating systems, detailing their definitions, states, and the differences between them. It discusses process control blocks, context switching, and various scheduling algorithms, including First-Come, First-Served (FCFS) and Shortest Job First (SJF). Additionally, it compares preemptive and non-preemptive scheduling, highlighting their characteristics and implications for CPU utilization and process management.

Uploaded by

Helping Hand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Operating System

Unit-2
Process

• A process is a program in execution.

• A process is an ‘active’ entity instead of a program, which is considered a


‘passive’ entity.

• Example : C/C++ program source code- Program

after compilation binary code- Process


States of Process

• As a process executes, it changes state

• new: The process is being created

• running: Instructions are being executed

• waiting: The process is waiting for some event to occur

• ready: The process is waiting to be assigned to a processor

• terminated: The process has finished execution


States of Process
Process Control Block
Information associated with each process

• Process state DS used by OS to store


all the info. of
• Program counter Process

• CPU registers

• CPU scheduling information

• Memory-management information

• Accounting information

• I/O status information


PCB
Context Switching

• The process of saving the context of one process and loading the context of
another process is known as Context Switching.

• In simple terms, it is like loading and unloading the process from the running
state to the ready state.

• When CPU switches to another process, the system must save the state of the old
process and load the saved state for the new process
Context Switching
CPU Switch From Process to Process
Thread

• It is a lightweight process that the operating system can schedule and run
concurrently with other threads.

• The operating system creates and manages threads, and they share the same
memory and resources as the program that created them.

• This enables multiple threads to collaborate and work efficiently within a single
program.
• Thread is the segment of a process
which means a process can have
multiple threads and these multiple
threads are contained within a
process
Types of thread

• User level thread

• Kernel level thread


User Level Threads

• User Level Thread is a type of thread that is not created using system calls.

• The kernel has no work in the management of user-level threads.

• User-level threads can be easily implemented by the user.

• In case when user-level threads are single-handed processes, kernel-level thread


manages them.
Kernel Level Threads

• A kernel Level Thread is a type of thread that can recognize the Operating system
easily.

• Kernel Level Threads has its own thread table where it keeps track of the system.
The operating System Kernel helps in managing threads.

• Kernel Threads have somehow longer context switching time.

• Kernel helps in the management of threads.


Multithreading

• A thread is also known as a lightweight process.

• The idea is to achieve parallelism by dividing a process into multiple threads.

• For example, in a browser, multiple tabs can be different threads.

• Multithreading is a technique used in operating systems to improve the


performance and responsiveness of computer systems.

• Multithreading allows multiple threads (i.e., lightweight processes) to share the


same resources of a single process, such as the CPU, memory, and I/O devices.
Process vs Thread
S.NO Process Thread

1. Process means any program is in execution. Thread means a segment of a process.

2. The process takes more time to terminate. The thread takes less time to terminate.

3. It takes more time for creation. It takes less time for creation.

4. It also takes more time for context switching. It takes less time for context switching.

5. The process is less efficient in terms of communication. Thread is more efficient in terms of communication.

We don’t need multi programs in action for multiple


6. Multiprogramming holds the concepts of multi-process. threads because a single process consists of multiple
threads.

7. The process is isolated. Threads share memory.


Process vs. Thread
S.NO Process Thread

A Thread is lightweight as each thread in a process shares


8. The process is called the heavyweight process.
code, data, and resources.

Thread switching does not require calling an operating system


9. Process switching uses an interface in an operating system.
and causes an interrupt to the kernel.

If a user-level thread is blocked, then all other user-level


10. If one process is blocked then it will not affect the execution of other processes
threads are blocked.

Thread has Parents’ PCB, its own Thread Control Block, and
11. The process has its own Process Control Block, Stack, and Address Space.
Stack and common Address space.

Since all threads of the same process share address space and
12. Changes to the parent process do not affect child processes. other resources so any changes to the main thread may affect
the behavior of the other threads of the process.

13. A system call is involved in it. No system call is involved, it is created using APIs.

14. The process does not share data with each other. Threads share data with each other.
Process Scheduling Queue

• Job queue – set of all processes in the system

• Ready queue – set of all processes residing in main memory, ready and waiting to
execute

• Device queues – set of processes waiting for an I/O device

• Processes migrate among the various queues


Process Scheduling
Process Scheduler

• Long-term scheduler (or job scheduler) – selects which processes should be


brought into the ready queue

• Short-term scheduler (or CPU scheduler) – selects which process should be


executed next and allocates CPU

• Medium-Term Scheduler - responsible for suspending and resuming the process


Medium Term Scheduler
Scheduling Criteria

• CPU scheduling is a method process or task that the CPU will run at any given
moment.

• CPU Scheduling has several criteria


1. CPU utilization - objective of any CPU scheduling algorithm is to keep the CPU as busy as possible (40-90%)

2. Throughput- number of processes being executed and completed per unit of time

3. Turnaround time - how long it takes to execute that process

4. Waiting time - Time Difference between turn around time and burst time

5. Response time- time taken from submission of the process of the request until the first response is produced.
• Turn Around Time = Completion Time - Arrival Time.

• Waiting Time = Turnaround Time - Burst Time.

• Response Time = CPU Allocation Time(when the CPU was allocated for the first)
- Arrival Time
Scheduling Algorithm

• FCFS

• SJF

• RR
FCFS Examples
Process id Arrival Time Burst Time(Execution Time)

P1 0 2

P2 1 2

P3 5 3

P4 6 4

Find the average Turnaround time and Waiting time.


FCFS
Process id Arrival Time Burst Time(Execution Time)

P1 3 4

P2 5 3

P3 0 2

P4 5 1

P5 4 3

Find the average Turnaround time and Waiting time.


FCFS
Process id Arrival Time Burst Time(Execution Time)

P1 0 2

P2 3 1

P3 5 6

Find the average Turnaround time and Waiting time.


Convoy Effect

• FCFS algorithm is non-preemptive in nature, that is, once CPU time has been
allocated to a process, other processes can get CPU time only after the current
process has finished. This property of FCFS scheduling leads to the situation
called Convoy Effect.
SJF
• RACTICE PROBLEMS BASED ON SJF SCHEDULING-

Problem-01

• Consider the set of 5 processes whose arrival time and burst time are given below-
Process ID Arrival Time Burst Time
P1 3 1
P2 1 4

P3 4 2

P4 0 6
P5 2 3

If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and average turn
around time.
SJF Solution

Process Id Arrival time Burst time

P1 3 1

P2 1 4

P3 4 2

P4 0 6

P5 2 3
• Now,

• Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit

• Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit


• If the CPU scheduling policy is SJF preemptive, calculate the average waiting
time and average turn around time.
SJF solution with preemption
Process Id Exit time Turn Around time Waiting time

P1 7 7–3=4 4–1=3

P2 16 16 – 1 = 15 15 – 4 = 11

P3 9 9–4=5 5–2=3

P4 6 6–0=6 6–6=0

P5 12 12 – 2 = 10 10 – 3 = 7
Preemptive vs. Non-preemptive
Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING

Once resources(CPU Cycle) are allocated


In this resources(CPU Cycle) are allocated to a process for to a process, the process holds it till it
Basic
a limited time. completes its burst time or switches to
waiting state.

Process can not be interrupted until it


Interrupt Process can be interrupted in between.
terminates itself or its time is up.

If a process with a long burst time is


If a process having high priority frequently arrives in the
Starvation running CPU, then later coming process
ready queue, a low priority process may starve.
with less CPU burst time may starve.

Overhead It has overheads of scheduling the processes. It does not have overheads.
Preemptive vs. Non-preemptive
Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING

Flexibility flexible rigid

Cost cost associated no cost associated

CPU Utilization In preemptive scheduling, CPU utilization is high. It is low in non preemptive scheduling.

Non-preemptive scheduling waiting time


Waiting Time Preemptive scheduling waiting time is less.
is high.
Preemptive vs. Non-preemptive
Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING

Decisions are made by the scheduler and are based on priority and time Decisions are made by the process itself and the OS
Decision making slice allocation just follows the process’s instructions

The OS has less control over the scheduling of


Process control The OS has greater control over the scheduling of processes processes

Lower overhead since context switching is less


Overhead Higher overhead due to frequent context switching frequent

Examples Examples of preemptive scheduling are Round Robin and Shortest Examples of non-preemptive scheduling are First
Remaining Time First. Come First Serve and Shortest Job First.

You might also like