Sei sulla pagina 1di 7

REMINDER: Coursework Teams

IN2011 n For the coursework you are allowed to


Networks and Operating Systems work in teams of up to 3.
n Email Olga
(Olga.Herrero.1@city.ac.uk), cc'ing
the other team members.
Session 3 n Deadline 1 pm Monday 26 Feb.

Processes and Schedulers

IN2011 Nets & Ops Session 3 2

Overview Process Concept

n The notion of a process n An OS provides a platform for executing


l a program in execution, which forms the basis of programs
all computation l a program is a passive thing, just a chunk of code

n Process life-cycle n A program in execution is known as a


process
n Process creation (parent/child)
l also called jobs or tasks
n Scheduling n Note that in execution does not mean
running on the CPU
l many processes actually spend most of their time
waiting, with only short bursts of life on the CPU

IN2011 Nets & Ops Session 3 3 IN2011 Nets & Ops Session 3 4

Process State Process Life Cycle

n As a process executes, it changes state


l new: The process is being created

l running: Instructions are being executed


l waiting: The process is waiting for some event to
occur (eg I/O completion)
4 each OS may make finer distinctions, according to what
kind of event is being waited for
l ready: The process is waiting to be assigned to a
processor
l terminated: The process has finished execution
This is a very important diagram. Remember it.

IN2011 Nets & Ops Session 3 5 IN2011 Nets & Ops Session 3 6

1
Process State Process in Memory

n When the OS creates a new process it


must:
l Allocate memory to the process

l Create a data structure called a Process Control


Block (PCB) to keep track of the process state

n When a process terminates, the OS can


delete the PCB and reclaim the memory
the program code

IN2011 Nets & Ops Session 3 7 IN2011 Nets & Ops Session 3 8

Process Control Block (PCB) Process Creation

n A parent process creates children


OS creates and processes, which, in turn create other
maintains a PCB processes, forming a tree of processes
for every process
n Each process is identified and managed
via a process identifier (pid)
n Execution
l Parent and children may execute concurrently
l Or parent waits until children terminate

IN2011 Nets & Ops Session 3 9 IN2011 Nets & Ops Session 3 10

Process Creation (Cont) Process Creation

n Address space
l Child duplicate of parent

l Child has a program loaded into it

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

IN2011 Nets & Ops Session 3 11 IN2011 Nets & Ops Session 3 12

2
C Program Forking new Process Process Termination
int main()
{
pid_t pid;
n Process executes its final statement
/* fork another process */
and asks to be deleted by the operating
pid = fork();
if (pid < 0) { /* error occurred */
system (exit)
fprintf(stderr, "Fork Failed");
exit(-1);
l Output data from child to parent (via wait)
}
else if (pid == 0) { /* child process */
l Process’ resources are deallocated by the OS
execl("/bin/ls", "ls", NULL);
} n Some operating systems do not allow a
else { /* parent process */
/* parent will wait for the child to complete */ child to continue if its parent terminates
wait (NULL);
printf ("Child Complete"); l All children terminated - cascading termination
exit(0);
} l Note that this is not the default behaviour in Linux
}

IN2011 Nets & Ops Session 3 13 IN2011 Nets & Ops Session 3 14

Schedulers CPU Scheduler

n Long-term scheduler (job scheduler) n Selects from among the processes in


– selects which processes should be memory that are ready to execute, and
brought into the ready queue allocates the CPU to one of them
n Short-term scheduler (CPU n CPU scheduling decisions may take
scheduler) – selects which process place when a process:
should be executed next and allocates 1. Switches from running to waiting state
CPU 2. Switches from running to ready state (preemption)
3. Switches from waiting to ready
4. Terminates

IN2011 Nets & Ops Session 3 15 IN2011 Nets & Ops Session 3 16

Process Scheduling Queues Ready Queue And Various I/O Device Queues

n Job queue – the set of all processes in


the system
n Ready queue – the set of all processes
residing in main memory, ready and
waiting to execute
n Device queues – set of processes
waiting for an I/O device
n Processes migrate among the various
queues
IN2011 Nets & Ops Session 3 17 IN2011 Nets & Ops Session 3 18

3
Context Switch CPU Context Switch

n When CPU switches to another process,


the system must save the state of the old running

process and load the saved state for the


new process via a context switch
l Context of a process represented in the PCB
running

n Context-switch time is pure overhead;


the system does no useful work while
switching
running

IN2011 Nets & Ops Session 3 19 IN2011 Nets & Ops Session 3 20

Basic Concepts Typical Distribution of CPU-burst Times

n Maximum CPU utilization obtained with


multiprogramming
n Processes can be described as either:
l I/O-bound process – spends more time doing I/O
than computations, many short CPU bursts
l CPU-bound process – spends more time doing
computations; few, very long CPU bursts

n CPU burst distribution

IN2011 Nets & Ops Session 3 21 IN2011 Nets & Ops Session 3 22

Alternating Sequence of CPU And I/O Bursts Scheduling Criteria

n CPU utilization – keep CPU busy


n Throughput – # of processes that complete
their execution per time unit
n Turnaround time – amount of time to
execute a particular process
n Waiting time – amount of time a process has
been waiting in the ready queue
n Response time – amount of time it takes
from when a request is submitted until the
first response is initiated
IN2011 Nets & Ops Session 3 23 IN2011 Nets & Ops Session 3 24

4
Scheduler Aims Some Scheduling Schemes

n To maximise CPU utilisation and n First-Come, First-Served (FCFS)


Throughput l FIFO queue; non-preemptive

