I wrote a program that realizes the equivalent of the nice
command in linux.
How can I modify the program to report the failure to stderr, take over and execute a simple command?
#include <stdio.h>
#include <sys/resource.h>
int my_nice(int incr)
{
/* adjusts the nicess value of a process by +incr.
Returns -1 on failure.
Returns new priority on success.
*/
int prio = getpriority(PRIO_PROCESS, 0);
if (setpriority(PRIO_PROCESS, 0, prio + incr) == -1)
return -1;
prio = getpriority(PRIO_PROCESS, 0);
return prio;
}
int main(void)
{
int prio = getpriority(PRIO_PROCESS, 0);
printf("Current priority = %d\n", prio);
printf("\nAdding +5 to the priority\n");
my_nice(5);
prio = getpriority(PRIO_PROCESS, 0);
printf("Current priority = %d\n", prio);
printf("\nAdding -7 to the priority\n");
my_nice(-7);
prio = getpriority(PRIO_PROCESS, 0);
printf("Current priority = %d\n", prio);
return 0;
}