Chapter 3 1 Process Description and Control Chapter 3.

53
Chapter 3 1 Process Description and Process Description and Control Control Chapter 3 Chapter 3

Transcript of Chapter 3 1 Process Description and Control Chapter 3.

Page 1: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 31

Process Description and ControlProcess Description and Control

Chapter 3Chapter 3

Page 2: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 32

Processes - TasksProcesses - Tasks

Recall: a process is a program in execution why must we distinguish between process and

program? because a single program can be executed by

several processes... The word process sometimes applies also to a

program that is capable of executing independently and simultaneously with others

Task is an equivalent term

Page 3: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 33

OS Requirements for ProcessesOS Requirements for Processes

OS must interleave the execution of several processes to maximize CPU usage while providing reasonable response time

OS must allocate resources to processes while avoiding deadlock and starvation

OS must support inter process communication and user creation of processes

Page 4: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 34

Dispatcher Dispatcher (short-term scheduler, CPU scheduler)(short-term scheduler, CPU scheduler)

Is an OS program that assigns the CPU to one process or to another

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

The CPU will execute instructions from the dispatcher to switch from process A to process B

Page 5: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 35

When does a process get created?When does a process get created?

Submission of a job User logs on Created by OS to provide a service to a

user (ex: printing a file) Spawned by an existing process

a user program can dictate the creation of a number of processes

Page 6: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 36

When does a process get terminated?When does a process get terminated?

User logs off

Process executes a system call to terminate

Error or fault conditions, timeout

Page 7: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 37

Reasons for Process TerminationReasons for Process Termination

Normal completion Time limit exceeded Memory unavailable Memory bounds violation Protection error

example: write to read-only file Arithmetic error Time overrun

process waited longer than a specified maximum for an event

Page 8: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 38

Reasons for Process TerminationReasons for Process Termination

I/O failure Invalid instruction

happens when try to execute data Privileged instruction Operating system intervention

such as when deadlock occurs Parent request to terminate one offspring Parent terminates so child processes

terminate

Page 9: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 39

Process StatesProcess States

Let us start with these states: The Running state

The process(es) that is (are) executing on the CPU are in Running state

The Blocked state A process that is waiting for something (e.g. I/O) to

complete is in Blocked state The Ready state

A process that is ready to be executed is in Ready state [due to limited number of CPUs]

Page 10: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 310

Other Useful StatesOther Useful States

The New state OS has performed the necessary actions to

create the process has created a process identifier has created tables needed to manage the

process but has not yet committed to execute the

process (not yet admitted) because resources are limited

Long-term scheduler takes this decision

Page 11: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 311

Other Useful StatesOther Useful States

The Exit state Termination moves the process to this state It is no longer eligible for execution Tables and other info are temporarily

preserved for auxiliary program Ex: accounting program that cumulates resource

usage for billing the users The process (and its tables) gets deleted

when the data is no longer needed

Page 12: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 312

A Five-state Process ModelA Five-state Process Model

Page 13: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 313

Process TransitionsProcess Transitions

New -> Admit: long-term scheduler decides that the process

can be executed

Page 14: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 314

Process TransitionsProcess Transitions

Ready --> Running The dispatcher selects a new process to run (to

be discussed in Chapter 4). Running --> Ready

the running process is timed out the running process gets interrupted because a

higher priority process is in the ready state (needs to run)

Page 15: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 315

Process TransitionsProcess Transitions

Running --> Blocked When a process requests something for which

it must wait a service of the OS that requires a wait an access to a resource not yet available initiates I/O and must wait for the result waiting for a process to provide input (IPC)

Blocked --> Ready When the event for which it was waiting occurs

Page 16: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 316

Process TransitionsProcess Transitions

Release: we discussed the reasons earlier

Page 17: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 317

A Queuing DisciplineA Queuing Discipline

Queue Ready: processes waiting to get executed on CPU When event n occurs, the process that was waiting for it is

moved into the Ready queue

