Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal”...

26
Process Management Process Management CIS 657 CIS 657

Transcript of Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal”...

Page 1: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process ManagementProcess Management

CIS 657CIS 657

Page 2: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

DefinitionsDefinitions

Program: process under executionProgram: process under execution User mode: “normal” execution modeUser mode: “normal” execution mode Kernel mode: privileged execution mode; Kernel mode: privileged execution mode;

required for, e.g., I/O operations required for, e.g., I/O operations User state: registers, PC, PSW, stack pointers, User state: registers, PC, PSW, stack pointers,

plus memory contentsplus memory contents Kernel state:Kernel state:

Process structure (always resident)Process structure (always resident) User structure (only resident when process is)User structure (only resident when process is)

Page 3: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Multiprogramming and Multiprogramming and Multitasking SupportMultitasking Support

Context switching: changing the currently Context switching: changing the currently running process (changing execution context)running process (changing execution context) Hardware dependent; some ISA define instructions Hardware dependent; some ISA define instructions

to save processor context; others must explicitly to save processor context; others must explicitly savesave

Kernel software state saved/loaded explicitlyKernel software state saved/loaded explicitly Optimization is a very good idea…Optimization is a very good idea…

Scheduling: deciding which process to run nextScheduling: deciding which process to run next Related to CS, in that the CS just picks the process Related to CS, in that the CS just picks the process

at the head of the ready queue; scheduler maintains at the head of the ready queue; scheduler maintains ready queueready queue

Page 4: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Historical CommentHistorical Comment

Early UEarly UNIXNIX didn’t have multiprogramming didn’t have multiprogramming support: only one process in memory at a timesupport: only one process in memory at a time

MMU support allowed multiple processes, but MMU support allowed multiple processes, but synchronous disk I/O meant no multitasking synchronous disk I/O meant no multitasking (no automatically switching between (no automatically switching between processes)processes)

In 1972 asynchronous disk I/O made it In 1972 asynchronous disk I/O made it possible to switch between processes, so possible to switch between processes, so UUNIXNIX got real multitasking. got real multitasking.

Page 5: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

SchedulingScheduling

Processes have “bursts” of activity (both CPU Processes have “bursts” of activity (both CPU bursts and I/O bursts)bursts and I/O bursts)

We characterize processes by the ratio of We characterize processes by the ratio of these bursts:these bursts: Lots of I/O, little CPU: I/O bound (interactive)Lots of I/O, little CPU: I/O bound (interactive) Lots of CPU, little I/O: CPU bound (noninteractive)Lots of CPU, little I/O: CPU bound (noninteractive)

Real-time processes must have subportions Real-time processes must have subportions completed by particular deadlines; think of completed by particular deadlines; think of factory-floor automation.factory-floor automation.

Page 6: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

BSD SchedulingBSD Scheduling

BSD uses a priority-based round robin BSD uses a priority-based round robin scheme favoring interactive processesscheme favoring interactive processes

Processes have dynamic prioritiesProcesses have dynamic priorities Jobs get a time sliceJobs get a time slice

Complete your time slice: priority dropsComplete your time slice: priority drops Give up the CPU before the end: priority Give up the CPU before the end: priority

stays the samestays the same Inactive processes have priority increasedInactive processes have priority increased

Page 7: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Ramifications of SchedulingRamifications of Scheduling Long-lived CPU-bound Long-lived CPU-bound

jobs drop quickly in priority, jobs drop quickly in priority, they’ll run only when there aren’t any high-priority they’ll run only when there aren’t any high-priority

interactive jobs available.interactive jobs available. But they won’t starve (why not?)But they won’t starve (why not?)

Interactive jobsInteractive jobs Maintain high priorityMaintain high priority Don’t monopolize CPU (why not?)Don’t monopolize CPU (why not?)

Mixed-mode jobsMixed-mode jobs Drop in priority when CPU-boundDrop in priority when CPU-bound Rise back up later (why?)Rise back up later (why?)

Page 8: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

4.4BSD Process Structure4.4BSD Process Structure(See /usr/src/sys/sys/proc.h)(See /usr/src/sys/sys/proc.h)

ProcessStructure

machine-dependentprocess information

process group

process credential

VM space

file descriptors

resource limits

statistics

signal actions

process control blockprocess kernel stack

session

user credential

region list

file entries

} user structure

Page 9: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process Structure Main EntriesProcess Structure Main Entries

Process ID: process Process ID: process id and parent pidid and parent pid

Scheduling: priority, Scheduling: priority, CPU utilization, time CPU utilization, time spent sleepingspent sleeping

Process state: run Process state: run state of the process, state of the process, status flags, wait info status flags, wait info if sleepingif sleeping

