NICE(3P)

🌸 NICE(3P) 🌸

okay bestie, so this is NICE(3P), straight out of the POSIX Programmer's Manual, and it's lowkey the origin story of "hey, can u chill a sec" for your processes πŸ’…


🫢 PROLOG

So real talk: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so if you're on Linux, go consult the corresponding Linux manual page for the deets on Linux behavior. And plot twist: the interface might not even be implemented on Linux at all. πŸ™ˆ Not every baddie makes it to every platform, iykyk.


✨ NAME

nice β€” change the nice value of a process πŸŽ€


πŸ’» SYNOPSIS

#include <unistd.h>

int nice(int incr);

🌈 DESCRIPTION

okay so here's the vibe: the nice() function shall add the value of incr to the nice value of the calling process. And what even is a nice value? It's a non-negative number, and the more positive it gets, the less favorable your scheduling becomes. so like... being "nice" is literally the sacrifice mechanic. you're giving up CPU time out of the goodness of your heart. iconic behavior honestly πŸ’–

The system imposes a maximum nice value of 2*{NZERO}-1 and a minimum nice value of 0. Ask for something above or below those limits and the nice value just gets set to the corresponding limit β€” the system is NOT negotiating, periodt.

⚠️ Only a process with appropriate privileges can lower the nice value. so you can make yourself nicer for free, but un-nice-ing yourself? that's a privilege thing bestie, don't try it without the credentials πŸ’€

Calling the nice() function has no effect on the priority of processes or threads with policy SCHED_FIFO or SCHED_RR. For processes or threads with other scheduling policies, the effect is implementation-defined. (mysterious. unknowable. very much "it depends" energy ✨)

The nice value set with nice() shall be applied to the process. And if the process is multi-threaded? the nice value shall affect all system scope threads in the process. everybody eats together 🫢

and now the classic gotcha, the one that gets everybody: since -1 is a permissible return value in a successful situation, if you're an application wishing to check for error situations, you should set errno to 0, then call nice(), and if it returns -1, check to see whether errno is non-zero. like, set errno to 0 FIRST. do not skip that step. this is the whole plot of the movie 😭


⭐ RETURN VALUE

Upon successful completion, nice() shall return the new nice value -{NZERO}. πŸ’–

Otherwise, -1 shall be returned, the nice value of the process shall not be changed, and errno shall be set to indicate the error. (no change, no cap, no nice β€” just an error code and a lesson πŸ™ˆ)


πŸ”₯ ERRORS

The nice() function shall fail if:

  • EPERM β€” The incr argument is negative and the calling process does not have appropriate privileges. πŸ’€ so you tried to lower the nice value without the clearance and the system said "absolutely not, ma'am."

The following sections are informative. (translation: the lore is over, everything from here on is bonus content πŸ‘‘)


πŸ“ EXAMPLES

🧚 Changing the Nice Value

The following example adds the value of the incr argument, -20, to the nice value of the calling process:

#include <unistd.h>
...
int incr = -20;
int ret;

ret = nice(incr);

look at her go. one little call, one big scheduling mood shift ✨


πŸ’œ APPLICATION USAGE

None.

(that's it. nothing. empty. the void. honestly? respect πŸŽ€)


🌸 RATIONALE

None.


πŸ¦‹ FUTURE DIRECTIONS

None.


πŸ‘‘ SEE ALSO

  • exec, getpriority()
  • The Base Definitions volume of POSIX.1‐2017, <limits.h>, <unistd.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie!! brought to you by IEEE/The Open Group, dated 2017, and yes the manual is literally called NICE(3P) πŸ’… go be nice to your scheduler, she's doing her best 🫢✨

πŸ“¦ where this page comes from