Chapter 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 90

Chapter 2 PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Chapter 2

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.

Example figure3 2 p103

Example ----Figure3.2,P103

Example figure 3 3 p104

Example --- Figure 3.3,P104

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 4a p105

Figure 3.4a,P105

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.

Chapter 2

Five-State Process Model

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.

Chapter 2

Queuing Model of Figure 3.5

Queuing model of figure 3 5

Queuing Model of Figure 3.5

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

Process state transition with suspend states p113

Process State Transition with Suspend States (p113)

Process state transition with suspend states

Process State Transition with Suspend States

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

Thread vs process figure4 1 p147

Thread vs. Process(Figure4.1,P147)

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.

Single thread and multi thread figure 4 2

Single Thread and Multi-thread ( figure 4.2)

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

One thread in rpc remote procedure call

One Thread in RPC (Remote Procedure Call)

Multi thread in rpc

Multi-thread in RPC

Thread synchronization p152

Thread Synchronization(P152)

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

Types of thread p153

Types of Thread (P153)

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 ?

System state processes and resources

System State---Processes and Resources

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.

Chapter 2


  • 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

Exp of control and status registers

EXP. of Control and Status Registers

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

User processes in virtual memory f3 11

User Processes in Virtual Memory(F3.11)

Pcb list f3 6a is presented as f3 12

PCB List (F3.6a is Presented as F3.12)

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.

Chapter 2


  • UNIX process management


    Homework:3.3 3.4 3.8

  • Login