Signal state: signals Signal state: signals pending delivery, pending delivery, current mask, and current mask, and actionsactions

Tracing informationTracing information Machine-dependent Machine-dependent

statestate Real-time timer and Real-time timer and

CPU utilization CPU utilization counterscounters

Page 10: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process Structure SubstructuresProcess Structure Substructures

Process group ID: process group and Process group ID: process group and session for this processsession for this process

User credentials: real, effective, and User credentials: real, effective, and saved UID and GIDsaved UID and GID

Memory Management: virtual address Memory Management: virtual address space management (page tables)space management (page tables)

File descriptors: pointers to open file File descriptors: pointers to open file entries and cwdentries and cwd

Page 11: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process Structure Substructures IIProcess Structure Substructures II

Resource Accounting: system/user time used, Resource Accounting: system/user time used, memory allocation, paging and disk I/O, etc. memory allocation, paging and disk I/O, etc. (see (see §§3.8)3.8)

Statistics (kept in user structure): accounting Statistics (kept in user structure): accounting information (see information (see §§3.9)3.9)

Signal actions (kept in user structure): Signal actions (kept in user structure): pointers to signal handlers, list of blocked pointers to signal handlers, list of blocked signals, etc. See /usr/src/sys/sys/signalvar.h.signals, etc. See /usr/src/sys/sys/signalvar.h.

Page 12: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

FreeBSD 4.4 User StructureFreeBSD 4.4 User Structure/*

* Per process structure containing data that isn’t needed in core when the

* process isn’t running (esp. when swapped out). This structure may or may not

* be at the same kernel address in all processes.

*/

struct user {

struct pcb u_pcb;

struct sigacts u_sigacts; /* p_sigacts points here (use it!) */

struct pstats u_stats; /* p_stats points here (use it!) */

/* Remaining fields only for core dump and/or ptrace—

not valid at other times! */

struct kinfo_proc u_kproc; /* proc + eproc */

struce md_coredump u_md; /* machine dependent glop */

}

Page 13: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process StatesProcess States

SIDL: process is partially createdSIDL: process is partially created SRUN: process is runnableSRUN: process is runnable SSLEEP: process is awaiting eventSSLEEP: process is awaiting event SSTOP: process is stopped (by signal or SSTOP: process is stopped (by signal or

parent process)parent process) SZOMB: process is partially terminatedSZOMB: process is partially terminated

Page 14: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

QueuesQueues

Run queues: hold processes ready to Run queues: hold processes ready to executeexecute Not a single ready queue; 32 queuesNot a single ready queue; 32 queues Artifact of the old VAX daysArtifact of the old VAX days

Sleep queues: hold processes waiting Sleep queues: hold processes waiting on eventson events Hashed data structure, optimized for Hashed data structure, optimized for

searching on searching on wait channelwait channel

Page 15: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Sleep() and Sleep QueuesSleep() and Sleep Queues

4.4 BSD defines:4.4 BSD defines:#define TABLESIZE 128#define TABLESIZE 128

static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE];static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE]; #define LOOKUP(x) (((intptr_t) (x) >> 8) & (TABLESIZE – 1)#define LOOKUP(x) (((intptr_t) (x) >> 8) & (TABLESIZE – 1)

LOOKUP(x) hashes an identifier to a sleep LOOKUP(x) hashes an identifier to a sleep queuequeue

Ident can be a void *, int, or other (generally a Ident can be a void *, int, or other (generally a pointer).pointer).

The sleep routines do not determine how The sleep routines do not determine how many identifiers there are in the systemmany identifiers there are in the system

Page 16: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Wakeup()Wakeup()

Wakeup awakens all processes on a Wakeup awakens all processes on a channelchannel Want to wake up everyone in case the Want to wake up everyone in case the

awakened process doesn’t actually use the awakened process doesn’t actually use the resource (e.g. disk buffers)resource (e.g. disk buffers)

Not a big race condition problem in a Not a big race condition problem in a uniprocessor (serialization of execution) uniprocessor (serialization of execution) with system calls (why?)with system calls (why?)

Page 17: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Scheduling PrioritiesScheduling Priorities

Processes have two scheduling Processes have two scheduling prioritiespriorities

p_usrpri: priority when executing user p_usrpri: priority when executing user codecode

p_priority: priority when inside the kernelp_priority: priority when inside the kernel Priorities range from 0-127Priorities range from 0-127

Lower value = higher priorityLower value = higher priority All priorities < 50 are kernel-onlyAll priorities < 50 are kernel-only

Page 18: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Important Process PrioritiesImportant Process Priorities

Priority Value Description

PSWP 0 swapping

PVM 4 waiting for memory

PINOD 8 waiting for file control information

PRIBIO 16 waiting on disk I/O completion

