第二章 进程与调度 (Processes and Scheduling)

advertisement
Chapter 2
Processes and Scheduling
(I)
Contents
 Foundation:Process Description and Control
 Implement: Mutual Exclusion and Synchronization
 Avoidance: Deadlock(死锁) and Hungry(饥饿)
 Solution:
Several Classical(经典) Problems
 About:
Process Communications
 Policy:
Process scheduling
PROCESSES AND SCHEDULING
Process Description and Control (p101)
Concurrence (vs.parallel) execution of processes
(supplement(补充))
Process states and transition
Process description(PCB)
Process control(Primitive/Atomic)
PROCESSES AND SCHEDULING
Serial Execution of Program
 Serial execution characteristics
‫ ٭‬Sequence ( 顺 序 ) 、 isolate ( 独 立 ) 、
recurrence(再现)
Concurrence execution characteristics
‫ ٭‬discontinuity、no-isoloate、no-recurrence
Bernstein condition
R(P1) ∩W(P2)∪W(P1) ∩R(P2)∪W(P1) ∩W(P2) = {Φ}
PROCESSES AND SCHEDULING
Some Problems for Process Management(p101)
How to dispatch(分派) CPU to multi-processes
How to allocate resource to multi-processes and
avoid deadlock and hungry
How to implement the process communication
PROCESSES AND SCHEDULING
Process Concept(p60)
 A program in execution
 The “animated spirit” of a program
 The entity that can be assigned to and
executed on a processor
PROCESSES AND SCHEDULING
Process Characteristics(Supplement)
Dynamic: create->dispatch->request I/O->
redispatch(重分派)->exit
Concurrence:
Independence:Each process does not feel the
existence of other processes.
Discontinuity:
PROCESSES AND SCHEDULING
Architecture of Process(p120)
User data and program
System stack
PCB,Process Control Block
PROCESSES AND SCHEDULING
Process States
Concurrence execution of processes
States of processes(2-states、5-states)
PROCESSES AND SCHEDULING
Example of Concurrence Execution(p102)
An example:
‫ ٭‬Three processes are fully loaded in main memory.
‫ ٭‬A small dispatcher program that moves the processor
from one process to another.
‫ ٭‬The first 12 instruction executed in processes A and C.
‫ ٭‬Process B executes four instructions and the fourth
instruction invokes(激发) an I/O operation for
which the process must wait.
PROCESSES AND SCHEDULING
PROCESSES AND SCHEDULING
Example ----Figure3.2,P103
PROCESSES AND SCHEDULING
Example --- Figure 3.3,P104
PROCESSES AND SCHEDULING
Two State Process Model(P104)
Two states
‫ ٭‬Running
‫ ٭‬Not-running
A new process enters into the not-running state.
A running process will be interrupted and a new
process will be selected to run.
PROCESSES AND SCHEDULING
Figure 3.4a,P105
PROCESSES AND SCHEDULING
Figure 3.4b
There is a single queue in which each entry(项)
is a pointer to a particular process.
PROCESSES AND SCHEDULING
Creation and Termination Processes (I)
 Creation Process: OS builds the data structures that are
used to manage the process and allocates address space to
the process.
 Reasons for Process Creation
