0

As I understand every user in linux can change the every process's priority by using nice and renice commands. Also I know that the all users just can increase the priority of processes and the only user that can decrease this priority is ROOT. When I test this in Terminal environment using these commands everything was OK. Mean's that when I want to decrease the priority of process the system issues the "permission denied" text message. As I know too by default all processes runs with priority 0 .So when I want to set an process's priority by a negative number, the system must issues an error text. When I run this C language program:

#include<stdio.h>
#include<unistd.h>
void main()
{
int n=10,r;
r=nice(n);
while(1)
    printf("Test);
}

I expected that the system shows an error But program runs with no problem! Although the program runs by a typical user permission. I want to know why when I want to decrease the process's priority using renice command according to what was expected the system shows error, but when I want to decrease process's priority by using nice() function the programs doesn't issues any error? Is this sentence All users just can increase the priority of process and the only user that can decrease this priority is ROOT. correct?

4
  • Maybe, you mean "increase niceness"? Niceness of a process is an opposite concept to priority. All processes may decrease their processor usage (== increase its niceness, == descrease the priority), but only root processes may eat more than before. Dec 2, 2012 at 20:04
  • yes,exactly my question is about niceness.you write "All processes may decrease their processor usage, but only root processes may eat more than before."can you explain that,please?I couldn't understand this correctly .
    – Azad
    Dec 2, 2012 at 20:20
  • It's a security decision: to make Denial of Service impossible, I think. Users do need to increase priority of processes, but some malicious users may use this to bring a system down, so only the root (a really trusted user) is permitted to do it. Dec 2, 2012 at 20:22
  • yes, but why when I use the nice() function in my program, I can decrease the process's priority(Although I have my typical user!)?
    – Azad
    Dec 2, 2012 at 20:26

2 Answers 2

1

Azad, you're NOT able to reduce the nice-value; you simply chose not to check the result of what you're doing. Check the content of r after you try to apply a negative increment. It will be -1, which means your attempt failed.

0
#include<stdio.h>
#include<unistd.h>
#include<sys/resource.h>
void main()
{
    int n=10;
    (void)setpriority(PRIO_PROCESS, 0, n);
    sleep(60);
    while(1)
        printf("Test");
}

After setting the process priority to required level, delay by 1 mins so that we can check altered priority in top/ps command. Check this link, http://linux.die.net/man/2/setpriority

You must log in to answer this question.