Top Banner
1 Process Description and Control Chapter 3 Chapter 3 Why process? Why process? What is a process? What is a process? How to represent processes? How to represent processes? How to control processes? How to control processes?
40

1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

Dec 21, 2015

Download

Documents

Alannah Doyle
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

1

Process Description and Control

Chapter 3Chapter 3

Why process? Why process? What is a process?What is a process? How to represent processes?How to represent processes? How to control processes?How to control processes?

Page 2: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

2

Processes vs. ProgramsProcesses vs. Programs

Dynamic vs. StaticA process is a dynamic execution of instructions from one or more programs

A program is a static list of instructions and data

Processes own resources (CPU, memory, files, I/O devices, etc.)

Many-to-many relationshipMultiple executions of one program create multiple processes

One process may contain instructions from more than one program

Page 3: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

3

OS Requirements for ProcessesOS Requirements for Processes

Manage processesCreate/destroy processes

Maintain process information

Allocate/release resources

Interleave the execution of processes

Schedule processes

Support inter-process communication (IPC)

Page 4: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

4

A Five-State Process ModelA Five-State Process Model

Page 5: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

5

Page 6: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

6

Dispatcher (short-term scheduler)Dispatcher (short-term scheduler)

An OS program that moves the processor from one process to another

It prevents a single process from monopolizing processor time

It decides who goes next according to a scheduling algorithm (Chapter 9)

The CPU will always execute instructions from the dispatcher while switching from process A to process B

Page 7: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

7

State TransitionsState Transitions

Page 8: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

8

Managing Process ExecutionManaging Process Execution

Two queues: ready queue and blocked queue

When an event occurs, the OS scans the entire blocked queue, searching for those processes waiting on that event

This could be slow when the blocked queue is long

Page 9: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

9

Multiple Blocked QueuesMultiple Blocked Queues

Ready queue without priorities (ex: FIFO)When event n occurs, the corresponding queue is moved into the ready queue

Page 10: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

10

A Seven-state Process ModelA Seven-state Process Model

Page 11: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

11

New state transitionsNew state transitions

Blocked blocked suspendWhen all processes are blocked, the OS will make room to bring a ready process in memory

Blocked suspend ready suspendWhen the event for which it has been waiting occurs

Ready suspend readyWhen there are no more ready processes in main memory

Ready ready suspend (unlikely)When there are no blocked processes and must free memory for adequate performance

Page 12: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

12

Summary of What a Process isSummary of What a Process is

A process is an execution of a program

A process has five basic states:New, ready, running, blocked, and exit

A process can be suspended by the OSTwo new process states Ready, Suspend and Blocked, Suspend are introduced

The process execution can be managed through different queues (ready queues and blocked queues)

Questions?

Page 13: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

13

Process images in virtual memoryProcess images in virtual memory

Each process image is in virtual memory

May be big or small depending on the size of the program

May not occupy a contiguous range of addresses (depends on the memory management scheme used)

Process Image

Page 14: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

14

Process images in virtual memoryProcess images in virtual memory

Process Image

Page 15: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

15

Process List StructuresProcess List Structures

Page 16: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

16

Summary of Process RepresentationSummary of Process Representation

A process consists of a process image, resources, and a process control block (PCB)

A process control block (PCB) consists of:Process Identification

Process id, user id, …

Processor State InformationCPU registers, program status word

Process Control InformationProcess state, priority, waiting events, IPC information, memory information, I/O resources, …

Questions?

Page 17: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

17

How to Control ProcessesHow to Control Processes

Process creation

Process protection (modes of execution)

Mode switching

Process switching

Relationship between OS and user processes

Page 18: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

18

Process CreationProcess Creation

Allocate a PCB entry in the process table

Allocate space for the process image, load the process image into the allocated space

Initialize process control blockAssign a unique process identifier

Many default values (ex: state is New, no I/O devices or files...)

Put the PCB into a queue according to its state

Page 19: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

19

Modes of ExecutionModes of ExecutionTo provide protection to OS data, most processors support at least 2 execution modes:

Kernel mode (a.k.a. system mode, privileged mode)

manipulating control registers, primitive I/O instructions, memory management, …

User mode

For this the CPU provides a mode bit (or a few mode bits) which may only be set by an interrupt or trap or OS call

Page 20: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

20

When to Switch to Kernel Mode?When to Switch to Kernel Mode?

Supervisor Callexplicit request by the program (ex: file open)

TrapAn error resulted from the last instruction. It may cause the process to be moved to the Exit state

Interrupt The cause is external to the execution of the current instruction. Control is transferred to interrupt handler

Page 21: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

21

Examples of interrupt processingExamples of interrupt processingClock: happens periodically

process has expired its time slice and is transferred to the ready state

I/O: happens when an I/O operation finishesfirst move the processes that were waiting for this event to the ready (or ready suspend) state

then resume the running process or choose a process of higher priority

Memory fault: happens when referenced address is not in physical memory

OS must bring corresponding block into main memory

thus move this process to a blocked state (waiting for the I/O to complete)

Page 22: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

22