n To minimise Turnaround time, Waiting n Round Robin (RR)


time, Response time l FIFO queue + time quanta; preemptive

n These aims may be in conflict, priorities n Shortest Job First (SJF)


will be different for batch-processing l preemptive and non-preemptive variants
and interactive (multitasking) systems
n Priority-based
l interactive systems prioritise low response times
(ie fast responses)

IN2011 Nets & Ops Session 3 25 IN2011 Nets & Ops Session 3 26

FCFS Round Robin (RR)

n Simple scheme which services jobs from n Designed for time-sharing (multitasking)
the ready queue in First-Come-First- n Each process gets a small unit of CPU time
Served order (time quantum).
l New processes join the back of the queue l When quantum elapses, process is preempted and added
to the back of the ready queue
l Once a process is scheduled on the CPU it stays there
until it is interrupted: l implemented using timers to generate periodic interrupts

4 either by itself (system call, eg I/O request) n For n processes in the ready queue and time
4 or by a hardware interrupt (eg I/O device) quantum q, each process gets 1/n of the CPU
l No good for interactive systems, because CPU-bound time in chunks of at most q time units
processes hog the CPU l q must be large with respect to context switch time,
otherwise overhead is too high

IN2011 Nets & Ops Session 3 27 IN2011 Nets & Ops Session 3 28

Round Robin Example Gantt Diagram


IO demands

1 1 2 1,2 1 3 3

0- 5- 10- 15- 20- 25- 30- 35- 40- 45- 50- 55- 60- 65- 70- 75- 80- 85- 90- 95- 100- 105- 110- 115-
process arrival resource demands summary 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120
id time (ms) (not known in advance) 1 1 * 2 3 2 1 3 2 1 3 3 2 1 2 3 1 2 3 1 2 3 1 2

1 0 CPU I/O CPU I/O CPU CPU demands

10ms 10ms 10ms 10ms 20ms


P1 starts
P1 ends
2 15 CPU I/O CPU
15ms 10ms 25ms P2 starts P3 ends

3 20 CPU I/O CPU


20ms 10ms 15ms For each P3 starts P2 ends
process:

Assume context switching takes zero time Turnaround_time = process end_time – process start_time
Assume each process uses a different I/O device Length = sum of all the process' resource demand times
Assume time quantum = 5ms Waiting_time = process Turnaround_time – process Length

IN2011 Nets & Ops Session 3 29 IN2011 Nets & Ops Session 3 30

5
RR Calculations: Exercise Shortest-Job-First (SJF)

n Calculate turnaround, length, and waiting n Associate with each process the length
times for each process of its next CPU burst. Use these
l P1
lengths to schedule the process with the
l P2
l P3
shortest time
n Calculate: n SJF is optimal – gives minimum
l average turnaround time average waiting time for a given set of
l average waiting time processes
n Calculate CPU utilisation (percentage of CPU l The difficulty is knowing the length of the next
time spent running processes) CPU burst!

IN2011 Nets & Ops Session 3 31 IN2011 Nets & Ops Session 3 32

Length of Next CPU Burst Prediction of the Length of the Next CPU Burst

n Can only estimate the length


n Can be done by using the length of
previous CPU bursts, using exponential
averaging:
1. tn = actual length of n th CPU burst
2. τ n+1 = predicted value for the next CPU burst
3. 0 ≤ α ≤ 1
4. Define: τ n+1 = α tn + (1− α ) τ n .

IN2011 Nets & Ops Session 3 33 IN2011 Nets & Ops Session 3 34

Priority Scheduling Thread Scheduling

n A priority number (integer) is associated n There is a logical distinction between


with each process user-level and kernel-level threads
n The CPU is allocated to the process l but Windows and Linux both use a one-to-one
model, which keeps things simple
with the highest priority (smallest integer
º highest priority) n Technical detail: An OS with kernel-
l Problem º Starvation – low priority processes
level threads schedules kernel-level
may never execute threads, not processes
l Solution º Aging – as time progresses increase l Linux literature often uses “task” as generic term
the priority of older processes

IN2011 Nets & Ops Session 3 35 IN2011 Nets & Ops Session 3 36

6
Multiple-Processor Scheduling Linux Scheduling (before 2.6.23)

n CPU scheduling is more complex when n Constant order O(1) scheduling time
multiple CPUs are available n Two priority ranges: time-sharing and
n Read [OSC] Section 5.5 real-time
n Real-time range from 0 to 99 and nice
value from 100 to 140
l essentially different schedulers used for real-time
and non real-time scheduling

IN2011 Nets & Ops Session 3 37 IN2011 Nets & Ops Session 3 38

Linux Scheduling (from 2.6.23) What to do now

n So-called "Completely Fair Scheduler"


l uses a red-black tree (self-balancing binary tree) to
order processes by time spent on CPU
n Labs
4 red-black tree has O(log N) access/update time n Preparation for Session 4
l scheduler picks the process which has spent least
time on CPU (left-most in the tree) l see Moodle for details
l allocates a quantum based on how long is "fair" given
how many other processes are ready and how long
the chosen process has been held in the ready state

n (Details not examinable)

IN2011 Nets & Ops Session 3 39 IN2011 Nets & Ops Session 3 40

REMINDER: Coursework Teams

n For the coursework you are allowed to


work in teams of up to 3.
n Email Olga
(Olga.Herrero.1@city.ac.uk), cc'ing
the other team members.
n Deadline 1 pm Monday 26 Feb.

IN2011 Nets & Ops Session 3 41

Potrebbero piacerti anche