Search code examples
cunixoperating-systemsystem

C - UNIX - check if getpriority() returns an error


I wanted to know how can I check if the value -1 returned of getpriority() is an error or a legitimate value. From the manual:

RETURN VALUE

  Since  getpriority() can legitimately return the value -1, it is neces-
  sary to clear the external variable errno prior to the call, then check
  it  afterwards  to  determine  if -1 is an error or a legitimate value.

I do not really know how to use the variable errno, because I always user perror(...) for showing errors...


Solution

  • Do what it says! Clear the value of errno, then call, then do your check.

    #include <errno.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    errno = 0;
    int val = getpriority();
    if (val == -1 && errno) {
      // we have an error!
    }