‫ ٭‬New batch job
‫ ٭‬Interactive(交互) logon
‫ ٭‬Created by OS to provide a service example: printing
‫ ٭‬Spawned(产生) by existing process  process tree
PROCESSES AND SCHEDULING
Example of CreateProcess
Create()
{ struct pcb *p;
p=alloc_pcb(); //alloc a empty process control block
…… // initial p
p->state= not-running;
p->parent=current;
current->child=p;
enqueue(WAITQUEUE, p); // put p into wait queue
……
}
PROCESSES AND SCHEDULING
Creation and Termination Processes (II)
Reasons for Process Termination
‫ ٭‬Halt command
‫ ٭‬User log off
‫ ٭‬User program exit
‫ ٭‬Program error(e.g. no resource for use)
‫ ٭‬Normal completion
‫ ٭‬Time limit exceeded
‫ ٭‬Memory unavailable
PROCESSES AND SCHEDULING
Creation and Termination Processes (III)
Reasons for Process Termination
‫ ٭‬Bounds violation
‫ ٭‬Protection error
▪ Example write to read-only file
‫ ٭‬Arithmetic error
‫ ٭‬Time overrun
▪ Process waited longer than a specified maximum for
an event
PROCESSES AND SCHEDULING
Creation and Termination Processes (IV)
Reasons for Process Termination
‫ ٭‬I/O failure
‫ ٭‬Invalid instruction
▪ happens when try to execute data
‫ ٭‬Privileged instruction
‫ ٭‬Data misuse
‫ ٭‬Operating system intervention
▪ such as when deadlock occurs
‫ ٭‬Parent terminates so child processes terminate
‫ ٭‬Parent request
PROCESSES AND SCHEDULING
A Five State Model (p107)
Some processes in the not-running state are
ready to execute, while others are blocked,
waiting for an I/O operation to complete.
PROCESSES AND SCHEDULING
A Five State Model
Running: For single processor,only one process
can be in this state at a time.
Ready: A process that is prepared to execute
when given the opportunity.
Blocked: A process cannot execute until some
event occurs, such as the completion of an I/O
operation.
New: A process that has just been created, but
has not yet been admitted to the pool(池) of
executable processes.
Exit: A process has been release from the pool of
executable processes.
PROCESSES AND SCHEDULING
Five-State Process Model
PROCESSES AND SCHEDULING
State Transition for a Process (I)
 Null->new: A new process is created to execute a program
 New->ready: OS will move a process from new state to
ready state when it is prepared to take on an additional
process.
 Ready->running: OS chooses one of the processes in the
ready state to run.
 Running->Exit: Process has completed or aborts.
 Running->ready: The running process has reached the
maximum allowable time for uninterrupted execution, or
higher priority process reaches to preempt the CPU.
PROCESSES AND SCHEDULING
State Transition for a Process(II)
 Running->blocked: A process is put in the blocked state if it
requests something for which it must wait. E.g. a system
service or I/O operation.
 Blocked->ready: When the event for which the blocked
process has been waiting occurs.
 Ready->exit: In some systems, a parent may terminate a
child process at any time. Also, if a parent terminates, all
child processes of it may be terminated.
 Blocked->exit: Same as the Ready->exit.
PROCESSES AND SCHEDULING
PROCESSES AND SCHEDULING
Queuing Model of Figure 3.5
PROCESSES AND SCHEDULING
Queuing Model of Figure 3.5
PROCESSES AND SCHEDULING
Swapping(p111交换)
The processor is so much than I/O that it will
be common for all of the processes in memory
to be waiting for I/O.
Swapping: Moving part or all of a process
from main memory to disk.
PROCESSES AND SCHEDULING
Suspending(挂起)
When all of the processes in memory are blocked,
OS will suspend a process ,and swap it out to
disk to release the memory for other ready
process.
A new state may be introduced in the suspending
state
PROCESSES AND SCHEDULING
Process State Transition with Suspend States (p113)
PROCESSES AND SCHEDULING
Process State Transition with Suspend States
PROCESSES AND SCHEDULING
Reasons for Process Suspension (Table 3.4 p116)
Swapping: OS needs to release sufficient main
memory to bring in a process that is ready to
execute.
Other OS reason: OS may suspend a
background or utility process or a process that is
suspected of causing a problem.
Interactive user request: A user may wish to
suspend execution of a program for purposes of
debugging or waiting the use of a resource.
PROCESSES AND SCHEDULING
Reasons for Process Suspension
Timing: A process may be executed
periodically( 周 期 性 ) and may be suspended
while waiting for the next time interval.
Parent process request: A parent process may
wish to suspend execution of a descendent(子
孙 ) to examine or modify the suspended
process, or to coordinate the activity of various
descendents.
PROCESSES AND SCHEDULING
Characteristics of Suspending Process (p116)
 Is not immediately available for execution.
 The process may or may not be waiting on an event.
If it is, this blocked condition is independent of the
suspend condition, and occurrence of the blocking
event does not enable the process to be executed.
 The process was placed in a suspended state by an
agent: either itself, a parent process or OS.
 The process may not be removed from this state until
the agent explicitly orders the removal.
PROCESSES AND SCHEDULING
Suspending and Block
Problem:
 Only a blocked process can be suspended?
 How to active a suspend process?
PROCESSES AND SCHEDULING
Suspending and Block
 Two concepts
