1
        int pri = getpriority(PRIO_PROCESS, 2134);
        printf("The original priority of 2134 is :%d", pri);

       //cpu_usage of the process in top is 80%, pri is 39 and NICE value is 19
        setpriority(PRIO_PROCESS, 20);

        int p = getpriority(PRIO_PROCESS, 2134);
        printf("priority is set to :%d", p);

Here I am trying to set a priority for a process(pid: 2134). In top command it is showing the priority is 39 and nice value is 19. So the priority of the process is very low. But the CPU usage of the process is above 70. If I set the priority to least value the CPU usage of that process should be reduced. I was confused whether to set the priority in the range of 40..1 or -19..20. Could anyone please check on this. Any help would be much appreciated.

Alperen Kantarcı
  • 1,038
  • 9
  • 27
vijay kumar kdp
  • 37
  • 1
  • 10

2 Answers2

3

The process priority is not proportional to its alloted percentage of cpu usage.

Instead, it only serves as a marker for the scheduler to determine which of two processes which want to run will get to run now.

So, a high priority process fighting it out with higher priority processes will get little to no attention, while even a low-priority process on an otherwise idle system can run all the time.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Thanks for your response Deduplicator. Lets take 5 processes running in an infinite loop of which, I set the priority of process 1 to least value. Consider 1 cycle, will scheduler schedules the process in every cycle or will it ignores the least prioritized process? – vijay kumar kdp Apr 15 '14 at 17:43
  • @vijaykumarkdp: Depends on the scheduler. There are common schedulers who never run lower-priority processes, and common ones that try not to starve low-priority processes completely, though they get far less time. – Deduplicator Apr 15 '14 at 18:07
0

I found this post Change priority of the current process in C showing how to set priority with function setpriority.

You have to use setpriority with 3 parameters

but you need to be root.

man of setpriority : http://linux.die.net/man/2/setpriority

Community
  • 1
  • 1
user43968
  • 2,049
  • 20
  • 37