Uploaded by Kumesa Jota

2.Process

advertisement
Chapter 2: Processes
1
Process Concept
o
Process
 Also called task
 The entity that can be assigned to and executed on a
processor
 A unit of activity characterized by a single sequential
thread of execution, a current state, and an associated
set of system resources
 a program in execution; process execution must progress
in sequential fashion
o
list the sequence of instructions that execute
1-
2
Process Concept…
• A process has the following:
 The code (text section)
 Program counter and contents of the registers
 A data section for global variables.
• Conceptually, each process has its own virtual
CPU.
• In reality, of course, the real CPU switches back
and forth from process to process.
• This rapid switching back and forth is called
multiprogramming.
• A process is an activity of some kind which has a
program, input, output, and a state.
3
Program Vs Process
• Program
It is sequence of instructions defined to perform
some task
o It is a passive entity
o
• Process
 It is a program in execution
 It is an instance of a program running on a
computer
 It is an active entity
 A processor performs the actions defined by a
process
1-
4
Analogy
• Real life example: Consider a computer scientist who is baking
a birthday cake for her daughter and who is interrupted by her
daughter’s bleeding accident
• Sequence of actions
o
o
o
o
o
o
o
Bringing ingredients i.e. flour, sugar, eggs, etc
Placing the mixture into the oven
Following the baking processes
Hearing a cry and analyzing it to be because of bleeding
Recording baking processes state and switching to provide
first aid service
Providing first aid service
Coming back and resuming the baking process
1-
5
• Analysis
o
o
o
o
o
o
Processes Baking Cake First Aid
Processor Comp.Scientist Comp Scientist
Program Recipe First Aid Book
Input Ingredients First Aid Kit
Output Cake First Aid Service
States Running, Idle Running, Idle
1-
6
Two-State Process Model
• Process may be in one of the
two states
• Running
• Not-running
o Ready to execute
o Blocked, waiting for I/O
7
Process State
• As a process executes, it changes state
o
o
o
o
o
new: A process just created but not yet admitted to the pool of
executable processes by the operating system
 A code for the process is not yet loaded to memory.
running: Instructions are being executed
Waiting(Blocked): The process is waiting for some event to
occur.
Ready: A process that is not currently executing but that is
ready to be executed as soon as the operating system
dispatches it
Terminated(Exited): The process has finished execution
8
Process State change
• The main process state transitions are
• Null New
o It occurs when a new process is created to execute a program
 New Ready
o It occurs when the operating system is prepared to take on
additional process and a new process is admitted
 Ready Running
o It occurs when the operating system chooses one of the
processes in the ready state for running
o The process is called dispatching and it is caused by the
dispatcher (scheduler)
o Dispatch (process name)
9
Process State …
Running Blocked
• It occurs when a process requests something for which it must wait
o
o
Blocked
Ready
 It occurs when the event for which a process has been
waiting occurs
 Wakeup (process name)
o Running
Ready
 It occurs when a process is preempted by the operating system
 Examples
 When time is expired
 When a blocked process with higher priority is moved to ready
state
 Timeout (process name)
10
Process State …
Running Exit
 It occurs when a currently running process is
terminated
 Halt (process name) or Abort (process name)
o Ready
Exit
o Blocked
Exit
 It occurs due to two most common reasons
 When a parent process terminates a child process
 When a parent process is terminated