‫ ٭‬If a process is waiting a event, it may be
blocked.
‫ ٭‬If a process is swapped out to memory, it
is suspended.
PROCESSES AND SCHEDULING
Combination States
 Ready: The process is in main memory and available for
execute.
 Blocked: The process is in main memory and awaiting an
event.
 Blocked, suspend: The process is in secondary memory
and awaiting an event.
 Ready, suspend: The process is in secondary memory but
is available for execution as soon as it is loaded into main
memory.
PROCESSES AND SCHEDULING
States Transition with Suspend State (p116)
 Blocked->blocked,suspend: OS swap out a blocked
process to release memory.
 Blocked,suspend->ready,suspend: A process in the
blocked, suspend state is moved to the ready, suspend
state when the event for which it has been waiting occurs.
 Ready,suspend->ready: OS bring one in to continue
execution.
 Ready->ready,suspend: Sometime OS swap out a ready
process to release memory.
PROCESSES AND SCHEDULING
States Transition with Suspend State
 New->ready,suspend(new->ready): When a new process is
created, it can either be added to the ready queue or ready,
suspend queue. If there is no enough memory for a new
process, the new->ready,suspend transition happened.
 Blocked,suspend->blocked: If there is free memory, OS
may put the highest priority blocked, suspend process into
memory.
PROCESSES AND SCHEDULING
States Transition with Suspend State
 Running->ready,suspend: OS is preempting the process
because a higher priority process on the blocked,suspend
queue has just become unblocked, the OS could move the
running process directly to the (ready, suspend)queue
and free some main memory
 Various->exit: When a process be terminated by it’s
parent, the process can be in any state when be
terminated.
PROCESSES AND SCHEDULING
Thread
Why using thread
Thread entity
Thread states: ready、running、block
State transition
PROCESSES AND SCHEDULING
Thread vs. Process(Figure4.1,P147)
PROCESSES AND SCHEDULING
Why Using Thread
 To improve the concurrence of processes
‫ ٭‬Unit of resource ownership
▪ Always process before or after introduce thread
‫ ٭‬Unit of dispatching
▪ Before introducing thread, is process, after that, is
thread (p145)
‫ ٭‬A main thread is always associate with the process in
all its lifeline.
‫ ٭‬The context of a thread is smaller than that of a process,
and switching thread(线程切换) has small spending
than switching process.
‫ ٭‬The overhead of communication between two threads
in one process is little.
PROCESSES AND SCHEDULING
Thread Entity(p146)
 States: running, ready, etc.
 Thread context
‫ ٭‬Every thread has it’s execution point(执行点).
 An execution stack.
 Some per-thread static storage for local variables.
 Access to the memory and resources of its process,
share with all other threads in that process.
PROCESSES AND SCHEDULING
Single Thread and Multi-thread ( figure 4.2)
PROCESSES AND SCHEDULING
Multi-thread
Each process includes many threads,and it has
a private space and a PCB
Threads belongs to a process share the resource
of the process
‫ ٭‬E.g. When a thread open a file,the other
threads in the same process can also access
the file.
PROCESSES AND SCHEDULING
Examples of Multi-thread
Ftp server.
Ms-word and it’s spelling checking.
Asynchronous processing, e.g.: write disk
(backup).
When a thread is computing data, another thread
is reading the data.
PROCESSES AND SCHEDULING
The States of Thread(p150)
Ready
Running
Block
?Suspend
PROCESSES AND SCHEDULING
States Transition of Thread(p150)
 Spawn: The new thread is provide with its own
register context and stack space and placed on the
ready list.
 Block: When needs to wait for an event, ready-> block.
 Unblock: blockready
 Finish: Register context and stacks are deallocated
 The states of threads are independent
PROCESSES AND SCHEDULING
One Thread in RPC (Remote Procedure Call)
PROCESSES AND SCHEDULING
Multi-thread in RPC
PROCESSES AND SCHEDULING
Thread Synchronization(P152)
PROCESSES AND SCHEDULING
Types of Thread
 User-level Thread, ULT
‫ ٭‬All of the work of thread management is done by
the application and the kernel is not aware of the
existence of threads. (figure4.6a)
 Kernel-level Thread, KLT
‫ ٭‬All of the work of thread management is done by
the kernel.
 Time alloc: ULT vs. KLT
