Unit 2 OS
Unit 2 OS
Unit-2
Process
• CPU registers
• Memory-management information
• Accounting information
• 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 is a type of thread that is not created using system calls.
• 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.
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.
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
• Ready queue – set of all processes residing in main memory, ready and waiting to
execute
• CPU scheduling is a method process or task that the CPU will run at any given
moment.
2. Throughput- number of processes being executed and completed per unit of time
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.
• 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
P1 3 4
P2 5 3
P3 0 2
P4 5 1
P5 4 3
P1 0 2
P2 3 1
P3 5 6
• 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
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
• Now,
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
Overhead It has overheads of scheduling the processes. It does not have overheads.
Preemptive vs. Non-preemptive
Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING
CPU Utilization In preemptive scheduling, CPU utilization is high. It is low in 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
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.