0% found this document useful (0 votes)
15 views

OS UNIT-II (1)

The document provides an overview of Linux shell and its types, including command line and graphical shells, along with examples of shell scripting. It explains the concept of processes, process scheduling, and the differences between processes and programs, as well as the significance of cooperating processes and threads. Additionally, it covers process control blocks, operations on processes, and various shell script examples for practical understanding.

Uploaded by

ramuydlaramu
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)
15 views

OS UNIT-II (1)

The document provides an overview of Linux shell and its types, including command line and graphical shells, along with examples of shell scripting. It explains the concept of processes, process scheduling, and the differences between processes and programs, as well as the significance of cooperating processes and threads. Additionally, it covers process control blocks, operations on processes, and various shell script examples for practical understanding.

Uploaded by

ramuydlaramu
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/ 83

UNIT-II

Linux: Introduction to shell, Types of shell's , example shell programs.

Process and CPU Scheduling - Process concepts and scheduling,


Operations on processes, Cooperating Processes, Threads, Scheduling Criteria,
Scheduling Algorithms, Multiple -Processor Scheduling.
Introduction to Linux Shell

Shell is a program that helps to execute program or command in


linux os.

• It is an interface between user and kernel. In simple we can say that is an


program which is used to execute other programs on our system.

• If we are using any major operating system, we are indirectly interacting


with the shell. While running Ubuntu, Linux Mint, or any other Linux
distribution, we are interacting with the shell by using the terminal.
What is Kernel?
• The kernel is a computer program that is the core of a computer’s operating
system, with complete control over everything in the system. It manages the
following resources of the Linux system –
• File management
• Process management
• I/O management
• Memory management
• Device management etc.

Linus Torvalds has developed Linux OS, but actually, he is only responsible for the
development of the Linux kernel.

Complete Linux system = Kernel + GNU system utilities and libraries + other
management scripts + installation scripts.
What is Shell?
• A shell is a special user program that provides an interface for the user to use operating system services.
Shell accepts human-readable commands from users and converts them into something which the kernel
can understand. It is a command language interpreter that executes commands read from input devices
such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
Shell is broadly classified into two categories
• Command Line Shell
• Graphical shell
Command Line Shell
Shell can be accessed by users using a command line interface. A special program
called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided
to type in the human-readable commands such as “cat”, “ls” etc. and then it is being
executed.
Graphical Shells
Graphical shells provide means for manipulating programs based on the graphical
user interface (GUI), by allowing for operations such as opening, closing, moving,
and resizing windows, as well as switching focus between windows. Window OS or
Ubuntu OS can be considered as a good example which provides GUI to the user for
interacting with the program. Users do not need to type in commands for every
action.
Types of shells
•Bourne shell (sh)
•C shell (csh)
•Korn shell (ksh)
•Bourne Again shell (bash)

Bourne shell (sh):

• Developed at AT&T Bell Labs by Steve Bourne, the Bourne shell is regarded as the first UNIX
shell ever.
• It is denoted as sh.
• It gained popularity due to its compact nature and high speeds of operation.
• The complete path-name for the Bourne shell is /bin/sh and /sbin/sh. By default, it uses
the prompt # for the root user and $ for the non-root users.
Limitations

• Does not provide history list


• Does not provide command line editing
• Not friendly for interactive use

However, the Bourne shell has some major drawbacks.

•It doesn’t have in-built functionality to handle logical and arithmetic


operations.
•Also, unlike most different types of shells in Linux, the Bourne shell
cannot recall previously used commands.
•It also lacks comprehensive features to offer a proper interactive use.
C shell (csh)

• The C shell was created at the University of California by Bill Joy.


• It is denoted as csh.
• The c shells syntax and its usage is are very similar to c programming language.
• Further, it incorporated command history which was missing in different types of shells in Linux like the
Bourne shell.
• Another prominent feature of a C shell is “aliases”.
• The complete path-name for the C shell is /bin/csh.

Korn shell (ksh)


• The Korn shell was developed at AT&T Bell Labs by David Korn, to improve the Bourne shell.
• It is denoted as ksh.
• The Korn shell is essentially a superset of the Bourne shell.
It is faster than most different types of shells in Linux, including the C shell.
• Support command aliases and many features like bash.
Features
• Supports command line editing
• Provides built in commands
• It is not default shell on most linux system.
• The complete path-name is /bin/ksh.
•Bourne Again shell (bash)

