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:
@@ -246,6 +246,8 @@ extern pid_t setpgrp(void);
|
|||||||
|
|
||||||
extern int chroot(const char *path);
|
extern int chroot(const char *path);
|
||||||
|
|
||||||
|
extern int nice(int incr);
|
||||||
|
|
||||||
/* access permissions */
|
/* access permissions */
|
||||||
extern gid_t getegid(void);
|
extern gid_t getegid(void);
|
||||||
extern uid_t geteuid(void);
|
extern uid_t geteuid(void);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
|||||||
lockf.cpp
|
lockf.cpp
|
||||||
lseek.c
|
lseek.c
|
||||||
mount.c
|
mount.c
|
||||||
|
nice.c
|
||||||
pause.c
|
pause.c
|
||||||
pipe.c
|
pipe.c
|
||||||
process.c
|
process.c
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user