Page 18: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 318

Variations the basic modelVariations the basic model

The basic model we have discussed is very widely used.

It has a great number of variations, since no two OS are alike

An example follows: adding swapping states

Page 19: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 319

The need for swappingThe need for swapping

So far, all the processes had to be (at least partly) in main memory

Even with virtual memory, keeping too many processes in main memory will deteriorate the system’s performance ex: coexistence of several processes that do not use enough

the CPU (e.g. are all blocked) The OS may need to suspend some processes to bring in

others, ie: to swap them out to auxiliary memory (disk). We add 2 new states, which double Blocked and Ready: Blocked Suspend: blocked processes which have been

swapped out to disk Ready Suspend: ready processes which have been swapped

out to disk

Page 20: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 320

New state transitions (New state transitions (medium-term schedulingmedium-term scheduling))

Blocked --> Blocked Suspend When all processes are blocked, the OS may remove a

blocked process to bring a ready process in memory Blocked Suspend --> Ready Suspend

When the event for which process has been waiting occurs Ready Suspend --> Ready

When no ready processes in main memory Normally, this transition is paired with Blocked --> Blocked

suspend for another process (memory space must be found) Ready--> Ready Suspend

When there are no blocked processes and must free memory for adequate performance

Page 21: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 321

A Seven-state Process ModelA Seven-state Process Model

Page 22: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 322

Operating System Control StructuresOperating System Control Structures

An OS maintains the following tables for managing processes and resources: Memory occupation tables (see later) I/O tables (see later) File tables (see later) Process tables (this chapter)

See detailed discussion in texbook

Page 23: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 323

Page 24: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 324

Process ImageProcess Image

The information that the OS requires in order to manage the process User program User data System and user stack(s)

for calls, returns and parameter passing Process Control Block (execution context)

Data needed (process attributes) by the OS to suspend/resume the process. This includes:

• Process identification information• Processor state information• Process control information

Page 25: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 325

Process imagesProcess images

Page 26: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 326

Location of the Process ImageLocation of the Process Image

The location of each process image is pointed to by an entry in the Primary Process Table

Page 27: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 327

Page 28: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 328

What needs to be saved and when What needs to be saved and when

Mode switching: process is suspended (e.g. to process interrupt) but immediately after this it resumes execution on the same CPU: only information necessary to resume

execution of the same process needs to be saved (e.g. program counter)

Process switching: process is suspended and another process will take the CPU: more may need to be saved: context, data... also process state must be updated.

Page 29: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 329

Process Identification (in the PCB)Process Identification (in the PCB)

A few numeric identifiers may be used Unique process identifier (always) User identifier

the user who is responsible for the process Identifier of the process that created this

process

Page 30: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 330

Processor State Information (in PCB)Processor State Information (in PCB)

Contents of processor registers User-visible registers Control and status registers: program counter... Stack pointers

Program status word (PSW) contains status information Example: the EFLAGS register on Pentium

machines

Page 31: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 331

Process Control Information (in PCB)Process Control Information (in PCB)

scheduling and state information Process state (ie: running, ready, blocked...) Priority of the process Event for which the process is waiting (if

blocked) data structuring information

may hold pointers to other PCBs for process queues, parent-child relationships and other structures

Page 32: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 332

Queues as linked lists of PCBsQueues as linked lists of PCBs

Page 33: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 333

Process Control Information (in PCB)Process Control Information (in PCB)

interprocess communication may hold flags and signals for IPC

process privileges Ex: access to certain memory locations...

memory management pointers to segment/page tables assigned to

this process resource ownership and utilization

resource in use: open files, I/O devices... history of usage (of CPU time, I/O...)

Page 34: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 334

Modes of ExecutionModes of Execution

To provide protection to PCBs (and other OS data) most processors support at least 2 execution modes: Privileged mode (a.k.a. system mode, kernel

mode, supervisor mode, control mode, master mode... )

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

