From 3d0320f8912188663f1aab908d37736bc482b528 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 22 Jun 2021 17:00:09 +0800 Subject: [PATCH] libc/sched: Map the nice value more correctly from https://pubs.opengroup.org/onlinepubs/007904875/functions/setpriority.html: 1.The nice value shall in the range [-{NZERO},{NZERO} -1] 2.Lower nice value shall cause more favorable scheduling Signed-off-by: Xiang Xiao Change-Id: I5ad60d92abc3b69fbaa406da68cec2e40ca3fa6d --- include/limits.h | 4 ++-- libs/libc/unistd/lib_getpriority.c | 6 +++++- libs/libc/unistd/lib_setpriority.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/limits.h b/include/limits.h index ec18dd7127..28f02c4254 100644 --- a/include/limits.h +++ b/include/limits.h @@ -276,10 +276,10 @@ #define NL_TEXTMAX _POSIX2_LINE_MAX /* NZERO - * Default process priority. Minimum Acceptable Value: 20 + * Default process priority. Minimum Acceptable Value: 128 */ -#define NZERO 20 +#define NZERO 128 /* Required for asynchronous I/O */ diff --git a/libs/libc/unistd/lib_getpriority.c b/libs/libc/unistd/lib_getpriority.c index 95996d2313..44d9c8d8ed 100644 --- a/libs/libc/unistd/lib_getpriority.c +++ b/libs/libc/unistd/lib_getpriority.c @@ -82,5 +82,9 @@ int getpriority(int which, id_t who) return ret; } - return param.sched_priority; + /* Since -1 is a legal return value, clear errno to avoid the chaos */ + + set_errno(0); + + return NZERO - param.sched_priority; } diff --git a/libs/libc/unistd/lib_setpriority.c b/libs/libc/unistd/lib_setpriority.c index 057f05fa73..d02c7af41e 100644 --- a/libs/libc/unistd/lib_setpriority.c +++ b/libs/libc/unistd/lib_setpriority.c @@ -69,7 +69,7 @@ int setpriority(int which, id_t who, int value) return ret; } - param.sched_priority = value; + param.sched_priority = NZERO - value; return sched_setparam(who, ¶m); }