1 process process creation/termination context process control block (PCB) context switch ...

17
1 process process creation/termination context process control block (PCB) context switch 5-state process model process scheduling short/medium/long term Unix process model Lecture 4: Process Management

Transcript of 1 process process creation/termination context process control block (PCB) context switch ...

Page 1: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

1

process process creation/termination context process control block (PCB) context switch 5-state process model process scheduling

• short/medium/long term Unix process model

Lecture 4: Process Management

Page 2: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

2

process (also called task, or job) is a program in execution

process

• program code (or text) sequence of instructions to be executed note: multiple processes may be running the

same code (editor, web-browser, etc)

• context (execution state)

Process

prompt% ps -eflUID PID PPID C STIME TTY TIME COMMANDroot 0 0 0 Jan 19 ? 10:53 swapperroot 1 0 0 Jan 19 ? 0:17 initroot 2 0 0 Jan 19 ? 7:39 telnetd…mikhail 1546 1321 0 15:38:45 pts/17 3:31 netscape

Page 3: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

3

Unix Process Creation

one process can create another process, perhaps to do some work for it

• child – the new process an (almost) identical copy of parent (same code, same data, etc.)

• parent – the original process parent can either wait for the child to complete, or continue

executing in parallel (concurrently) with the child parent does not terminate until the child does

In UNIX, a process creates a child process using the system call fork( )

• In child process, fork() returns 0

• In parent process, fork() returns process id of new child

• child often uses exec( ) to start another completely different program

Page 4: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

4

Example of Unix Process Creation

#include <sys/types.h>#include <stdio.h>int a = 6; /* global (external) variable */int main(void) { int b; /* local variable */ pid_t pid; /* process id */ b = 88; printf("..before fork\n"); pid = fork(); if (pid == 0) { /* child */ a++; b++; } else /* parent */ wait(pid);

printf("..after fork, a = %d, b = %d\n", a, b); exit(0);}

prompt% forkprogram..before fork..after fork, a = 7, b = 89..after fork, a = 6, b = 88

example execution

Page 5: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

5

Unix Process Tree

as one processes creates another, they form a tree

Page 6: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

6

Process Termination

after completing work a process may execute exit() system call – asking the OS to delete it

• parent is notified

• process’ resources are deallocated by operating system parent may terminate execution of a child process - abort()

system call

• possible reasons task assigned to child is no longer required child exceeded allocated resources

• if parent exits (some) OS require all children to terminate – cascading termination

Page 7: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

7

Context context – information about running process

• program code (?)

• static data (bss)

• heap (dynamic data)

• procedure call stack - contains temporary data - subroutine parameters, return addresses, local variables

• register contents general purpose registers program counter (PC) — address of next instruction

to be executed stack pointer (SP) program status word (PSW) — interrupt status,

condition codes, etc.

• OS resources in use - open files, connections to other programs

• accounting information note that for each process there are two contexts

• for user mode and for kernel mode

program memory allocation

Page 8: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

8

Process Control Block

process control block (PCB) – data structure maintained by OS to keep track of process’ state

contains

• process id number

• user id of the owner

• process state (running, not-running, etc.)

• PC, SP, PSW, general purpose registers

• memory limits (static, dynamic) - base/limit register contents

• list of open files

• CPU scheduling information (e.g., priority)

• I/O states, I/O in progress - list of I/O devices allocated to process, list of open files, etc. status of I/O requests

• …

Page 9: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

9

Context Switch

context switch - stopping one process and restarting another

sequence of actions OS takes control

(through interrupt) saves old context in the

process PCB reloads new context

from the new process PCB• this amounts

to changingthe execution of processes (as soon as the PC is reloaded)

returns control to app. program how is context switch different from mode switch? How are they similar?

how many mode switches are there in a context switch? context switch requires several thousand instructions

• time-sharing OS may do 100–1000 context switches a second

Page 10: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

10

Interleaving of Process Execution in Time-Sharing OS

process

Aprocess

Bprocess

C

process

D

time process Aprocess Bprocess Cprocess Aprocess Bprocess Cprocess Aprocess Cprocess Aprocess Dprocess Cprocess Dprocess C

time

from the user’s standpointmultiple processesare executed concurrently

in reality OS interleavesprocess execution

Page 11: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

11

Five State Process Model

OS maintains the state of a process. process is

• new – when it is being created

• running - executing on the CPU

• waiting (blocked) – waiting on input/output even to occur

• ready – ready to run on the CPU

• terminated – finished execution, is being destroyed how many processes in the system can be in a particular state?

Page 12: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

12

OS Process Queues

OS queues PCBs according to their state ready queue –

processes in stateready (waiting tobe run on CPU)

for each device thereis an I/O queue –processes in statewaiting, blockedon particular devicewaiting for theirrequest to be submittedto the devicecontroller

Page 13: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

13

Short Term Scheduler

selects processfrom the readyqueue to run on the CPU

runs when

• process is created or terminated

• process switches from running to blocked

• interrupt occurs goals:

• minimize response time (e.g., program execution, character to screen)• minimize variance of average response time — predictability may be important• maximize throughput

minimize overhead (OS overhead, context switching, etc.) efficient use of resources

• fairness — share CPU in an equitable fashion

Page 14: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

14

Other Types of Schedulers

medium-term scheduler

• temporarilyswaps processes in/out of main memory

• may suspend/resume processes

• goal: balance load for better throughput long-term scheduler (job scheduler) – does not exist on time-sharing OS

• selects job from the spool, and loads it into memory

• executes infrequently, maybe only when process leaves system

• controls degree of multiprogramming

• goal: good mix of CPU-bound and I/O-bound processes I/O-bound process – spends more time doing I/O than

computations, many short CPU bursts CPU-bound process – spends more time doing computations; few

very long CPU bursts

Page 15: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

15

enhanced 5-state model blocked and ready states has to be split depending on

whether a process is swapped out on disk or in memory running state is also split depending on the mode: kernel or user Unix process states:

• created - just created not yet ready to run

• ready (memory) - ready as soon as kernel schedules it

• ready (disk) - ready, but needs to be swapped to memory

• asleep - blocked (memory) - waiting on event in memory

• asleep - blocked (disk) - waiting on event on disk

• running (kernel) - executing in kernel mode

• running (user) - executing in user mode

• zombie - process terminated but left a record for parent to collect

Unix Process States

Page 16: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

16

Unix Process Scheduling

process is running in user mode until an interrupt occurs or it executes a system call

if time slice expires the process is preempted and another is scheduled a process goes to sleep if it needs to wait for some event to occur and is woken

up when this event occurs when process is created decision is made whether to put it in memory or disk

zombie

readykernel

running ready

asleep asleep

created

userrunning

user mode kernel mode

disk

wakeup

swap out

wakeup

swap inswap out

not enough memory

enoughmemory

fork

exitsleep

schedule

return interrupt,system call

interrupt

preempt

Page 17: 1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.

17

Lecture Review

the execution of a certain task from OS standpoint is a process

• OS maintains (creates/destroys) processes for user

• OS schedules processes for user there are three types of schedulers: short/medium/long term

OS saves process states in a process control block OS context switches between processes in 5-state process model a process can be in one of five states: created,

running, ready, blocked, terminated

• UNIX adds states for being swapped out to disk

• depending on process state its PCB is attached to either ready queue or device queue