unistd: introduce nice()

This commit implements nice() as specified in POSIX.1-2008.

Fixes #4932.

Change-Id: Ibd8d4636d9e3a8adf0f26a081d5b1180f0cbb839
Reviewed-on: https://review.haiku-os.org/c/863
Reviewed-by: Jérôme Duval <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Leorize
2019-01-19 18:31:58 +00:00
committed by waddlesplash
parent 8ae2e95643
commit b9c25b0d0e
3 changed files with 37 additions and 0 deletions
+2
View File
@@ -246,6 +246,8 @@ extern pid_t setpgrp(void);
extern int chroot(const char *path);
extern int nice(int incr);
/* access permissions */
extern gid_t getegid(void);
extern uid_t geteuid(void);
+1
View File
@@ -30,6 +30,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
lockf.cpp
lseek.c
mount.c
nice.c
pause.c
pipe.c
process.c
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright 2019, Leorize <[email protected]>. All rights reserved.
* Distributed under the terms of the MIT license.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#include <limits.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/param.h> /* MAX(), MIN() */
/* setpriority() is used for the implementation as they share the same
* restrictions as defined in POSIX.1-2008. However, some restrictions might not
* be implemented by Haiku's setpriority(). */
int
nice(int incr)
{
int priority = incr;
/* avoids overflow by checking the bounds beforehand */
if (priority > -(2 * NZERO - 1) && priority < (2 * NZERO - 1))
priority += getpriority(PRIO_PROCESS, 0);
priority = MAX(priority, -NZERO);
priority = MIN(priority, NZERO - 1);
return setpriority(PRIO_PROCESS, 0, priority) != -1 ? priority : -1;
}