37.7. 프로세스의 스케줄링 정책 및 관련 속성 표시
sched_getattr() 함수는 PID로 식별되는 지정된 프로세스에 현재 적용된 스케줄링 정책을 쿼리합니다. PID가 0이면 호출 프로세스의 정책이 검색됩니다.
size 인수는 userspace에 알려진 sched_attr 구조의 크기를 반영해야 합니다. 커널은 sched_attr::size 를 sched_attr 구조의 크기로 작성합니다.
입력 구조가 작으면 커널은 제공된 공간 이외의 값을 반환합니다. 결과적으로 E2BIG 오류로 인해 시스템 호출이 실패합니다. 다른 sched_attr 필드는 sched_attr 구조에 설명된 대로 채워집니다.
프로세스
sched_timeslice.c소스 파일을 생성하여 텍스트 편집기에서 엽니다.$ {EDITOR} sched_timeslice.csched_timeslice.c파일에 다음 행을 추가합니다.#define _GNU_SOURCE #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <linux/unistd.h> #include <linux/kernel.h> #include <linux/types.h> #include <sys/syscall.h> #include <pthread.h> #define gettid() syscall(__NR_gettid) #define SCHED_DEADLINE 6 /* XXX use the proper syscall numbers */ #ifdef __x86_64__ #define __NR_sched_setattr 314 #define __NR_sched_getattr 315 #endif struct sched_attr { __u32 size; __u32 sched_policy; __u64 sched_flags; /* SCHED_NORMAL, SCHED_BATCH */ __s32 sched_nice; /* SCHED_FIFO, SCHED_RR */ __u32 sched_priority; /* SCHED_DEADLINE (nsec) */ __u64 sched_runtime; __u64 sched_deadline; __u64 sched_period; }; int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int size, unsigned int flags) { return syscall(__NR_sched_getattr, pid, attr, size, flags); } int main (int argc, char **argv) { struct sched_attr attr; unsigned int flags = 0; int ret; ret = sched_getattr(0, &attr, sizeof(attr), flags); if (ret < 0) { perror("sched_getattr"); exit(-1); } printf("main thread pid=%ld\n", gettid()); printf("main thread policy=%ld\n", attr.sched_policy); printf("main thread nice=%ld\n", attr.sched_nice); printf("main thread priority=%ld\n", attr.sched_priority); printf("main thread runtime=%ld\n", attr.sched_runtime); printf("main thread deadline=%ld\n", attr.sched_deadline); printf("main thread period=%ld\n", attr.sched_period); return 0; }sched_timeslice.c파일을 컴파일합니다.$ gcc sched_timeslice.c -o sched_timeslicesched_timeslice프로그램의 출력을 확인합니다.$ ./sched_timeslice main thread pid=321716 main thread policy=6 main thread nice=0 main thread priority=0 main thread runtime=1000000 main thread deadline=9000000 main thread period=10000000