chapter04.ppt

advertisement
Chapter 4 – Processes and
Scheduling
Aims:
Define the basics states of a process.
Outline differing scheduling algorithms.
Outline multiprocessor systems and problem segmentation.
Define the main problems of multi-processor systems.
Outlines the usage of inter-process communications.
Define finite-state machines, giving an example.
Processes on a system
The perfect environment for a process is run on the processor, without
any disturbance from any other process. Unfortunately this would be
inefficient in the usage of the system for many reasons, such as:
• Inefficient use of the processor. The process being run could be
waiting for some input from a device, or waiting to send to an output
device. This is inefficient as other processes could be run while the
current process is waiting.
• Not allowing other processes to run, at the same time. On a
general-purpose computer, the user should be able to run other
processes at the same time, such as running multiple Internet sessions,
and word processor programs.
• Not running any support programs. The system should be able to
run other processes which provide support to the user’s processes.
Typical to these might be a process which responded to the user
printing a document. This process would then take the output from a
process and then store it. It would then communicate with the printer,
and allow the user program to get on with other tasks, without having
to communicate with the printer.
Process status
• Running. This is where the process is actually currently running on
the processor.
• Waiting. This is where the process is waiting on another process to
run and provide it with some data, or if a process is waiting to access
a resource. The waiting process can sometimes turn into a zombie
process, where a process terminates for some reason, but whose
parent process has not yet waited for it to terminate. A zombie
process is not a big problem, as it has no resources allocated to it.
Normally the only way to get rid of zombie processes is to reboot the
system.
• Ready. This is where the process is ready to be run on the processor,
and is not waiting for any other processes or has terminated.
• Terminated. This is where a process has finished its run, and all
resources that have been allocated to it must be taken away from it.
Process states
7
2
3
1
4
Running
Ready
EXIT
Hurry
Hurryup.
up.I’m
I’m
waiting
on
2
to
waiting on 2 totell
tell
me
mewhat
whatto
todo
do
5
8
Waiting
9
Terminated
Zombie
(Terminated, but still waiting for parent process)
6
Scheduling queues
• Long-term (Job) scheduler. This type of scheduler is used in batch
systems.
• Short-term scheduler. This type of scheduler typically uses a FIFO
(First In, First Out) queue, or a priority queue.
• Medium-term scheduler. This type of scheduler swaps processes in
and out to improve the job mix. Normally it schedules on the
following:
Medium term scheduler
•
•
•
•
Time since swapped in or out. Swapping involves moving the running
process to some temporary storage space (such as the local disk). A process
which has been swapped out for a longer time than another one, is more
likely to be chosen by the scheduler, over the one which has waited the least
time.
Processor time used. In order to be fair, a scheduler might pick a process
that has had a lesser time on the processor; over one that has had more time.
The time that a process has spent on the processor is no guarantee that it has
used its time efficiently.
Size. Typically a small process will most likely take the least amount of
resources, and processing time (but this is no guarantee), thus the scheduler
may pick the process which has the smallest size. An improved method is for
the scheduler to make an estimate about the amount of resources that the
process will require to complete, and then chose the next process on the
basis of this. An efficient schedule is to try and complete processes which can
be completed in the shortest time.
Priority. This is possibly one of the best methods that can be used to
determine which process should be run next. But who or what decides the
priority of a process? It is well known that users will always believe that their
process is more important than anyone else’s. Thus there must be some
independent method of determining the priority of a process.
Scheduling algorithms
Objectives
Description
Scheduler must try
to:
Fairness
Each process has a fair share of the processor
Maximize
Efficiency
Efficient use of the processor
Maximize
Throughput
Number of processes completed
Maximize
Turnaround
Time taken for processes to complete
Maximize
Waiting time
Time taken in the queue
Minimize
Predictability
Allowing a dependable response
Maximize
Response time
Time to react to actions
Minimize
Scheduler
Throughput
Turnaround
Waiting time
Fairness
Efficiency
Predictability
Response time
Scheduler
Maximised
Minimised
Scheduling algorithms
• First-Come, First-Served (FCFS). This type of scheduling is used
with non-preemptive operating systems, where the time that a
process waits in the queue is totally dependent on the processes
which are in front of it. The response of the system is thus not
dependable.
• Round Robin (RR). This is a first-come, first-served schedule with
preemption, where each process is given a finite time slice on the
processor. The queue is circular, thus when a process has been run, the
queue pointer is moved to the next process. This is a relatively
smooth schedule and gives all processes a share of the processor. As
children we would typically be assigned to things in a round-robin way,
especially when there were too many demands on a certain resource,
and we didn’t get enough time on it. A good example is when children
want to get access to a bouncy castle, and there’s a limit on the
number that can be on the castle at any one time. An example
schedule might be to allow a child onto the castle for a minute, and
then they must come off, and go back to the end of the queue. The
next child in the queue can then take their place on the castle, and so
on.
First-come, first-severed
Hooray.
Hooray.I Icould
could
stay
here
forever.
stay here forever.
Anyway,
Anyway,I’m
I’m
not
going
back
not going backtoto
the
theend
endofofthe
thequeue.
queue.
Hurry
Hurryup.
up.I’m
I’m
waiting.
You’ve
waiting. You’ve
been
beenon
onthat
that
processor
processor
for
forages
ages
1
2
Processor
Sorry.
Sorry.First
Firstcome,
come,
first
served.
first served.
3
4
Process queue
Look
Lookatatthe
the
size
of
that
size of that
queue!
queue!
5
6
Round-robin
Come
Comeon.
on.My
My
turn
soon
turn soon
2
3
Processor
1
Okay
OkayNo.1,
No.1,you’ve
you’ve
had
your
turn,
had your turn,
get
gettotothe
theback
backofof
the
thequeue.
queue.Next!
Next!
4
Process queue
5
Scheduling algorithms
• Shortest-Job-First (SJF). This is one of the most efficient algorithms,
and involves estimating the amount of time that a process will run for,
and taking the process which will take the shortest time to complete.
A problem for the scheduler is thus to determine the amount of time
that the process will take to complete its task. This is not an easy task.
For example, let’s say that we’ve got four tasks to complete. Task one
will take 1 hour, task two takes 2 hours, task three will take 3 hours,
and finally task four will take 8 hours to complete. We could schedule
the task to each get half-an-hour of processing time. Once we have
finished this time, we make a decision on what task to do next.
Scheduling algorithms
• Priority Scheduling. This type of scheduling assigns each process a
priority. It is typically used in general-purpose operating systems (such
as Microsoft Windows and UNIX) and can be used with either
preemptive or non-preemptive operating systems. The main problem
is to assign a priority to each process, where priorities can either be
internal or external. Internal priorities relate to measurable system
resources, such as time limits, memory requirements, file input/output,
and so on. A problem is that some processes might never get the
required priority and may never get time on the processor (which
leads to process starvation). To overcome this, low-priority waiting
processes can have their priority increased, over time (known as
ageing).
• Multilevel Queue Scheduling. This scheme supports several
different queues, and sets priorities for them. For example, a system
could run two different queues: foreground (interactive) and
background (batch), as illustrated in Figure 4.5. The foreground task
could be given a much higher priority over the background task, such
as 80%–20%.
Multilevel queue scheduling
I Iwish
wishI Iwas
was
important,
important,like
like
the
foreground
the foreground
processes
processes
A
B
C
Batch queue
2
3
4
Processor
Foreground queue
1
Okay.
Okay.Foreground
Foreground
queue
queuefirst.
first.Your
Your
turn
No.
2.
turn No. 2.
Scheduling algorithms
• Multilevel Feedback Queue Scheduling. This scheme is the most
complex, and allows processes to move between a number of
different priority queues. Each queue has an associated scheduling
algorithm. To support this there must be a way to promote and
relegate processes for their current queues.
Multiprocessor systems
• Heterogeneous. This is where there are a number of different types
of processors, each with their own instruction set. This is typically the
case in distributed computing, where a process is run over a number
of computers on a network. For it to work there must be a high-level
protocol that allows communication between the computers. Java is
an excellent programming language for this, as it can produce
machine-independent code.
• Homogeneous. This is where all the processors can run the same
code. In the scheduler there is either a common queue for all
processors (and they take processes from the queue once they have
completed a process) or there is a separate queue for each process
(this involves a scheduler deciding on what processes should be given
to each of the processors). The processing can either be asymmetric
multiprocessor where a single processor looks after scheduling, I/O
processing and other system activities, or symmetric multiprocessing,
where each processor is self-scheduling or has a master-slave
structure.
Multiprocessor segmentation example
Find
average
Processor
1
Processor
2
Pixels around
boundaries
must be intercommunicates
Processor
3
33 processing array
4 interfaces
Processor
4
44 processing array
6 interfaces
22 processing array
2 interfaces
nn processing array
2(n–1) interfaces
Trade-off for multi-processor systems
Multiprocessor array
Col1
Col1
Col2
Col2
[j,m,n]
PP1
1
Result
Result
Col3
Col3
[k,n,q]
PP2
2
Column data
passed South
[l,o,r]
PP3
3
Zero
Zero
Result
passed East
Result
Result
y,y,y
Result
Result
éa b
Arr = êêd e
êë g h
PP4
4
0,y,y
PP5
5
0,0,y
PP6
6
PP7
7
PP8
8
PP9
9
Sink
Sink
Sink
Sink
Sink
Sink
Zero
Zero
0,0,0
c ù é j k l ù éaj + bm + cp ak + bn + cq al + bo + cr ù
f úú êêm n oúú = êêdj + em + fp dk + en + fq dl + eo + fr úú
i úû êë p q r úû êë gj + hm + ip gk + hn + iq gl + ho + ir úû
Zero
Zero
Initialise
Initialise
value
valuefrom
from
matrix
matrix
Stop
Stop
Send
Sendresult
result
to
the
West
to the West
Send
Sendvector
vector
to
South
to South
Get
Getpartial
partial
product
productfrom
from
East
East
Vec
We tor to
st
rm
Te
te
a
n
i
Get
Getvector
vector
from
North
from North
r from
o
t
c
e
V
Nor th
V
So ecto
ut r t
h o
Multiprocessor algorithm
Update
Update
partial
partial
product
product
vector
vector
Vector from
East
Calculate
Calculate
partial
partial
product
product
Terminate Vector
to
West
Vector Vector
to
from
South North
Vector
from
East
Interrupts
Operating
system
Program
Programrequests
requests
Interrupt
Interrupt
controller
controller
Interrupt
Interrupt
Service
Service
Routine
Routine
(ISR)
(ISR)
Software interrupts
Hardware interrupts
High-level primitives
On shared memory systems the following are used:
• Semaphores. This involves setting flags, which allow or bar other
processes from getting access to certain resources. An analogy of a
semaphore is where two railway trains are using a single-track railway
line. When one train enters the single-track line, it sets a semaphore
which disallows the other train from entering the track. Once the
train on the single track has left the single track, it resets the
semaphore flag, which allows the other train to enter the single track.
• Signals. Signals are similar to interrupts, but are implemented in
software, rather than hardware. This is a primitive
Signals
• UNIX uses signals in a similar way to interrupts, but they are
implemented at a higher level. Events rather than hardware devices
generate these interrupts. Typically, software interrupt service routines
are called on certain signals. The signal handler sets the status of a
process, but a process may also put itself in a sleep mode, waiting for a
signal. One problem with this is that signals may get lost if they are
sent before the process goes into a waiting mode. One solution is to
set a flag in the process whenever it receives a signal. The process can
then test this flag before it goes into the wait state, if it is set; the wait
operation does not block the process.
Pipes
• Pipes allow data to flow from one process to another, in the required
way. Typically they are implemented with a fixed size storage area (a
buffer) in which one process can write to it, while the other reads
from it (when the data is available).
Pipes and sockets
Pipe
Socket
(on a remote
computer)
Process_1 | Process_2 | Process_3
Process
Process 11
Pipe
Process
Process 22
Pipe
Process
Process 33
Child
Child process
process A
A
(looks
(looks after
after
input
input pipe)
pipe)
Main process
(for process 2)
Child
Child process
process B
B
(looks
(looks after
after
output
output pipe)
pipe)
Messages
Process
Process
AA
signal(10)
Process
Process
AA
Process
Process
BB
(waiting
(waitingfor
for
new
newdata)
data)
Process
Process
BB
[waiting
[waitingfor
for
signal(10)]
signal(10)]
Signals
(a simple
method which
allows processes
to identify events)
New data
message
New data
message
Message
Message
Port
Port
(with
(withmessage
message
handler)
handler)
Message
(an improved method
where an indication
of the actual event
can be passed between
processes)
Finite state machines
State
number
State
State transition
1
2
3
Red
Red
Don’tWalk
Walk
Don’t
Red/Amber
Red/Amber
Don’tWalk
Walk
Don’t
Green
Green
Don’tWalk
Walk
Don’t
7
Red
Red
Walk
Flashing
Walk Flashing
6
Red
Red
Walk
Walk
Pedestrian
button unpressed
Amber
Amber
Don’t
Don’tWalk
Walk
5
Red
Red
Don’t
Don’tWalk
Walk
4
Pedestrian
button pressed
FSM (revised)
0
OFF
OFF
OFF
OFF
System
startup
System
shutdown
1
2
3
Red
Red
Don’tWalk
Walk
Don’t
Red/Amber
Red/Amber
Don’tWalk
Walk
Don’t
Green
Green
Don’tWalk
Walk
Don’t
7
Red
Red
Walk
Flashing
Walk Flashing
6
Red
Red
Walk
Walk
Pedestrian
button unpressed
Amber
Amber
Don’t
Don’tWalk
Walk
5
Red
Red
Don’t
Don’tWalk
Walk
4
Pedestrian
button pressed
Download