chapter 2
Skip this Video
Download Presentation
Chapter 2

Loading in 2 Seconds...

play fullscreen
1 / 90

Chapter 2 - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 2' - maire

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 2

Chapter 2

Processes and Scheduling


  • Foundation:Process Description and Control
  • Implement: Mutual Exclusion and Synchronization
  • Avoidance: Deadlock(死锁) and Hungry(饥饿)
  • Solution: Several Classical(经典) Problems
  • About: Process Communications
  • Policy: Process scheduling
process description and control p101
Process Description and Control (p101)
  • Concurrence (vs.parallel) execution of processes (supplement(补充))
  • Process states and transition
  • Process description(PCB)
  • Process control(Primitive/Atomic)
serial execution of program
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) = {Φ}

some problems for process management p101
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
process concept p60
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
process characteristics supplement
Process Characteristics(Supplement)
  • Dynamic: create->dispatch->request I/O-> redispatch(重分派)->exit
  • Concurrence:
  • Independence:Each process does not feel the existence of other processes.
  • Discontinuity:
architecture of process p120
Architecture of Process(p120)
  • User data and program
  • System stack
  • PCB,Process Control Block
process states
Process States
  • Concurrence execution of processes
  • States of processes(2-states、5-states)
example of concurrence execution p102
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.
two state process model p104
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.
figure 3 4b
Figure 3.4b
  • There is a single queue in which each entry(项)

is a pointer to a particular process.

creation and termination processes i
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
example of createprocess
Example of CreateProcess


{ struct pcb *p;

p=alloc_pcb(); //alloc a empty process control block

…… // initial p

p->state= not-running;



enqueue(WAITQUEUE, p); // put p into wait queue



creation and termination processes ii
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
creation and termination processes iii
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
creation and termination processes iv
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
a five state model p107
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.
a five state model
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.
state transition for a process i
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.
state transition for a process ii
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.
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.
  • 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
reasons for process suspension table 3 4 p116
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.
reasons for process suspension
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.
characteristics of suspending process p116
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.
suspending and block
Suspending and Block


  • Only a blocked process can be suspended?
  • How to active a suspend process?
suspending and block1
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.
combination states
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.
states transition with suspend state p116
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.
states transition with suspend state
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.
states transition with suspend state1
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.
  • Why using thread
  • Thread entity
  • Thread states: ready、running、block
  • State transition
why using thread
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.
thread entity p146
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.
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.
examples of multi thread
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.
the states of thread p150
The States of Thread(p150)
  • Ready
  • Running
  • Block
  • ?Suspend
states transition of thread p150
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
types of thread
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
process vs thread p145
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)
adv of ult instead of klt
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).
disadv of ult vs klt
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.
process description p117
Process Description (p117)
  • How does OS perceive(感知) the process、control process ?
operating system control structure
Operating System Control Structure
  • Use to record the state of every process and resource
    • Memory table
    • I/O table
    • File table
    • Process table
memory table
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.
i o table
I/O Table
  • Record the state of a device.
  • Record I/O operation states.
  • Record the buffer address of the I/O device.
file table
File Table
  • Record the file state and attribute.
  • Record the address of the file.
process table
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
control structure of a process p120
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.
  • 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
typical elements of a pcb table3 6
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
typical elements of a pcb
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
os kernel supplement
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
os kernel
OS Kernel
  • Interrupt handling
  • Timing
  • Primitive: Atomic Operation
  • Accounting
  • Monitoring
execution mode
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
process switch vs mode switch p129
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.
mechanisms for interrupting the execution of a process t3 9
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.
mode switch
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.
execution of the os
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
process switch
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.
process control p127
Process Control (P127)
  • Process Switch
  • Create and Terminate
  • Block and Wakeup
  • Suspend and Activate
process creation p128
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
destroy process
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
block and wakeup
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
block and wakeup1
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
suspend and active
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.
  • UNIX process management


Homework:3.3 3.4 3.8