Process - CPU Scheduling
Process - CPU Scheduling
• Process
Process is an instance of a program in execution
A program is passive; a process active.
Multiple instances of the same program are different processes
Process has three main components. You can see the activeness of a process with respect to these three
components.
1. Address space (Figure 6) The memory that the process can access.
Consists of various pieces: the program code, static variables, heap, stack, etc.
The address space is how the process sees its own memory. Not necessarily how the memory is laid out
in physical RAM.
2. CPU state
The CPU registers associated with the running process Includes general purpose registers, program
counter, stack pointer, etc.
We can see an example where the CPU state is used. Let take there are two process P1 and P2. Both the
processes are using a statement, i.e. “a=b+c”. This statement can be divided into three instructions,
i.e. “mov b AX; add ax c; mov AX a;”. Although both the processes using the same statement, still
the memory required for a, b , and c are different from each other. This is because these variables
are stored corresponding to their process address space. But the registers which are used in both the
process are belongs to CPU. So, whenever CPU switches from one process to another process all the
registers information is pushed into the stack area of the process address space. Similarly, when the
process again resumes, the register information stored in the stack is popped and stored in the actual
CPU registers.
3. OS resources
Various OS state associated with the process Examples: open files, network sockets, etc.
OS represents each process using Process Control Block(PCB). One PCB per process. OS maintains a
list of PCB’s for all processes.
• Contents of PCB
12
– Pointer to next process
– Process id
– parent process id
– Process state
– CPU state: CPU register contents
– Scheduling info (Priority info, pointer to scheduling queue)
– Memory info (Pointers to different memory areas)
– Open file information
– Accounting info(CPU time,...)
• Process scheduling
CPU is allotted to the process, and process runs. (Figure 9)
A newly arrived process is put in the ready queue. Processes waits in ready queue for allocating the CPU.
Once the CPU is assigned to a process, then that process will execute. While executing the process, 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 in the ready
queue.
13
New
Admit Suspend
Admit
Activate Dispatch
Ready, Ready Running Exit
suspend
Suspend Time out
Event Event
Event
Occurs Wait
Occurs
Activate
Blocked, Blocked
suspend
Suspend
– Scheduler : Taking the responsibility of switches process (PCB) from one queue to another. It also picks
a process from the ready queue according to some scheduling policy and assign it to CPU.
∗ Long term scheduler
· It is also called job scheduler
· Selects which processes should be brought into the ready queue
· It is used to move the process from new to ready state and from waiting to ready state.
· The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. If the average rate of number of processes arriving in memory is
equal to that of departing the system then the long term scheduler may need to be invoked only
when a process departs the system. It may also be very important that long term scheduler
should take a careful selection of processes i.e. processes should be combination of CPU and
I/O bound types. If all processes are I/O bound, the ready queue will always be empty and the
short term scheduler will have nothing to do. If all processes are CPU bound. no process will
be waiting for I/O operation and again the system will be unbalanced. Therefore, the long term
scheduler provides good performance by selecting combination of CPU bound and I/O bound
process.
· Controls the degree of multiprogramming (no. of jobs in memory)
· Invoked infrequently (seconds, minutes)
∗ Short term scheduler
· It is also called CPU scheduler
· Selects which process should be executed next and allocates CPU (moves process from ready
state to running state)
14
Figure 9. Scheduling
• How does the scheduler gets scheduled?(Suppose we have only one CPU)
• Context switching
This is a mechanism to change the control of the CPU from one process to another process.(Figure 12)
Context-switch time is an overhead; the system does not do any useful work while switching.
15
Figure 10. Scheduling queue
3 Thread
Each thread has a thread control block (TCB). TCB contains processor state(Registers), thread state, and
pointer to corresponding PCB.
Context switch between two threads in the same process does not need to change address space.
Context switch between two threads in different processes must change address space.
• Thread system classification (Figure 14)
• Different types of thread and their functionality.
Basically there are two types :
16
Figure 12. Context switching
17
Figure 14. Thread system classification
∗ if one thread wants for any I/O, then all the threads belongs to same process will automatically
blocked.
∗ Since the OS does not know about the existence of the user-level threads, it may make poor schedul-
ing decisions.
– How to create a user-level thread? (Figure 16)
Thread library maintains a TCB for each thread in the application, just a linked list. Allocate a separate
stack for each thread (usually with malloc).
18
Figure 16. User Level Thread
4 CPU scheduling
– Determining which processes run when there are multiple runnable processes.
– As a whole we can say that there is a pool of runnable processes contending for the CPU and compete
for resources. The job of the scheduler is to distribute the scarce resource of the CPU to the different
processes “fairly” in such a way that the overall performance will be maximized.
• What is it goal?
– A process will run for a while (CPU burst), perform some IO (IO burst), then run for a while more
(the next CPU burst). These things will continue till the completion of the process.
• Types of Process
19
– IO Bound processes:
∗ processes that perform lots of IO operations. Each IO operation is followed by a short CPU burst
to process the IO.
– CPU bound processes:
∗ processes that perform lots of computation and do little IO.
• When do scheduling decisions take place? / When does CPU choose which process to run?
Scheduling done only under 1 and 4 condition is non-preemptive. All other scheduling is preemptive.
• Dispatcher
Dispatcher module gives control of the CPU to the process selected by the scheduler; this involves:
– switching context
– switching to user mode
– jumping to the proper location in the user program to restart that program
Dispatch latency - time it takes for the dispatcher to stop one process and start
• How to evaluate scheduling algorithm?
There are many scheduling criteria:
• Scheduling Algorithm
20
∗ Maintains one ready queue. OS runs the process at head of queue, new processes come in at the
end of the queue.
∗ A process does not give up CPU until it either terminates or performs IO. (non-preemptive)
∗ Problem: Average waiting time is more (If initial processes have more CPU burst)
– Shortest-Job-First (SJF)
∗ Process has less CPU burst will execute first.
∗ Eliminate Waiting and Turnaround time.
∗ This algorithm is optimal with respect to average waiting time.
∗ Problem: how does scheduler figure out how long will it take the process to run?
· Scheduler, must use the past to predict the future. We can use weighted average technique.
s n+1 = w T n + (1 - w)s n
, where
sn+1 be the predicted value of next CPU burst
w is the weightage, 0 ă= w ă= 1
Tn be the measured burst time of the nth burst.
s0 is defined as some default constant or system average.
∗ Preemptive vs. Non-preemptive SJF scheduler.
· Preemptive SJF: If the new process has less CPU burst time as compare to remaining CPU
burst time of the current process then the CPU preempts the running process and executes the
new process.
· Non-Preemptive SJF: Scheduling decision only takes place when running process voluntarily
gives up CPU. In effect, it allows every running process to finish its CPU burst.
– Priority Scheduling
∗ Each process is given a priority, then CPU executes process with highest priority.
∗ Problem: starvation or blocking of low-priority processes.
· Can use aging to prevent starvation - As the time progress, increase the priority of a process
which stays at ready state but not run.
– Round-robin scheduling
∗ Give response to users in a reasonable time.
∗ Similar to FCFS but with preemption.
∗ Have a time quantum or time slice. Implementing round-robin requires timer interrupts.
∗ Let the first process in the queue run until it expires its quantum (i.e. runs for as long as the time
quantum), then run the next process in the queue.
∗ It gives good response time, but can give bad waiting time.
∗ Problem: with a small quantum - context switch overhead.
– Multilevel Queue Scheduling
∗ Classify processes into separate categories and give a queue to each category.
– Multilevel Feedback Queue Scheduling
∗ Like multilevel scheduling, except processes can move between queues as their priority changes.
∗ Can be used to give IO bound and interactive processes CPU priority over CPU bound processes.
∗ Can also prevent starvation by increasing the priority of processes that have been idle for a long
time.
21