PVFS 20 waiting for kernel-level FS lock

PZERO 22 baseline priority

PSOCK 24 waiting on a socket

PWAIT 32 waiting for child to exit

PLOCK 36 waiting for user-level FS lock

PPAUSE 40 waiting for signal to arrive

PUSER 50 base user priority

Page 19: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Context SwitchingContext Switching

Involuntary context switch: time slice Involuntary context switch: time slice expired or higher-priority process is expired or higher-priority process is ready to runready to run

Voluntary context switch: call to sleep().Voluntary context switch: call to sleep(). The routine mi_switch() does the dirty The routine mi_switch() does the dirty

work, selecting the next process to run work, selecting the next process to run (see /usr/src/sys/kern/kern_synch.c).(see /usr/src/sys/kern/kern_synch.c).

Page 20: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process SynchronizationProcess Synchronization Processes in the bottom half of the kernel can’t call sleep.Processes in the bottom half of the kernel can’t call sleep. Coordinate with top half by setting priority levelsCoordinate with top half by setting priority levels Two flags: Two flags: lockedlocked and and wantedwanted..

ACQUIRE

while (locked) {

wanted = true;

sleep();

}

locked = true;

RELEASE

locked = 0;

If (wanted) {

Wakeup();

}

Page 21: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

splxxx (Set Priority Level)splxxx (Set Priority Level)

splxxx is generic splxxx is generic reference to Set Priority reference to Set Priority Level routineLevel routine

Returns old levelReturns old level Processes must be Processes must be

careful when allocating careful when allocating resources and calling resources and calling sleep() (remember the sleep() (remember the conditions for conditions for deadlock?)deadlock?)

S = spltty(); /* raise priority to block tty intr */

. . . /* frob tty */

splx(s); /* reset cpl */

NAME BLOCKS

spl0() normal mode

splsoftclock() low-priority clock

splnet() network protocols

spltty() terminals

splbio() block I/O

splimp network devices

splclock() high-priority clock

splhigh() all interrupt activity

Page 22: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Process Priority CalculationProcess Priority Calculation

From proc.h:From proc.h: p_estcpu provides estimate of recent CPU p_estcpu provides estimate of recent CPU

utilizationutilization p_nice user-settable weighting factor p_nice user-settable weighting factor

between –20 and 20 (default 0)between –20 and 20 (default 0) Recalculate every 4 ticks:Recalculate every 4 ticks:p_usrpri = PUSER + (p_estcpu / 4) + 2 x p_nice;p_usrpri = PUSER + (p_estcpu / 4) + 2 x p_nice;

Page 23: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Scheduling Calculations IIScheduling Calculations II

p_estcpu incremented every clock tick for p_estcpu incremented every clock tick for the current processthe current process

p_estcpu aged (decayed) every second; p_estcpu aged (decayed) every second; forgets about 90% of the history in a forgets about 90% of the history in a variable time periodvariable time period

Load_weight = (2 * load) / (2 * load + 1);Load_weight = (2 * load) / (2 * load + 1);

p_estcpu = Load_weight * p_estcpu + p_nice;p_estcpu = Load_weight * p_estcpu + p_nice;

Page 24: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

CPU SchedulingCPU Scheduling

roundrobin() causes the system to reschedule roundrobin() causes the system to reschedule processes in highest priority queue in RR processes in highest priority queue in RR fashionfashion

schedcpu() recalculates prioritiesschedcpu() recalculates priorities mi_switch(): machine-independent context mi_switch(): machine-independent context

switch (can’t call directly from bottom half)switch (can’t call directly from bottom half) cpu_switch(): machine-dependent context cpu_switch(): machine-dependent context

switchswitch

Page 25: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

mi_switch()mi_switch()

We can’t call mi_switch() directly from the We can’t call mi_switch() directly from the bottom half—it requires a process context to bottom half—it requires a process context to runrun

So, in the bottom half, we call need_resched() So, in the bottom half, we call need_resched() to post the need to reschedule (want_resched to post the need to reschedule (want_resched flag)sflag)s

The next time we’re leaving the kernel, if the The next time we’re leaving the kernel, if the want_resched flag is set, don’t resume the want_resched flag is set, don’t resume the current process; instead, call mi_switch()current process; instead, call mi_switch()

Page 26: Process Management CIS 657. Definitions l Program: process under execution l User mode: “normal” execution mode l Kernel mode: privileged execution mode;

Let’s Explore the KernelLet’s Explore the Kernel

What happens when the hardware clock What happens when the hardware clock goes off?goes off?

How are software clock ticks handled?How are software clock ticks handled? Where is scheduling done?Where is scheduling done? How are the run and sleep queues How are the run and sleep queues

managed?managed? How are processes awakened?How are processes awakened?