• Developed by Brain fox and Chet Ramey at software foundation


• Based on bourne shell
• More popularly known as the Bash shell, the GNU Bourne-Again shell was designed to be compatible with
the Bourne shell.
• It incorporates useful features from different types of shells in Linux such as Korn shell and C shell.
• It allows us to automatically recall previously used commands and edit them with help of arrow keys,
unlike the Bourne shell.
• The complete path-name for the GNU Bourne-Again shell is /bin/bash.
• By default, it uses the prompt bash-VersionNumber# for the root user and bash-VersionNumber$ for the
non-root users.

These different shells do the same job but understands different commands and provides different built in
functions.
• What is a terminal?
A program which is responsible for providing an interface to a user so that
he/she can access the shell. It basically allows users to enter commands and
see the output of those commands in a text-based interface.
Shell Scripting
• shells are interactive, which means they accept commands as input from users
and execute them.
• If we want to execute a bunch of commands repeatedly, we have to type all
commands each and every time in the terminal.
• To overcome this we can write commands in a file and execute them in shell to
avoid repetitive work. These files are called Shell Scripts or Shell Programs.)
• Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved
with `.sh` file extension e.g., myscript.sh.
• A shell script has syntax just like any other programming language. If you have
any prior experience with any programming language like Python, C/C++ etc.
Why shell scripts?

There are many reasons to write shell scripts:


• To avoid repetitive work and automation
• System admins use shell scripting for routine backups.
• System monitoring
• Adding new functionality to the shell etc.
Shell Script Example
• Assume we make a test.sh script. We have to alert a system that the shell
script is started before we include anything else in our script.
• It can be done with the help of the shebang construct.
#!/bin/bash
It is known as the shebang due to the symbol # is known as hash and the
symbol ! It is known as the bang.

Note: Every script will have a .sh extension.


Simple example:
1. Create a file name as vi filename.sh
2. #! /bin/bash
echo “hello world”
3. Execute the file as ./filename.sh
4. To check permissions of the file chmod 777.
o/p: hello world
Examples of shell programs

Addition of 2 numbers
#! /bin/bash
echo "enter value of a:"
read a
echo "enter value of b:"
read b
c=`expr $a + $b`
echo “value of c is: $c”

O/p:
enter value of a:5
enter value of b:5

Value of c is 10
Shell program to find greatest of two numbers

echo "Enter the first number:"


read num1
echo "Enter the second number:"
read num2
# Check which number is greater
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2"
elif [ $num2 -gt $num1 ]; then
echo "$num2 is greater than $num1”
else
echo "Both numbers are equal"
fi
A shell script to find the factorial of a number

#! /bin/bash
echo "Enter a number:"
read number
factorial=1
# Check if the number is non-negative
if [ $number -lt 0 ]; then
echo "Factorial is not defined for negative
numbers."
elif [ $number -eq 0 ]; then
echo "Factorial of 0 is 1"
else
# Call the factorial function
for(( i=1; i<=number; i++ )); do
factorial=$(( factorial *i ))
done
echo "Factorial of $number is
$factorial"
fi
shell script to check whether the number is palindrome or not:
#! /bin/bash
echo “enter n”
read n
num=0
on=$n
while [ $n -gt 0 ]; do
num=$(expr $num \* 10)
k=$(expr $n % 10)
num=$(expr $num + $k)
n=$(expr $n / 10)
done
if [ $num -eq $on ]; then
echo “palindrome”
else
echo “not palindrome”
fi
Print a greeting based on the time of day

#! /bin/bash
currenthour=$(date +%H)
if [ $currenthour -gt 5 -a $currenthour -lt 12 ]; then
greeting="Good morning"
elif [ $currenthour -gt 12 -a $currenthour -lt 18 ]; then
greeting="Good afternoon"
else
greeting="Good night"
fi
echo "$greeting have a nice day!"
Process
A process is a program at the time of execution.

Process Program
 A Program is basically a collection of
 The process is basically an instance