PROCESSES AND SCHEDULING
Types of Thread (P153)
PROCESSES AND SCHEDULING
Process vs. Thread(P145~)
Dispatch unit and resource allocated unit
‫ ٭‬process: resource allocated unit
‫ ٭‬thread: dispatch unit
Concurrence
System spending(系统开销)
Examples:
‫ ٭‬pthread_create(&tid,NULL,my_thread, 0)
PROCESSES AND SCHEDULING
Adv. of ULT instead of KLT
Saves the overhead(负荷) of two mode
switches( user to kernel; kernel back to user).
Scheduling can be application specific.
ULT can run on any OS. (The threads library is a
set of application-level utilities shared by all
applications).
PROCESSES AND SCHEDULING
Disadv. of ULT vs. KLT
When a thread executes a system call, all of the
threads within the process are blocked.
Can not take advantage of multiprocessing.
PROCESSES AND SCHEDULING
PROCESSES AND SCHEDULING
Process Description (p117)
How does OS perceive(感知) the process、
control process ?
PROCESSES AND SCHEDULING
System State---Processes and Resources
PROCESSES AND SCHEDULING
Operating System Control Structure
Use to record the state of every process and
resource
‫ ٭‬Memory table
‫ ٭‬I/O table
‫ ٭‬File table
‫ ٭‬Process table
PROCESSES AND SCHEDULING
Memory Table
The allocation of main memory to processes.
The allocation of secondary memory to
processes.
Any protection attributes of segments of main
or virtual memory, such as which processes
may access certain share memory regions.
Any information needed to manage virtual
memory.
PROCESSES AND SCHEDULING
I/O Table
Record the state of a device.
Record I/O operation states.
Record the buffer address of the I/O device.
PROCESSES AND SCHEDULING
File Table
Record the file state and attribute.
Record the address of the file.
PROCESSES AND SCHEDULING
Process Table
Record the location of a process.
Record the attribute of a process.
‫ ٭‬Process ID
‫ ٭‬Process state
‫ ٭‬Address of a process
‫ ٭‬Resource owned by a process
PROCESSES AND SCHEDULING
PROCESSES AND SCHEDULING
Control Structure of a Process (p120)
 Process image: collection of program,data,stack and
attributes
 Typical elements of a process image (table3.5,P120)
‫ ٭‬User data: The modifiable part of the user space.
‫ ٭‬User program: The program to be executed.
‫ ٭‬System stack: store parameters and calling address
for procedure and system calls.
‫ ٭‬Process Control Block: Data needed by the operating
system to control the process.
PROCESSES AND SCHEDULING
PCB
The basic data structure of OS to management the
processes.
A PCB can identify a process.
Information of PCB
‫ ٭‬Process identification
‫ ٭‬Processor state information
‫ ٭‬Process control information
PROCESSES AND SCHEDULING
Typical Elements of a PCB(Table3.6)
Identifiers:
‫ ٭‬Identifier of this process
‫ ٭‬Identifier of parent process
‫ ٭‬User identifier
Processor State information:
‫ ٭‬User-visible registers
‫ ٭‬Control and status registers
‫ ٭‬Stack pointers
PROCESSES AND SCHEDULING
EXP. of Control and Status Registers
PROCESSES AND SCHEDULING
Typical Elements of a PCB
 Process control information
‫ ٭‬Scheduling and state information
▪ Process state
▪ Priority
▪ Scheduling-related
information:
depend
on
algorithm
▪ event
‫ ٭‬Data Structuring: PCB-list
‫ ٭‬Inter-process Communication:signal,message etc…
‫ ٭‬Process Privileges:
‫ ٭‬Memory Management: Segment、Page
‫ ٭‬Resource Ownership and Utilization:etc. files the
process has opened
PROCESSES AND SCHEDULING
User Processes in Virtual Memory(F3.11)
PROCESSES AND SCHEDULING
PCB List (F3.6a is Presented as F3.12)
PROCESSES AND SCHEDULING
OS Kernel (Supplement)
 Process management:
‫ ٭‬Create and destroy process
‫ ٭‬Process scheduling
‫ ٭‬Process state transition
‫ ٭‬Process synchronization and communication
‫ ٭‬Maintain PCB
 Memory management:
