#author("2019-05-29T16:41:47+09:00","default:honma","honma")
* sched_getattr [#xc39e4ea]

スケジューリングポリシーやパラメータを取得する

#highlight(c){{
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>

typedef unsigned long long u64;
typedef unsigned int u32;
typedef int s32;

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 */
	u64 sched_runtime;
	u64 sched_deadline;
	u64 sched_period;
};

#define sched_getattr(pid, attr, size, flags) syscall(__NR_sched_getattr, pid, attr, size, flags)

void get_prio(void)
{
	int ret;
	int policy;
	struct sched_param param;
	struct sched_attr attr;

	policy = sched_getscheduler(0);
	printf("policy = %d\n", policy);

	ret = sched_getparam(0, &param);
	if (ret == 0) {
		printf("sched_priority = %d\n", param.sched_priority);
	}
	else {
		perror("sched_getparam");
	}

	/*
	 * sched_getattr() は glibcラッパー関数がないので自分で syscall() する
	 */
	ret = sched_getattr(0, &attr, sizeof(attr), 0);
	if (ret == 0) {
		printf("sched_policy = %d\n", attr.sched_policy);
		printf("sched_nice = %d\n", attr.sched_nice);
		printf("sched_priority = %d\n", attr.sched_priority);
	}
	else {
		perror("sched_getattr");
	}
}

int main(int argc, char **argv)
{
	get_prio();
	/* キー入力待ち */
	getchar();

	get_prio();
	/* キー入力待ち */
	getchar();

	return 0;
}
}}
#highlight(end)
[[ソースコード]]

実行結果

 $ ./sched_get_prio
 policy = 0
 sched_priority = 0
 sched_policy = 0
 sched_nice = 0
 sched_priority = 0
 
 ※ reniceで nice値を変えてみる
 
 policy = 0
 sched_priority = 0
 sched_policy = 0
 sched_nice = 1
 sched_priority = 0

別のターミナルから renice を実行

 $ cat /proc/`pidof sched_get_prio`/sched | grep -e policy -e prio
 sched_get_prio (93999, #threads: 1)
 policy                                       :                    0
 prio                                         :                  120
 $ cat /proc/`pidof sched_get_prio`/stat | awk -F' ' '{print $18,$19}'
 20 0
 
 $ sudo renice -n 1 -p `pidof sched_get_prio`
 [sudo] ****** のパスワード:
 93999 (process ID) old priority 0, new priority 1
 $ cat /proc/`pidof sched_get_prio`/sched | grep -e policy -e prio
 sched_get_prio (93999, #threads: 1)
 policy                                       :                    0
 prio                                         :                  121
 $ cat /proc/`pidof sched_get_prio`/stat | awk -F' ' '{print $18,$19}'
 21 1


#htmlinsert(amazon_book.html);

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS