cdsfiui 发表于 2017-01-09 22:48

C函数的设置进程优先级,不起作用,帮我看看吧

本帖最后由 cdsfiui 于 2017-01-10 14:50 编辑

很简单的一段代码:

#include<unistd.h>
#include<sys/resource.h>
#include<sys/syscall.h>
#include<sys/types.h>
#include<stdio.h>
int main()
{
    int i=0;
    pid_t pid=getpid();
    pid_t tid=syscall(SYS_gettid);
    printf("%d,%d\n",pid,tid);
    setpriority(PRIO_PROCESS,0,-2);
    while(true){
      ++i;
    }
    return 0;
}

setpriority函数调用的时候,第二个参数填0,指代当前进程。这个是根据man文档来的:
       Thevaluewhichis one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER, and
       whoisinterpretedrelativetowhich(aprocessidentifierfor
       PRIO_PROCESS, process group identifier for PRIO_PGRP, and a user ID for
       PRIO_USER).A zero value for who denotes(respectively)thecalling
       process,the process group of the calling process, or the real user ID
       of the calling process.Prio is a value in the range -20to19(but
       seetheNotesbelow).   Thedefault priority is 0; lower priorities
       cause more favorable scheduling.

编译运行后,我看top命令中,a.out的PR仍然是默认的20,没有我期待的-2变成了18
这是为什么,拿到我的setpriority没有起作用吗?

windoze 发表于 2017-01-09 23:38

setpriority (2)

mournjust 发表于 2017-01-10 17:21

回复 1# cdsfiui

sudo ./a.out

MMMIX 发表于 2017-01-10 19:36

回复 1# cdsfiui

不检查函数/系统调用的返回值是非常糟糕的习惯,尽早改了吧。

cdsfiui 发表于 2017-01-10 20:10

mournjust 发表于 2017-01-10 17:21
回复 1# cdsfiui

sudo ./a.out

sudo ./a.out果然可以! 原来是执行权限的问题。

yulihua49 发表于 2017-01-10 20:42

cdsfiui 发表于 2017-01-09 22:48
很简单的一段代码:

#include


我现在用别的办法解决优先级问题了。
页: [1]
查看完整版本: C函数的设置进程优先级,不起作用,帮我看看吧