Soong-Sil University
Process Scheduling and Time
숭실대학교 대학원 소프트웨어공학 연구실
이병헌 (leebh42@kebi.com)
SOONG-SIL UNIVERSITY SE LAB.
Introduction
Time Sharing System
Time slice or Time quantum is allocated to the process Uses relative time of execution
Scheduler in Unix
Every active process has a scheduling priority
Lee Byoung-Heon
운영체제론
2
SOONG-SIL UNIVERSITY SE LAB.
Process Scheduling
The scheduler on Unix System
Round robin with multilevel feedback
Allocates the CPU to process for a time quantum Preempts a process that exceeds its time quantum
Feeds it back into one of several priority queues
Context Switch
Process resumes execution from the point where it
had been suspended
Lee Byoung-Heon
운영체제론
3
SOONG-SIL UNIVERSITY SE LAB.
Algorithm for Process Scheduling
Selecting the highest priority process from those in the states “ready to run and loaded in memory” and “preempted” If several processes tie for highest priority, the kernel picks the one that has been “ready to run” for the longest time If there are no processes eligible for execution, the processor idles until the next interrupt After handling that interrupt, the kernel again attempts to schedule a process to run
Lee Byoung-Heon
운영체제론
4
SOONG-SIL UNIVERSITY SE LAB.
Algorithm for Process Scheduling
Algorithm schedule_process Input : none Output : none { while ( no process picked to execute ) { for ( every process on run queue ) pick highest priority process that is loaded in memory; if ( no process eligible to execute) idle the machine; /* interrupt takes machine out of idle state */ } remove chosen process from run queue; switch context to that of chosen process, resume its execution; }
Lee Byoung-Heon
운영체제론
5
SOONG-SIL UNIVERSITY SE LAB.
Scheduling Parameters
다음에 스케줄 될 프로세스를 결정하기 위한 값 : 상 대적 수행시간 이용 Each process table entry contains a priority field for process scheduling The range of process priority
User priority
Is below a threshold value Is preempted on their return from the kernel to user
mode
Kernel priority
Is above a threshold value
Achieves them in the sleep algorithm
Lee Byoung-Heon
운영체제론
6
SOONG-SIL UNIVERSITY SE LAB.
Range of Process Priority
Kernel Mode Priorities Not Interruptible Priority Levels Swapper
Processes
Waiting for Disk IO Waiting for Buffer
Waiting for Inode Waiting for TTY Input
Interruptible
Threshold Priority
Waiting for TTY Output
Waiting for Child Exit User Level 0 User Level 1
User Mode Priorities
Lee Byoung-Heon
User Level n
운영체제론
7
SOONG-SIL UNIVERSITY SE LAB.
Calculation of the priority of a process
Processes that sleep in lower-level algorithms tend to cause more system bottlenecks the longer they are inactive
A process sleeping and waiting for the completion of disk I/O has a higher priority than a process waiting for a free buffer
The process waiting for completion of disk I/O
already has a buffer A process waiting for a free buffer may be waiting for a buffer held by the process waiting for completion of I/O
Lee Byoung-Heon
운영체제론
8
SOONG-SIL UNIVERSITY SE LAB.
Calculation of the priority of a process
The kernel adjusts the priority of a process that returns from kernel mode to user mode
Kernel priority must be lowered to a user-level priority when returning to user mode The kernel penalizes the executing process
Clock handler runs scheduling algorithm at 1 second intervals
It adjusts the priorities of all processes in user mode It prevents a process from monopolizing use of the CPU
Lee Byoung-Heon
운영체제론
9
SOONG-SIL UNIVERSITY SE LAB.
Calculation of the priority of a process
Clock handler Increments a field in the process table that records the recent CPU sage of the process Adjusts the recent CPI usage of each process according to a decay function
decay(CPU) = CPU/2
Recalculates the priority of every process
priority=(“recent CPU usage”/2)+(base level user priority)
Processes in the “ready-to-run” state will tend to occupy more priority level Periodic recalculation for process priority assures a round robin scheduling policy for processes executing in user mode
Lee Byoung-Heon
운영체제론
10
SOONG-SIL UNIVERSITY SE LAB.
Movement of a Process in Priority Queues
Kernel Mode Priorities Not Interruptible Priority Levels Swapper
Processes
Waiting for Disk IO Waiting for Buffer
Waiting for Inode Waiting for TTY Input
Interruptible
Threshold Priority
Waiting for TTU Output
Waiting for Child Exit User Level 0 User Level 1
User Mode Priorities
Lee Byoung-Heon
User Level n
운영체제론
11
SOONG-SIL UNIVERSITY SE LAB.
Examples of Process Scheduling
The Processes are created simultaneously with initial priority 60 The highest user-level priority is 60 The clock interrupts the system 60 times a second The processes make no system calls and no other processes are ready to run CPU = decay(CPU) = CPU/2 priority = (CPU/2) + 60
Lee Byoung-Heon
운영체제론
12
SOONG-SIL UNIVERSITY SE LAB.
Examples of Process Scheduling(cont.)
Time
0
60
Proc A
Priority CPU Count
0 1 2 : 60 30
Proc B
Priority
60
Proc C
CPU Count
0
Priority
60
CPU Count
0
1
75
60
2
67
15
75
0 1 2 : 60 30
60
0
60
3
63
4
76
7 8 9 : 67 33
67
15
75
0 1 2 : 60 30
63
5
68
16
76
7 8 9 : 67 33
67
15
63
7
Lee Byoung-Heon
운영체제론
13
SOONG-SIL UNIVERSITY SE LAB.
Examples of Process Scheduling(cont.)
The tie-breaker rule for processes with equal priority
The user-level priority of process A may low as it had received several time quanta on the CPU : the kernel leaves process A in the state “ready to run” The process B may enter the “ready to run” state, and its user-level priority may be higher than that of process A If the kernel does not schedule either process for a while, both processes could eventually be at the same user priority level The kernel would choose to schedule process A
운영체제론
14
Lee Byoung-Heon
SOONG-SIL UNIVERSITY SE LAB.
RR Scheduling & Process Priorities
60
B Higher Priority
A (a) 60
B
A (b)
A
(c)
B A
A B
B
(A runs first)
(d)
Lee Byoung-Heon
(e)
운영체제론
(f)
15
SOONG-SIL UNIVERSITY SE LAB.
Controlling Process Priorities
Processes can exercise crude control by using the nice system call nice(value), where value is added in the calculation of process priority Priority=(“recent CPU usage”/constant)+(base priority)+(nice value) Only the superuser can supply nice values that increase the process priority Only the superuser can supply a nice value below a particular threshold The nice system call works for the running process only
Lee Byoung-Heon 운영체제론
16
SOONG-SIL UNIVERSITY SE LAB.
Fair Share Scheduler
Some set of users may want the certain amount of CPU time on a guaranteed basis The principle
The user community is divided into a set of fair share groups The system allocates its CPU time proportionally to each group
Lee Byoung-Heon
운영체제론
17
SOONG-SIL UNIVERSITY SE LAB.
Fair Share Scheduler(cont.)
ex) Four groups, each group has 1,2,3 and 4 processes respectively
Regular scheduling algorithm: 10% of CPU time for each process Fair Share Scheduler: 25% of CPU time for each group
Simple to implement
Lee Byoung-Heon
운영체제론
18
SOONG-SIL UNIVERSITY SE LAB.
Example of Fair Share Scheduler
Time
Priority
Proc A
CPU
0 1 2 : 60 30
Proc B
Group
0 1 2 : 60 30
Proc C
Group
0
Priority
60
CPU
0
Priority
60
CPU
0
Group
0
0
60
1
90
60
2
74
3
96
15 16 17 : 75 37
15 16 17 : 75 37
90
0 1 2 : 60 30
0 1 2 : 60 30
60
0
75
0
0 1 2 : 60 30
74
15
4
78
5
98
18 19 20 : 78 39
18 19 20 : 78 39
81
7
15 16 17 : 75 37
67
93
0 1 2 : 60 30
15 16 17 : 75 37
70
3
18
76
15
18
Lee Byoung-Heon
운영체제론
19
SOONG-SIL UNIVERSITY SE LAB.
Real-Time Processing
implies the capability to provide immediate response to specific external events Kernel should be preemptive No standard UNIX system has this capability
Lee Byoung-Heon
운영체제론
20
SOONG-SIL UNIVERSITY SE LAB.
System Calls For Time
Global system time(Time from January 1, 1970)
stime(pvalue); superuser set a global kernel variable to a value that gives the current time time(tloc); time retrieves the time as set by stime
times(tbuffer) struct tms *tbuffer;
cumulative times the calling process spent executing
Individual processes
in user mode and kernel mode Cumulative times that all zombie children had executed in user mode and kernel mode Elapsed time, usually the time of system boot
alarm; user processes can schedule alarm signals using the alarm system call
운영체제론
21
Lee Byoung-Heon
SOONG-SIL UNIVERSITY SE LAB.
#include #include extern long times(); main(){ int i; /* tms is data structure containing 4 time elements */ struct tms pb1, pb2; long pt1, pt2; pt1 = times(&pb1); for (i=0; i<10; i++) if(fork() == 0) child(i); for (i=0; i<10; i++) wait((int *)0); pt2 = times(&pb2); printf(“parent real %u user %u sys %u cuser %u csys %u\n”, pt2-pt1, pb2.tms_utime-pb1.tms_utime, pb2.tms_stime-pb1.tms_stime, pb2.tms_cutime-pb1.tms_cutime, pb2.tms_cstime-pb1.tms_cstime); } child(n) int n; { int i; struct tms cb1, cb2; long t1, t2; t1=times(&cb1); for (i=0; i<10000; i++) ; t2 = times(&cb2); printf(“child %d: real %u user %u sys %u\n”, n, t2-t1, cb2.tms_utime-cb1.tms_utime, cb2.tms_stime-cb1.tms_stime); exit(); }
Lee Byoung-Heon
운영체제론
22
SOONG-SIL UNIVERSITY SE LAB.
#include #include #include main(argc, argv) int argc; char *argv[]; { extern unsigned alarm(); extern wakeup(); struct stat statbuf; time_t axtime; if (argc != 2){ printf(“only 1 arg\n”); exit(); } axtime = (time_t) 0; for(;;){ /* find out file access time */ if (stat(argv[1], &statbuf) == -1){ printf”file %s not there\n”, argvp[1]); exit(); } if(axtime != statbuf.st_atime){ printf(“file %s accessed\n”, argv[1]); axtime = statbuf.st_atime; } signal(SIGALRM, wakeup); /* reset for alarm */ alarm(60); pause(); /* sleep until signal */ } } Wakeup(){ }
Lee Byoung-Heon
운영체제론
23
SOONG-SIL UNIVERSITY SE LAB.
Clock
The functions of the clock interrupt handler
restart the clock, schedule invocation of internal kernel functions based on internal timers, provide execution profiling capability for the kernel and for user processes, gather system and process accounting statistics, keep track of time, send alarm signals to processes on request, periodically wake up the swapper process control process scheduling
24
Lee Byoung-Heon
운영체제론
SOONG-SIL UNIVERSITY SE LAB.
Algorithm for the Clock Handler
algorithm clock input : none output : none { restart clock; /* so that it will interrupt again */ if (callout table not empty) { adjust callout times; schedule callout function if time elapsed; } if (kernel profiling on) note program counter at time of interrupt; gather system statistics; gather statistics per process; adjust measure of process CPU utilization; if (1 second or more since last here and interrupt not in critical region of code) { for (all processes in the system) { adjust alarm time if active; adjust measure of CPU utilization; if (process to execute in user mode) adjust process priority; } wakeup swapper process is necessary; } }
Lee Byoung-Heon
운영체제론
25
SOONG-SIL UNIVERSITY SE LAB.
Internal System Timeouts
The kernel stores the necessary information in the callout table
Callout table consists of the function, a parameter for the function, and the time in clock ticks
The user has no direct control over the entries in the call out table The kernel sorts entries in the callout table according to their respective “time to fire”
Function a( ) b( ) c( ) Time to Fire -2 3 10 Before
Lee Byoung-Heon 운영체제론
Function a( ) b( ) f( ) c( )
Time to Fire -2 3 2 8 After
26
f( ) 함수가 5 클럭 틱 이후 발생한다고 가정
SOONG-SIL UNIVERSITY SE LAB.
Profiling
gives a measure of how much time the system is executing in user mode versus kernel mode, and how much time it spends executing individual routines in the kernel. The kernel profile driver monitors the relative performance of kernel modules by sampling system activity at the time of a clock interrupt The profile driver has a list of kernel addresses to sample, usually addresses of kernel functions
Lee Byoung-Heon
운영체제론
27
SOONG-SIL UNIVERSITY SE LAB.
Sample addresses of kernel Algorithms
Algorithm Address Count bread 100 5 breada 150 0 bwrite 200 0 brelse 300 2 getblk 400 1 user 2
Lee Byoung-Heon
운영체제론
28
SOONG-SIL UNIVERSITY SE LAB.
Profiling(cont.)
The mechanism does not account for time spent executing the clock handler and code that blocks out clock-level interrupts
Because the clock cannot interrupt such critical regions of code and therefore cannot invoke the profile interrupt there buff: the address of an array in user space bufsize: the size of the array offset: the virtual address of a user subroutine scale: a factor that maps user virtual addresses into the array
운영체제론
29
profil(buff, bufsize, offset, scale)
Lee Byoung-Heon
SOONG-SIL UNIVERSITY SE LAB.
#include int buffer[4096]; main(){ int offset, endof, scale, eff, gee, text; extern theend(), f(), g(); signal (SIGINT, theend); endof = (int) theend; offset = (int) main; /* calculates number of words of program text */ text = (endof – offset + sizeof(int) – 1) / sizeof(int); scale = 0xffff; printf(“offset %d endof %d text %d\n”, offset, endof, text); eff = (int) f; gee = (int) g; printf(“f %d g %d fdiff %d gdiff %d\n”, eff, gee, eff-offset, gee-offset); profil(buffer, sizeof(int)*text, offset, scale); for(;;){ f(); g(); } } f(){ } g(){ } theend(){ int i; for (i=0; i<4096, i++) if(buffer[i]) printf(“buf[%d] = %d\n”, i, buffer[i]); exit(); }
Lee Byoung-Heon
운영체제론
30
SOONG-SIL UNIVERSITY SE LAB.
Accounting and Statistics
Every process has two fields in its u area to keep a record of elapsed kernel and user time Every process has one field in its u area for the kernel to log its memory usage
When the clock interrupts a running process, the kernel calculates the total memory used by a process as a function of its private memory regions and its proportional usage of shared memory regions
Lee Byoung-Heon
운영체제론
31