Linux下C语言中的nice函数


=Start=

缘由:

学习需要

正文:

参考解答:
#include <stdio.h>    /* printf */
#include <stdlib.h>   /* atoi, system, exit */
#include <errno.h>    /* errno */
#include <string.h>   /* strerror */
#include <unistd.h>   /* nice */
int main(int argc, char const *argv[])
{
    int adjustment = 0;
    if ( argc > 1 ) {
        adjustment = atoi( argv[1] );
    }
    errno = 0;
    int ret;
    ret = nice( adjustment );
    printf( "nice(%d):%d\n", adjustment, ret );
    if ( -1 == ret && errno) {
        if ( errno == EACCES ) {
            printf( "Cannot set priority:%s./n", strerror( errno ) );
            exit(-1);
        }
    }
    sleep(10);
    system("nice");
    return 0;
}

&

for i in {1..5}; do
    ps aux | grep --color "a.out"
    sleep 2
done
参考链接:

https://linux.die.net/man/3/nice

Linux C语言库函数参考 — nice
http://blog.csdn.net/thinkerabc/article/details/746764

C语言nice()函数:改变进程优先顺序
http://c.biancheng.net/cpp/html/283.html

Difference between nice and setpriority in unix
https://stackoverflow.com/questions/7618291/difference-between-nice-and-setpriority-in-unix

=END=

, ,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注