User mode (a.k.a. slave mode) For this the CPU provides a (or a few) mode

bit which may only be set by an interrupt or trap or OS call

Page 35: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 335

Process CreationProcess Creation

Assign a unique process identifier Allocate space for the process image Initialize process control block

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

Set up appropriate linkages Ex: add new process to linked list used for the

scheduling queue Establish or enlarge some data structures

accounting info

Page 36: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 336

When to Switch a Process ?When to Switch a Process ?

A process switch may occur whenever the OS has gained control of CPU. ie when: Supervisor Call

explicit request by the program (ex: I/O). The process will probably be blocked

Trap An error resulted from the last instruction. It may

cause the process to be moved to exit state Interrupt

the cause is external to the execution of the current instruction. Control is transferred to IH

Page 37: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 337

Examples of interruptionsExamples of interruptions

Timers process has used up its time - transfer to ready

state I/O completion

transfer process to ready state CPU scheduler will decide when to resume it

Memory fault, page fault In virtual memory systems, process requests a

page that is not in memory block process, read page in memory

Page 38: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 338

Mode SwitchingMode Switching

It is possible that an interrupt does not produce a process switch

Control can immediately return to the interrupted program after executing the IH

Then only the processor state information needs to be saved (ref. Chap 1)

This is called mode switching (user to kernel mode when going into IH)

Less overhead: no need to use the PCB as for process switching

Page 39: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 339

Steps in Process (Context) SwitchingSteps in Process (Context) Switching

Save context of CPU including program counter and other registers

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

Move PCB to appropriate queue - ready, blocked

Select another process for execution Update PCB of the selected process Restore CPU context from PCB of the

selected process

Page 40: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 340

Simple Interrupt ProcessingSimple Interrupt Processing

Save Process Control Block

Restore Process Control Block

Page 41: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 341

Execution of the Operating SystemExecution of the Operating System

Up to now, by process we were referring to “user process”

If the OS is just like any other collection of programs, is the OS a process?

If so, how it is controlled?

The answer depends on the OS design.

Page 42: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 342

Nonprocess Kernel (old)Nonprocess Kernel (old)

The concept of process applies only to user programs

OS code is executed as a separate entity in privilege mode

OS code never gets executed within a process

Page 43: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 343

Execution within User ProcessesExecution within User Processes

OS operates mostly as it was a regular user, in fact OS function appear as functions of the users that invoke them: shared address space

Virtually all OS code gets executed within the context of a user process On Interrupts, Traps, System calls: CPU switches to kernel mode to

execute OS routine within the context of user process (mode switch) Control passes to process switching functions (outside processes) only

when needed (eg: interprocess synchronization, process scheduling…)

Page 44: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 344

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 45: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 345

Process-based Operating SystemProcess-based Operating System

The OS is a collection of system processes, outside of user’s address space

Major kernel functions are separate processes Process switching functions are executed outside

of any process

Page 46: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 346

Important concepts of Chapter 3Important concepts of Chapter 3

Process State Waiting queues of professes Process Image Process Control Block Process switching Mode switching Kernel and distribution of functionalities

between users and kernel

Page 47: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 347

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 processes

run in user mode for user programs run in kernel modes for system calls, traps, and

interrupts

Page 48: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 348

UNIX SVR4 Process StatesUNIX SVR4 Process States

Similar to our 7 state model 2 running states: User and Kernel

transitions 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 1 queue)

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

Page 49: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 349

UNIX Process State DiagramUNIX Process State Diagram

Page 50: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 350

UNIX Process CreationUNIX Process Creation

Every process, except process 0, is created by the fork() system call fork() allocates entry in 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 51: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 351

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 52: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 352

UNIX Process ImageUNIX Process Image

User-level context Process Text (ie: 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 53: Chapter 3 1 Process Description and Control Chapter 3.

Chapter 353

UNIX Process ImageUNIX Process Image

System-level context Process table entry

the actual entry concerning 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) area additional 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)