Scheduler Kook Joongjin tipsinessgmail com Operating Systems Lab
Scheduler Kook, Joongjin (tipsiness@gmail. com) Operating Systems Lab. Soongsil University 2009. 10. 13 1
문맥전환 – 레지스터의 저장 Process B PCB Process A PCB Stack ptr CPU Core CPU Register Save Restore Current running Context Switch cpsr r 0 r 1 r 2 r 3 r 4 r 5 r 6 r 7 r 8 r 9 r 10 r 11 r 12 r 14 (lr) r 15 (pc) Process B Stack(memory) Process A stack(memory) cpsr r 0 r 1 r 2 r 3 r 4 r 5 r 6 r 7 r 8 r 9 r 10 r 11 r 12 r 14 (lr) r 15 (pc) Process B Process A Time 14
O(n)과 O(1)의 스케줄러 성능 비교 r Rusty Russell의 커널 2. 4 vs 커널 2. 6 성능 비교 < 출처: http: //developer. osdl. org/craiger/hackbench/ > 20
struct runqueue r 실행 큐 (runqueue) w w 스케줄러의 기본자료 구조 kernel/sched. c에 정의되어 있음 struct runqueue { spinlock_t lock; unsigned long nr_running; /* 전체 실행중인 프로세스 수 */ unsigned long nr_switches; unsigned long nr_uninterruptible; unsigned long expired_timestamp; unsigned long timestamp_last_tick; task_t *curr, *idle; /* 현재 실행중인 프로세스 curr, idle 프로세스 */ struct mm_struct *prev_mm; /* 이전 프로세스의 주소 공간 */ prio_array_t *active, *expired, arrays[2]; int best_expired_prio; atomic_t nr_iowait; int push_cpu; … task_t *migration_thread; struct list_head migration_queue; }; 23
struct runqueue r active와 expired의 변경이 일어나는 schedule()의 일부. . . array = rq->active; // 모든 프로세스가 실행 시간을 모두 소비했으면 if (unlikely(!array->nr_active)) { //통계 정보를 업데이트 하고 schedstat_inc(rq, sched_switch); // 서로의 위치를 변경 rq->active = rq->expired; rq->expired = array; array = rq->active; rq->expored_timestamp = 0; rq->best_expored_prio = MAX_PRIO; }. . . 25
스케줄링 호출 그래프 r switch_tasks 레이블에서 수행하는 핵심코드 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 sched_info_switch(prev, next); if (likely(prev != next)) { next->timestamp = now; rq->nr_switches++; rq->curr = next; ++*switch_count; prepare_task_switch(rq, next); prev = context_switch(rq, prev, next); barrier(); /* * this_rq must be evaluated again because prev may have moved * CPUs since it called schedule(), thus the 'rq' on its stack * frame will be invalid. */ finish_task_switch(this_rq(), prev); } else 30
실습: 우선순위 요소 살펴보기 r prio. c를 통한 우선순위 설정 및 상태 보기 #include <stdio. h> <sys/types. h> <sys/resource. h> <unistd. h> //getpriority() //getpid() int main(void) { //현재 프로세스의 ID를 구함 pid_t current_pid = getpid(); //현재 프로세스의 우선순위를 구함 int priority = getpriority(PRIO_PROCESS, current_pid); printf("Process: %d, Priority: %dn", current_pid, priority); //for(; ; ); return 0; } 32
실습: 스케줄링 요소 살펴보기 r sched. c를 통한 스케줄링 요소 살펴 보기 #include <stdio. h> #include <stdlib. h> #include <sys/types. h> int main(void) { pid_t current_pid = getpid(); long type = sched_getscheduler(current_pid); char *type_name; if(type == 0) type_name = "Normal"; else if(type == 1) type_name = "FIFO"; else if(type == 2) type_name = "Round Robin"; else type_name = "Unknown"; printf("Current PID: %dn", (int)current_pid); printf("Scheduling Policy: %d, %sn", type_name); return 0; } 33
- Slides: 32