• The next slide shows a five state model of process state
transition
o
11
A Five-State Model
•
•
•
•
•
Running
Ready
Blocked
New
Exit
Figure 2-4: A Five state Process Model
12
Suspended Processes
• In multiprogramming systems
o memory can hold multiple processes at a time.
o the processor moves to another process when one process is
blocked(maximizes CPU utilization).
o But processor is faster than I/O so all processes could be waiting for
I/O(CPU becomes idle)
o So what should be done?
o Have large sized memory
o problem: cost and larger memory does not mean that large
number of processes in memory rather a large process in
memory.
o Swap blocked/suspended processes to disk to free up more memory
o Blocked state becomes suspend state when swapped to disk
• Two new states
o Blocked, suspend
o Ready, suspend
13
Suspended Process…
• there are four states based on whether a process is waiting on an
event (blocked or not) and whether a process has been swapped
out of main memory (suspended or not).
o Ready: The process is in main memory and available for
execution.
o Blocked: The process is in main memory and awaiting an
event.
o Blocked/Suspend: The process is in secondary memory and
awaiting an event.
o Ready/Suspend: The process is in secondary memory but is
available for execution as soon as it is loaded into main
memory.
o The next two slides depicts the seven process transition states
14
Suspended Process…
A Seven-state Process Model
15
Suspended Process…
A Seven-state Process Model
Figure 2-10: Suspended States
16
Deadlock
Deadlock
• Permanent blocking of a set of processes that
either compete for system resources or
communicate with each other
• No efficient solution
• Involve conflicting needs for resources by two or
more processes
Deadlock
Figure
3.1
Deadlock Joint Progress Diagram
Figure
3.2
Deadlock
Figure
3.3
Reusable Resources
• Used by only one process at a time and not
depleted by that use
• Processes obtain resources that they later release
for reuse by other processes
Reusable Resources
• Processors, I/O channels, main and secondary
memory, devices, and data structures such as
files, databases, and semaphores
• Deadlock occurs if each process holds one
resource and requests the other
Reusable Resources
Figure
3.4
Reusable Resources
• Space is available for allocation of 200Kbytes,
and the following sequence of events occur
P1
...
Request 80 Kbytes;
...
Request 60 Kbytes;
P2
. . . Request 70 Kbytes;
...
Request 80 Kbytes;
• Deadlock occurs if both processes progress to their
second request
• What if the amount is not known? eliminating the
possibility by using virtual memory
Consumable Resources
• Created (produced) and destroyed (consumed)
• Interrupts, signals, messages, and information in
I/O buffers
• Deadlock may occur if a Receive message is
blocking
• May take a rare combination of events to cause
deadlock
Example of Deadlock
• Deadlock occurs if receives blocking
P1
...
Receive(P2);
P2
...
Receive(P1);
...
...
Send(P2, M1);
Send(P1, M2);
Resource Allocation Graphs
• Directed graph that depicts a state of the system
of resources and processes
Conditions for Deadlock
• Mutual exclusion
– Only one process may use a resource at a time
• Hold-and-wait
– A process may hold allocated resources while
awaiting assignment of others
Conditions for Deadlock
• No preemption
– No resource can be forcibly removed form a process
holding it
• Circular wait
– A closed chain of processes exists, such that each
process holds at least one resource needed by the next
process in the chain
Resource Allocation Graphs
Resource Allocation Graphs
Figure
3.6
Figure
3.1b
Possibility of Deadlock
• Mutual Exclusion
• No preemption
• Hold and wait
Existence of Deadlock
•
•
•
•
Mutual Exclusion
No preemption
Hold and wait
Circular wait
Deadlock Prevention
• Mutual Exclusion
– Must be supported by the OS
• Hold and Wait
– Require a process request all of its required resources
at one time
Deadlock Prevention
• No Preemption
– Process must release resource and request again
– OS may preempt a process to require it releases its
resources
• Circular Wait
– Define a linear ordering of resource types
Deadlock Avoidance
• A decision is made dynamically whether the
current resource allocation request will, if
granted, potentially lead to a deadlock
• Requires knowledge of future process requests
Two Approaches to
Deadlock Avoidance
• Do not start a process if its demands might lead
to deadlock
• Do not grant an incremental resource request to a
process if this allocation might lead to deadlock
Strategies Once Deadlock Detected
• A check for deadlock can be made as frequently
as each resource request or, less frequently,
depending on how likely it is for a deadlock to
occur.
• Abort all deadlocked processes
• Back up each deadlocked process to some
previously defined checkpoint, and restart all
process
– Original deadlock may occur
Strategies Once Deadlock Detected
• Successively abort deadlocked processes until
deadlock no longer exists
• Successively preempt resources until deadlock no
longer exists
Advantages and Disadvantages
Scheduling and Dispatch
Introduction
• CPU scheduling is the basis of multiprogrammed OS. By switching the CPU
among processes, the OS can make the
computer more productive.
CPU Scheduler
• Selects from among the processes in memory that
are ready to execute, and allocates the CPU to one
of them.
• This is by short term scheduler.
• CPU scheduling decisions may take place when a
process:
1.Switches from running to waiting state
2.Switches from running to ready state
3.Switches from waiting to ready
4.Terminates
• Scheduling under 1 and 4 is nonpreemptive (no
choice since new process)
• The process keeps the CPU until it finishes.
• All other scheduling is preemptive
Dispatcher
• Dispatcher module gives control of the
CPU to the process selected by the shortterm 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
another running
Scheduling Criteria
• CPU utilization – keep the CPU as busy as
possible
• Throughput – # of processes that complete their
execution per time unit
• Turnaround time – amount of time to execute a
particular process
• Waiting time – amount of time a process has
been waiting in the ready queue
• 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
First-Come, First-Served (FCFS)
Scheduling
Process Burst Time
P1
24
P2
3
P3
3
• Suppose that the processes arrive in the order: P1
, P2 , P3
The Gantt Chart for the schedule is:
P1
0
P2
24
P3
27
30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2
0
•
•
•
•
P3
3
P1
6
30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
Shortest-Job-First (SJF)
Scheduling
• Associate with each process the length of its next
CPU burst. Use these lengths to schedule the
process with the shortest time
• Two schemes:
– nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst
– preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
• SJF is optimal – gives minimum average waiting
time for a given set of processes
Example of Non-Preemptive SJF
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
0
3
P3
7 8
P2
P4
12
16
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
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
0
P2 P3
2
4
P2
5
P4
7
P1
11
16
• Average waiting time = (9 + 1 + 0 +2)/4 = 3
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
– nonpreemptive
• SJF is a priority scheduling where priority is the
predicted next CPU burst time (a special case of
general priority algorithm)
• Problem  Starvation – low priority processes may
never execute
• Solution  Aging – as time progresses increase the
priority of the process
Cont…
• The priority is defined either
internally(memory req., time limit, ratio
of av. I/O to CPU burst time) or
externally(importance
of
process,
department sponsoring the work, others
like political factors).
Cont…
Round Robin (RR)
• Similar to FCFS except for the preemption.
• 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.
• CPU burst< time quantum -> the process
releases by itself or interrupt occur.
• Avg. waiting often longer
Cont…
• 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.
• If time quantum extremely large same as
FCFS and if extremely small processor
sharing(in theory)
• 80% cpu burst should be shorter than time
quantum.
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 121134 154162
Typically, higher average turnaround than
SJF, but better response
Thank You!!!
Download