instructions that mainly performs a
of the computer program that is
specific task when executed by the
being executed.
computer.
 A process has a shorter lifetime.  A Program has a longer lifetime.
 A Process requires resources such as  A Program is stored by hard-disk and
memory, CPU, Input-Output devices. does not require any resources.
 A process has a dynamic instance of  A Program has static code and static
code and data data.
 Basically, a process is the running  On the other hand, the program is
instance of the code. the executable code.
Process State
Process State
As a process executes, it changes state.

Each process may be in one of the following states:


 New. The process is being created.
 Running. Instructions are being executed.
 Waiting. The process is waiting for some event to occur (such as an
I/O
completion or reception of a signal).
 Ready. The process is waiting to be assigned to a processor.
Terminated. The process has finished execution..
Diagram of Process State

Operating System Concepts


Process Control Block (PCB)

Operating System Concepts


Process Control Block
 Each process is represented in the operating system by a process control block
(PCB)—also called a task control block.
 Process state. The state may be new, ready, running, and 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.
 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 value of 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 members, job or process
numbers, and so on.
 I/O status information-This information includes the list of I/O
devices allocated to the process, a list of open files, and so on.
Operations on processes
Process Creation
 Parent process create children processes, which, in turn create other processes,
forming a tree of processes.

 Resource sharing
Parent and children share all resources.
Children share subset of parent’s resources.

 Execution
Parent and children execute concurrently.
Parent waits until children terminate.
Operating System Concepts
Process Creation (Cont.)
Address space
Child duplicate of parent.
Child has a program loaded into it.

UNIX examples
fork system call creates new process
exec system call used after a fork to replace the process’ memory space with
a new program.

Operating System Concepts


