Scheduling
Scheduling
Contents
Basic
Concepts
Scheduling Criteria
Scheduling Algorithms
Process Creation
A process may create several new processes, via a create-process system
call during the course of execution.
Generally, process identified and managed via a process identifier (pid)
The creating process is called parent process and the new processes are
called children of that process, children processes, in turn create other
processes, forming a tree of processes.
Resource sharing
fork()
Copy 1:
Parent with all the code
after fork().
Parent process will
execute the next
instruction following the
fork() system call
Copy 2:
Child with all the
code after fork().
Child process will
execute the next
instruction
following the
fork() system call
pid = fork();
returns pid of
the child which
is non zero
pid = fork();
returns pid =0
in the child
Process Termination
Process executes last statement and asks the operating
system to delete it (exit)
Output data from child to parent (via wait)
Process resources are deallocated by operating system
Parent may terminate execution of children processes (abort)
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some operating system do not allow child to continue if
its parent terminates
All children terminated - cascading termination
C Program Examples
#include <stdio.h>
main()
{
int pid;
printf("I'm the original process with PID %d and PPID %d.\n", getpid(), getppid());
pid=fork();
/* Duplicate. Child and parent continue from here.*/
if (pid!=0)
/* pid is non-zero, so I must be the parent */
{
printf("I'm the parent process with PID %d and PPID %d.\n", getpid(),
getppid());
printf("My child's PID is %d.\n", pid);
}
else
/* pid is zero, so I must be the child. */
{
printf("I'm the child process with PID %d and PPID %d.\n", getpid(),
getppid());
}
printf("PID %d terminates.\n",getpid()); /* Both processes execute this */
}
Orphan Processes
If a parent dies before its child, the child is
automatically adopted by the original "init"
process, PID 1.
By inserting a sleep statement into the
child's code, the parent process
terminates before the child.
Orphan Processes
#include <stdio.h>
main()
{
int pid;
printf("I'm the original process with PID %d and PPID %d.\n", getpid(), getppid());
pid=fork();
/* Duplicate. Child and parent continue from here.*/
if (pid!=0)
/* Branch based on return value from fork() */
{
/* pid is non-zero, so I must be the parent */
printf("I'm the parent process with PID %d and PPID %d.\n", getpid(),
getppid());
printf("My child's PID is %d.\n", pid);
}
else
{
/* pid is zero, so I must be the child. */
sleep(50);
/*Make sure that the parent terminates first. */
printf("I'm the child process with PID %d and PPID %d.\n", getpid(),
getppid());
}
printf("PID %d terminates.\n",getpid());
/* Both processes execute this */
}
Output
I'm the parent process and my PID is 13464
I'm the child process with PID 13465 and PPID
13464.
I'm the parent process with PID 13464 and
PPID 13409
A child with PID 13465 terminated with exit
code 42
PID 13465 terminates
exit()
#include <stdio.h>
main()
{
printf("I'm going to exit with return code 42\n");
exit(42);
}
OutputI'm going to exit with return code 42
Zombie process
#include <stdio.h>
main()
{
int pid;
pid=fork(); /* Duplicate */
if (pid!=0) /* Branch based on return value from fork() */
{
while (1) /* never terminate, and never execute a wait() */
sleep(1000);
}
else
{
exit(42); /* Exit with a random number */
}
}
Zombie process
$ zombie.exe & ... execute the program in the background.
[1] 13545
$ ps
PID TT STAT TIME COMMAND
13535 p2 s 0:00 -ksh(ksh) ...the shell
13545 p2 s 0:00 zombie.exe...the parent process
13536 p2 z 0:00 <defunct> ...the zombie child process
13537 p2 R 0:00 ps
$ kill 13545
... kill the parent process.
[1] Terminated zombie.exe
$ ps
... notice the zombie is gone now.
PID TT STAT TIME COMMAND1
3535 p2 s 0:00 -csh(csh)
13548 p2 R 0:00 ps
CPU Scheduling
Determining
Dispatcher
Scheduling Criteria
CPU utilization Percentage of time that the CPU is doing useful work
Throughput Number of processes completed per unit time. May range
from 10 / second to 1 / hour depending on the specific processes.
Turnaround time Time required for a particular process to complete,
from submission time to completion.
It is sum of periods spent waiting to get into memory, waiting in the
ready queue, execution on the CPU, and doing I/O
Waiting time amount of time a process has been waiting in the ready
queue to get on the CPU
Response time amount of time it takes from when a request was
submitted until the first response is produced, not output (for timesharing environment)
Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
Most of the time we are interested in optimizing the
average Value.
First-Come, First-Served
(FCFS) Scheduling
Algorithm is non-preemptive.
The FCFS schedulers Gantt chart for these tasks would be:
The OS incurs some overhead each time it switches between processes due
to context switching. We will call this overhead cs.
CPUUtilization -26/(26+3cs)
Turnaroundtime - (8+12+21+26+6cs)/4 = 16.5 ignoring cs
Waiting-(0+8+12+21+6cs)/4=10.25ignoringcs
Throughput-4/(26+3cs)
Response-(0+8+cs+12+2cs+21+3cs)/4=10.25ignoringcs
24
3
3
First-Come, First-Served
(FCFS) Scheduling
P1
0
P2
24
P3
27
30
P2
0
P3
3
P1
6
30
First-Come, First-Served
(FCFS) Scheduling
Advantages
Disadvantages
Shortest-Job-First (SJF)
Scheduling
Shortest-Job-First (SJF)
Scheduling Example 1
Suppose the scheduler is given 4 tasks, A, B, C and D. Each task requires a certain
number of time units to complete.
Task
Time units
A
CPUUtilization -26/(26+3cs)
AvgTurnaroundtime-(4+9+cs+17+2cs+26+3cs)/4 = 14 ignoring cs
AvgWaiting-(0+4+cs+9+2cs+17+3cs)/4 = 7.5 ignoring cs
Throughput-4/(26 + 3cs)
AvgResponse-(0+4+cs+9+2cs+17+3cs)/4 = 7.5 ignoring cs
By comparison, if we were using the FCFS schedulingscheme, the average waiting
time would be 10.25 milliseconds.
Example 2: Non-Preemptive
SJF
Process Arrival Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
P1
0
P3
7
Burst Time
7
4
1
4
P2
P4
12
16
SJF (non-preemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 = 4(ignoring cs)
Example 3: Preemptive
SJF(SJRT)
Process
Arrival Time
Burst Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
P1
0
P2
2
P3
4
P2
5
P4
7
P1
11
16
SJF (preemptive)
Average waiting time = (9 + 1 + 0 +2)/4 = 3 (ignoring cs)
Shortest-Job-First (SJR)
Scheduling
Advantage
It is considered as an optimal algorithm as it gives the minimum
average waiting time
Moving a short burst ahead of a long one reduces wait time of short
process more than it lengthens wait time of long one.
Disadvantage
The problem is to know the length of time for which CPU is
needed by a process. A prediction formula can be used to predict
the amount of time for which CPU may be required by a process.
Problem of starvation
Priority Scheduling
Burst Time
P1
53
P2
P3
68
P4
17
24
81
20
94
97
P1
53
17
68
24
P2
20
37
P3
P4
57
P1
77
P3
P4
P1
P3
P3
on
As can be seen from this graph, the average turnaround time of a set of processes
does not necessarily improve as the time quantum size increases. In general, the
averageturnaroundtimecanbeimprovedifmostprocessesfinishtheirnextCPUburst
inasingletimequantum.
Process
P1
P2
P3
P4
Burst time
12
8
20
7
Type
FG
BG
FG
BG
P1
Q1
Burst time
12
8
20
7
16
P3
Q1
P1
Q1
20
P3
Q1
Type
FG
BG
FG
BG
28
P3
Q1
32
P2
Q2
40
47
P4
Q2
Scheduling
A new job enters queue Q1which is servedRRwithquantum8ms. When it gains
CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is
moved to queue Q2.
At Q2 job is again served RR and receives 16 additional milliseconds. If it still
does not complete, it is preempted and moved to queue Q3.
Preemptive Scheduling. If a process arrives in Q1, then processes in Q2 and Q3
will stop its execution
Burst time
17
25
8
32
18
Advantages
It allows a process to move between queues. This is fair for I/O bound
processes, they do not have to wait too long.
Aging prevents starvation.
More flexible
Disadvantages
Moving of processes around queues produces more CPU overheads.
Very complex algorithm
Multiple-Processor Scheduling
More complicated,
As now there is more than one CPU which must be kept busy
and in effective use at all times.
Issues in multiple-processor
scheduling
Disadvantages:
Approaches
to Multiple-Processor
a failure of the master brings down the whole system,
the master can become a performance bottleneck.
Scheduling
Processor Affinity
Hard affinity
Processor Affinity
Main memory architecture can also affect process affinity, if particular CPUs have
faster access to memory on the same chip or board than to other memory loaded
elsewhere. ( Non-Uniform Memory Access, NUMA. ) As shown below, if a process
has an affinity for a particular CPU, then it should preferentially be assigned memory
storage in "local" fast access areas.
Load Balancing
Keep the workload evenly distributed over the processors so that one
processor won't be sitting idle while another is overloaded.
Systems using a common ready queue are naturally self-balancing, and
do not need any special handling. Most systems, however, maintain
separate ready queues for each processor.
Balancing can be achieved through either push migration or pull
migration:
Find
References
Chapter
5 of A.Silberschatz, P.Galvin, G.
Gagne, Operating systems concepts Willey
international company (8th edition)