Mode SwitchingMode SwitchingWhen an interrupt or trap or OS call occurs, the processor mode moves from user mode to kernel modeWhen the OS finishes processing the interrupt or trap or OS call, the processor mode moves from kernel mode to user modeThese are called mode switching Only the processor state information needs to be saved on stackLess overhead than process switching: no need to update the PCB

Page 23: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

23

Process SwitchingProcess Switching

Is also called context switching

Performed by the OS dispatcher

May happen when a mode switch happensIf the process switch doesn’t happen on a mode switch, the running user process will continue to run after the processor mode changes back to user mode

Page 24: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

24

Steps in Process SwitchingSteps in Process Switching

Save context of processor, including program counter and other registers

Update the PCB of the running process with its new state and other associated info

Move PCB to appropriate queue - ready, blocked

Select another process for execution

Update PCB of the selected process

Restore CPU context from that of the selected process

Page 25: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

25

Relationship Between Relationship Between OS and User ProcessesOS and User Processes

The dispatcher of the OS is executed outside of any process

How about the other parts of the OS?Are they processes? or

Are they also outside of any user process?

The answer depends on the OS design

Page 26: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

26

Non-process KernelNon-process Kernel

The concept of process applies only to user programsOS code is executed as a separate entity in privileged modeOS code never gets executed within a process

Page 27: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

27

Execution within User ProcessesExecution within User Processes

Virtually all OS code gets executed within the context of a user process

On interrupts, traps, system calls: the CPU switches to kernel mode to execute the OS routine within the context of the user process (mode switch)

Control passes to dispatcher (outside processes) only when needed

Page 28: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

28

Execution within User ProcessesExecution within User Processes

OS code and data are in the shared address space and are shared by all user processes

Separate kernel stack for calls/returns when the process is in kernel mode

Within a user process, both user and OS programs may execute (more than 1)

Page 29: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

29

Process-based Operating SystemProcess-based Operating System

The OS is a collection of system processes

major kernel functions are separate processes

small amount of process switching functions is executed outside of any process

This is a design that easily accommodates multiprocessors

Page 30: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

30

Comparing the Three OS DesignsComparing the Three OS Designs

No# of kernel processes

Process switching

Process-based-kernel

Yes# of user

processesMode switching

Within-user-process-kernel

No1Process switching

Non-process-kernel

Limit the size of user processes

# of kernel stacks

Cost of OS call

Page 31: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

31

Summary of How to Control ProcessesSummary of How to Control Processes

Most work in process creation is filling the PCBThe processes can be protected from different modes supported by the CPUMode switching happens when the execution changes between user processes and the OSOS controls the process switching when it gains control of CPU through interrupt or trapOS may execute outside of all user processes, in user processes, or as separate processes depending on the OS design Questions?

Page 32: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

32

Case Study:Case Study:

UNIX SVR4 Process ManagementUNIX SVR4 Process Management

System process and user process

Process creation

Process states

Page 33: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

33

UNIX SVR4 Process managementUNIX SVR4 Process management

Most of OS executes within user processes

Uses two categories of processes:System processes

run in kernel mode for housekeeping functions (memory allocation, process swapping...)

User processesrun in user mode for user programs

run in kernel modes for system calls, traps, and interrupts

Page 34: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

34

UNIX SVR4 Process StatesUNIX SVR4 Process States

Similar to our 7 state model

2 running states: User and Kerneltransitions to other states (blocked, ready) must come from kernel running

Sleeping states (in memory, or swapped) correspond to our blocking states

A preempted state is distinguished from the ready state (but they form one queue)

Preemption can occur only when a process is about to move from kernel to user mode

Page 35: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

35

UNIX Process State DiagramUNIX Process State Diagram

Page 36: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

36

UNIX Process CreationUNIX Process Creation

Every process, except process 0, is created by the fork() system call

fork() allocates an entry in the process table and assigns a unique PID to the child process

child gets a copy of process image of parent: both child and parent are executing the same code following fork()

but fork() returns the PID of the child to the parent process and returns 0 to the child process

Page 37: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

37

UNIX System ProcessesUNIX System Processes

Process 0 is created at boot time and becomes the “swapper” after forking process 1 (the INIT process)

When a user logs in: process 1 creates a process for that user

Page 38: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

38

UNIX Process ImageUNIX Process Image

User-level contextProcess Text (i.e. code: read-only)

Process Data

User Stack (calls/returns in user mode)

Shared memory (for IPC)only one physical copy exists but, with virtual memory, it appears as it is in the process’s address space

Register context

Page 39: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

39

UNIX Process ImageUNIX Process ImageSystem-level context

Process table entrythe actual entry for this process in the Process Table maintained by OS

Process state, UID, PID, priority, event awaiting, signals sent, pointers to memory holding text, data…

U (user) areaadditional process info needed by the kernel when executing in the context of this process

effective UID, timers, limit fields, files in use, …

Kernel stack (calls/returns in kernel mode)Per Process Region Table (used by memory manager)

Page 40: 1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?

40

Summary of Summary of Process Description and ControlProcess Description and Control

A process is an execution of a program

A seven-state process model can describe the life time of a process

The PCB is an important data structure to represent and manage processes

Process switching

Relationship between OS and user processes

UNIX SVR4

Questions?