#include<stdio.h
> else if (pid
{ //Child == 0)
void
argc, main(int
char process
*argv[])
{ int pid; execlp("/bin/ls"
,"ls",NULL);
}else
/* Fork
another process { //Parent
*/pid = fork(); process
//Parent
if(pid < 0) will
child wait
to for the
{ //Error complete
occurred wait(NULL);
fprintf(stderr,
"Forkexit(-1);
Failed"); printf("Child
} complete");
exit(0);
}
Process Termination
Process executes last statement and asks the operating system to
decide it (exit).
Output data from child to parent (via wait).
Process’ resources are de allocated by operating system.
Parent may terminate execution of children processes (abort).
Child has exceeded allocated resources.
Task assigned to child is no longer required.
Parent is exiting.
Operating system does not allow child to continue if its parent terminates.
Cascading termination.
Cooperating Processes

• Independent Process: Independent Processes are those


processes whose task is not dependent on any other processes.
• Cooperating Process: Cooperating Processes are those
processes that depend on other processes or processes. They
work together to achieve a common task in an operating
system. These processes interact with each other by sharing
the resources such as CPU, memory, and I/O devices to
complete the task.
Advantages of Cooperating Process in OS
1.Information Sharing
Cooperating processes can be used to share information between various processes. It could involve
having access to the same files. A technique is necessary so that the processes may access the files
concurrently.
2.Modularity
Modularity refers to the division of complex tasks into smaller subtasks. Different cooperating processes can
complete these smaller subtasks. As a result, the required tasks are completed more quickly and efficiently.
3. Computation Speedup
Cooperating processes can be used to accomplish subtasks of a single task simultaneously. It improves
computation speed by allowing the task to be accomplished faster. Although, it is only possible if the system
contains several processing elements.
4. Convenience
There are multiple tasks that a user requires to perform, such as printing, compiling, editing, etc. It is more
convenient if these activities may be managed through cooperating processes.
Concurrent execution of cooperating processes needs systems that enable processes to communicate and
synchronize their actions.
Methods of Cooperating Process
1.Cooperation by sharing
The processes may cooperate by sharing data, including variables, memory,
databases, etc. The critical section provides data integrity, and writing is mutually
exclusive to avoid inconsistent data.

Cooperating Process in Operating System


Here, you see a diagram that shows cooperation by sharing. In this diagram, Process
P1 and P2 may cooperate by using shared data like files, databases, variables,
memory, etc.
2. Cooperation by Communication
The cooperating processes may cooperate by using messages. If every process waits
for a message from another process to execute a task, it may cause a deadlock. If a
process does not receive any messages, it may cause starvation. Process P1 and
P2 may cooperate by using messages to communicate.
Threads:
A process is divide into number of light weight process,
each light weight process is said to be a Thread.
The Thread has a program counter (Keeps track of which instruction
to execute next), registers (holds its current working variables),
stack (execution History).
Thread States:
•born State : A thread is just created.
•ready state : The thread is waiting for CPU.
•running : System assigns the processor to the thread.
•sleep : A sleeping thread becomes ready after the designated
sleep time expires.
•dead : The Execution of thethread finished.
Process Thread
Process takes more time to create. Thread takes less time to create.
it takes more time to complete execution & Less time to terminate.
terminate.
Execution is very slow. Execution is very fast.
It takes more time to switch It takes less time to switch b/w two threads.
b/w two processes.
Communication b/w two processes is difficult . Communication b/w two threads is easy.

Process can’t share the same memory area. Threads can share same memory area.
System calls are requested to communicate each System calls are not required.
other.
Process is looselycoupled. Threads are tightly coupled.
It requires more resources to execute. Requires few resources to execute.
Multithreading

A process is divided into number of smaller tasks each task is called a


Thread. Number of Threads with in a Process execute at a time is called
Multithreading.

If a program, is multithreaded, even when some portion of it is blocked, the


whole program is not blocked. The rest of the program continues working If
multiple CPU’s are available.

Multithreading gives best performance. If we have only a single thread,


number of CPU’s available, No performance benefits achieved.

Process creation is heavy-weight while thread creation is light-weight Can


Kernels are generally multithreaded
• CODE- Contains instruction
• DATA- holds global variable FILES- opening and closing files
• REGISTER- contain information about CPU state STACK-
parameters, local variables, functions

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.
Types Of Threads:

User Threads :
 Thread creation, scheduling, management happen in user space by
Thread Library.
 user threads are faster to create and manage.
 If a user thread performs a system call, which blocks it, all the other
threads in that process one also automatically blocked, whole process is
blocked.

Disadvantages
 In a typical operating system, most system calls areblocking.
 Multithreaded application cannot take advantage ofmultiprocessing.
Kernel Threads:
 kernel creates, schedules, manages these threads .
 these threads are slower, manage.
 If one thread in a process blocked, over all process need not be blocked.
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 multithreaded.
Disadvantages
 Kernel threads are generally slower to create and manage than the user threads.
 Transfer of control from one thread to another within same process requires a
mode switch to the Kernel.
PROCESS SCHEDULING:

 CPU is always busy in Multiprogramming. Because CPU switches from one


job to another job.

 But in simple computers CPU sit idle until the I/O request granted.
 scheduling is a important OS function. All resources are scheduled before
use.(cpu, memory, devices…..)

 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
Scheduling Objectives
 Maximize throughput.

 Maximize number of users receiving acceptable response times. Be


predictable.

 Balance resource use.

 Avoid indefinite postponement.

 Enforce Priorities.
What are Scheduling Queues?

 All processes, upon entering into the system, are stored in the Job Queue.

 Processes in the Ready state are placed in the Ready Queue.

 Processes waiting for a device to become available are placed in Device Queues.

 There are unique device queues available for each I/O device.

 A new process is initially put in the Ready queue.

 It waits in the ready queue until it is selected for execution(or dispatched).


Representation of Process Scheduling

Operating System Concepts


Scheduling fell into one of the two general categories:
Non Pre-emptive Scheduling: When the currently executing process
gives up the CPU voluntarily.
Pre-emptive Scheduling: When the operating system decides to favour
another process, pre-empting the currently executing process.
Schedulers

Types of Schedulers
There are three types of schedulers available:
Long Term Scheduler
Short Term Scheduler
Medium Term Scheduler

Operating System Concepts


Types of schedulers
1. Long term scheduler:
 select the jobs from the job pool and loaded these jobs into main memory (ready
queue). Long term scheduler is also called job scheduler.
2. Short term scheduler:
 select the process from ready queue, and allocates it to the cpu.
 If a process requires an I/O device, which is not present available then process
enters device queue.
 short term scheduler maintains ready queue, device queue. Also called as cpu
scheduler.
3. Medium term scheduler: if process request an I/O device in the middle of the
execution, then the process removed from the main memory and loaded into the
waiting queue.
 When the I/O operation completed, then the job moved from waiting queue to
ready queue. These two operations performed by medium term scheduler.
Addition of Medium Term Scheduling

Operating System Concepts


Schedulers (Cont.)
 Short-term scheduler is invoked very frequently (milliseconds)  (must be
fast).
 Long-term scheduler is invoked very infrequently (seconds, minutes) 
(may be slow).
 The long-term scheduler controls the degree of multiprogramming.

• Processes can be described as either:


 I/O-bound process – spends more time doing I/O than computations, many short
CPU bursts.
 CPU-bound process – spends more time doing computations; few very long CPU
bursts.
Operating System Concepts
Context Switch: Assume, main memory contains more than one process. If
cpu is executing a process,
• if time expires or if a high priority process enters into main memory, then
the scheduler saves information about current process in the PCB and
switches to execute the another process.
• The concept of moving CPU by scheduler from one process to other
process is known as context switch.
• Dispatcher: The main job of dispatcher is switching the cpu from one
process to another process. Dispatcher connects the cpu to the process
selected by the short term scheduler.
• Dispatcher latency: The time it takes by the dispatcher to stop one process
and start another process is known as dispatcher latency. If the dispatcher
latency is increasing, then the degree of multiprogramming decreases
CPU Scheduling: Scheduling Criteria
 CPU Utilization
 To make out the best use of the CPU and not to waste any CPU cycle, the
CPU would be working most of the time(Ideally 100% of the time).
Considering a real system, CPU usage should range from 40% (lightly
loaded) to 90% (heavily loaded.)
 Throughput
 It is the total number of processes completed per unit of time or rather
says the total amount of work done in a unit of time. This may range from
10/second to 1/hour depending on the specific processes.
 Turnaround Time
 It is the amount of time taken to execute a particular process, i.e. The
interval from the time of submission of the process to the time of
• Waiting Time
The sum of the periods spent waiting in the ready queue amount of time
a process has been waiting in the ready queue to acquire get control on
the CPU.
• Load Average
It is the average number of processes residing in the ready queue
waiting for their turn to get into the CPU.
• Response Time
Amount of time it takes from when a request was submitted until the
first response is produced. Remember, it is the time till the first response
and not the completion of process execution(final response).
In general CPU utilization and Throughput are maximized and other
Scheduling Algorithms

• First Come First Serve(FCFS) Scheduling


• Shortest-Job-First(SJF) Scheduling
• Priority Scheduling
• Round Robin(RR) Scheduling
• Shortest remaining time next
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling
First Come First Serve Scheduling
 The process which arrives first, gets executed first,(FIFO(First in First
out) Queue data structure,)
 A perfect real life example of FCFS scheduling is buying tickets at
ticket counter.
Consider the processes P1, P2, P3, P4 given in the below table,
arrives for execution in the same order, with Arrival Time 0,
and given Burst Time, let's find the average waiting time using the
FCFS scheduling algorithm.
fcfs scheduling with arrival time
Waiting time (WT) = Turn-around time (TAT) – Burst time (BT)
Process ID Arrival time Burst time
P1 2 2
P2 5 6
P3 0 4
P4 0 7
P5 7 4

Turn-around time (TAT) = Completion time (CT) – Arrival time (AT)

TAT = Burst time (BT) + Waiting time (WT)


Process Arrival Burst TAT=CT- WT=TAT-
CT RT
ID time time AT BT

P1 2 2 13 13-2= 11 11-2= 9 9

P2 5 6 19 19-5= 14 14-6= 8 8

P3 0 4 4 4-0= 4 4-4= 0 0

P4 0 7 11 11-0= 11 11-7= 4 4

P5 7 4 23 23-7= 16 16-4= 12 12

Average Waiting time = (9+8+0+4+12)/5 = 33/5 = 6.6 time unit


Average Turn-around time = (11+14+4+11+16)/5 = 56/5 = 11.2 time unit
Problems with FCFS Scheduling

• It is Non Pre-emptive algorithm, which means the process


priority doesn't matter.If a process with very least priority is being
executed, more like daily routine backup process, which takes more
time, and all of a sudden some other high priority process arrives,
like interrupt to avoid system crash, the high priority process will
have to wait, and hence in this case, the system will crash, just
because of improper process scheduling.
• Not optimal Average Waiting Time.
• Resources utilization in parallel is not possible, which leads
to Convoy Effect, and hence poor resource(CPU, I/O etc) utilization.
• What is Convoy Effect?
Convoy Effect is a situation where many processes, who need to
use a resource for short time are blocked by one process
holding that resource for a long time.
This essentially leads to poor utilization of resources and hence
poor performance.
Shortest Job First(SJF) Scheduling
• Shortest Job First scheduling works on the process with the
shortest burst time or duration first.
• This is the best approach to minimize waiting time.
• This is used in Batch Systems.
• It is of two types:
• Non Pre-emptive
• Pre-emptive
Non Pre-emptive Shortest Job First
• Consider the below processes available in the ready queue
for execution, with arrival time as 0 for all and given burst
times.
Problem with Non Pre-emptive SJF

If the arrival time for processes are different process with short
burst time have to wait for the current process's execution to
finish.

This leads to the problem of Starvation,


Non-Preemptive SJF with arrival tme
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
P1 P3 P2 P4

0 3 7 8 12 16

• Average waiting time = (0 + 6 + 3 + 7)/4 - 4


Operating System Concepts
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• Average waiting time = (9 + 1 + 0 +2)/4 - 3


Operating System Concepts
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
(smallest integer  highest priority).
– Preemptive
– Non preemptive
• Problem  Starvation – low priority processes may never
execute.

Operating System Concepts


Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and the time quantum is
q, then each process gets 1/n of the CPU time in chunks of at most q
time units at once. No process waits more than (n-1)q time units.
• Performance
– q large  FIFO
– q small  q must be large with respect to context switch, otherwise
overhead is too high.

Operating System Concepts


Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

• Typically, higher average turnaround than SJF, but better response.

Operating System Concepts


Time Quantum and Context Switch Time

Operating System Concepts


Turnaround Time Varies With The Time Quantum

Operating System Concepts


Multilevel Queue
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
• Scheduling must be done between the queues.
– Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
– Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR
– 20% to background in FCFS
Operating System Concepts
Multilevel Queue Scheduling

Operating System Concepts


Multilevel Feedback Queue
• A process can move between the various queues; aging can be
implemented this way.
• Multilevel-feedback-queue scheduler defined by the following
parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a process
– method used to determine when to demote a process
– method used to determine which queue a process will enter when that
process needs service
Operating System Concepts
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – time quantum 8 milliseconds
– Q1 – time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS. When it gains CPU,
job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is
moved to queue Q1.
– At Q1 job is again served FCFS and receives 16 additional milliseconds.
If it still does not complete, it is preempted and moved to queue Q2.
Operating System Concepts
Multilevel Feedback Queues

Operating System Concepts


Multiple-Processor Scheduling

• CPU scheduling more complex when multiple CPUs are available.


• Homogeneous processors within a multiprocessor.

Operating System Concepts


• Symmetric Multiprocessing: It is used where each processor is self-
scheduling. All processes may be in a common ready queue, or each
processor may have its private queue for ready processes. The scheduling
proceeds further by having the scheduler for each processor examine the
ready queue and select a process to execute.

• Asymmetric Multiprocessing: It is used when all the scheduling decisions


and I/O processing are handled by a single processor called the Master
Server. The other processors execute only the user code. This is simple
and reduces the need for data sharing, and this entire scenario is called
Asymmetric Multiprocessing.
Load sharing
Push Migration: In push migration, a task routinely checks the load on each
processor. If it finds an imbalance, it evenly distributes the load on each
processor by moving the processes from overloaded to idle or less busy
processors.
Pull Migration: Pull Migration occurs when an idle processor pulls a waiting
task from a busy processor for its execution.
Real-Time Scheduling

• Hard real-time systems – required to complete a critical task


within a guaranteed amount of time.
• Soft real-time computing – requires that critical processes
receive priority over less fortunate ones.

Operating System Concepts

You might also like