‫ ٭‬Allocate space for process 、 swapping 、 segment/page
management
 I/O management:
‫ ٭‬Buffer management、allocate device and channel
PROCESSES AND SCHEDULING
OS Kernel
Interrupt handling
Timing
Primitive: Atomic Operation
Accounting
Monitoring
PROCESSES AND SCHEDULING
Execution Mode
User Mode
‫ ٭‬Low privilege
‫ ٭‬User programs always run at this mode
System/Control/Kernel Mode
‫ ٭‬High privilege
‫ ٭‬System code run at this mode
PROCESSES AND SCHEDULING
Process Switch vs. Mode Switch (P129)
 Process Switch: A running process is interrupted, and
the operating system assigns another process to the
running state, and turns control over to that process.
 Mode Switch: When the code in process image call the
service of kernel subsystem, mode switch occurs.
 In most OS, the occurrence of an interrupt does not
necessarily mean a process switch, and in that case,
the context saving and subsequent restoral involve
little overhead.
PROCESSES AND SCHEDULING
Mechanisms for Interrupting the Execution of a Process(T3.9)
 Interrupt
‫ ٭‬External to the execution of the current instruction.
Not must be process switch.
‫ ٭‬Exp. I/O interrupt, the process who waits the I/O
change from block to ready. Is the process switch
occurrence?
 Trap
‫ ٭‬Associated with the execution of the current instruction.
Handling of an error or an exceptional condition.
 Supervisor call
‫ ٭‬Call to an operating system function.
PROCESSES AND SCHEDULING
Mode Switch
Interrupt:
‫ ٭‬Saves the context of the current program
being executed.
‫ ٭‬Sets the program counter to the starting
address of an interrupt handler program.
‫ ٭‬Switches from user mode to kernel mode so
that the interrupt processing code may
include privileged instructions.
PROCESSES AND SCHEDULING
Execution of the OS
Execute the kernel of the OS outside of any
process(F3.13a). The OS has its own space and
stack.
Execute virtually all OS in the context of a user
process(minicomputers). F3.13b F3.14
Process-based OS: F3.13c.Implement the OS as
a collection of system processes
PROCESSES AND SCHEDULING
Process Switch
 Save the context of the processor, include program counter
and other registers.
 Update the PCB.
 Move PCB to the appropriate queue.
 Select another process for execution.
 Update the PCB of the selected process.
 Update the memory-management data structures.
 Restore the context of the processor to the selected process.
PROCESSES AND SCHEDULING
Process Control (P127)
Process Switch
Create and Terminate
Block and Wakeup
Suspend and Activate
PROCESSES AND SCHEDULING
Process Creation (p128)
 Assign a unique process identifier to the new process,
and a new entry is added to the primary process table.
 Allocate space for the process:user space、stack、PCB,
and linkages must be set up for the appropriate linkages.
 PCB be initialized:process id、CPU states、process
states…
 Set up linkages: Maintains each scheduling queues
 Create other data structures
PROCESSES AND SCHEDULING
Destroy process
 Check the stats of the process
 If running, terminates it and schedules a new
process
 If has child process, terminates child process
 Give back the resources to parent process or
system
 Remove the PCB for the PCB list
PROCESSES AND SCHEDULING
Block and Wakeup
Reasons for block
‫ ٭‬Requests a system service
‫ ٭‬Starts a I/O operation
‫ ٭‬Waits a new data to reach
‫ ٭‬No work to do
PROCESSES AND SCHEDULING
Block and Wakeup
 block()
‫ ٭‬When a block event occurs, the process block itself
and enters into block state, and be inserted into the
block queues.
 wakeup()
‫ ٭‬The process be waked up is inserted into ready
queue and becomes a ready state process
PROCESSES AND SCHEDULING
Suspend and Active
 suspend()
‫ ٭‬Swapping the process from memory to secondary
memory, ready->ready,suspend; block->block,suspend,
and inserts the process into the correspondent queue.
 active()
‫ ٭‬Swapping the process from secondary memory to
memory, (ready, suspend)->ready、 (block,suspend)
-> block, and inserts the process into the correspondent
queue.
PROCESSES AND SCHEDULING
Exp.
UNIX process management
P135~P140
Homework:3.3 3.4 3.8
PROCESSES AND SCHEDULING
Download