Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Dec 3 '12 at 9:15

This question came from our site for professional and enthusiast programmers.

    
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. –  EarlGray Dec 2 '12 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 '12 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. –  EarlGray Dec 2 '12 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 '12 at 20:26

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
#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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.