unix中如何申请进程调度的优先级

一、前言

unix系统中,进程的调度是由内核决定的。在一个系统中,进程的优先级越高,表示其在一定时间中占用cpu的时间越久。本文将介绍unix系统如何修改以及获取进程的优先级。

二、nice值

nice值是unix系统中用于表征进程优先级的一个参数。需要注意的是,普通的进程只能通过nice值来降低进程的优先级,只有特权进程能够提升进程的优先级。

nice值越低,其优先级越高。

2.1 nice函数

nice函数能够设置以及获取进程的nice值,其函数原型如下:

#include <unistd.h> int nice(int inc);

参数: inc:表示需要增加的nice值。可以是负值或正值,但只能在nice值范围内才能有效调整

返回值:

如果成功,返回调整后的nice值。如果失败,返回-1

参考代码如下:

c 复制代码
/*************************************************************************
        > File Name: nice_test.c
        > Author: conbiao
        > Created Time: 2024年10月09日 星期三 10时04分38秒
 ************************************************************************/

/***********************************************************************
 *                             HEADER
 **********************************************************************/
#include<stdio.h>
#include<unistd.h>

/***********************************************************************
 *                              MACRO
 **********************************************************************/


/***********************************************************************
 *                          GLOBAL VARIABLE
 **********************************************************************/


/***********************************************************************
 *                       FUNCTION DESCRIPTION
 **********************************************************************/
int get_nice(void);
int set_nice(int ni);

/***********************************************************************
* FUNCTION NAME: get_nice()
 ***********************************************************************
*
* Summary:
*   get process's nice
*
* Params:
*   void
*
* Return:
*   if success,return nice
*   if fail,return -1
*
***********************************************************************/
int get_nice(void)
{
    int ret = 0;

    printf("%s: start!\n",__func__);

    ret = nice(0);
    if(ret == -1)
    {
        printf("%s: get nice fail!\n",__func__);
    }

    printf("%s: end!\n",__func__);

    return ret;
}


/***********************************************************************
* FUNCTION NAME: set_nice()
 ***********************************************************************
*
* Summary:
*   set process's nice
*
* Params:
*   void
*
* Return:
*   if success,return nice
*   if fail,return -1
*
***********************************************************************/
int set_nice(int ni)
{
    int ret = 0;

    printf("%s: start!\n",__func__);

    ret = nice(ni);

    if(ret == -1)
    {
        printf("%s: set nice failed!\n",__func__);
    }

    printf("%s: end!\n",__func__);

    return ret;
}


/***********************************************************************
 *                                MAIN
 **********************************************************************/
int main(int argc, char *argv[])
{
    int ret = 0;

    printf("%s:start!\n",__func__);

    ret = get_nice();

    if(ret != -1)
    {
        printf("%s: nice is %d\n",__func__,ret);
    }

    ret = set_nice(2);
    if(ret != -1)
    {
        printf("%s: new nice is %d\n",__func__,get_nice());
    }


    return ret;
}

运行结果如下所示:

(2.1-1)

2.2 getpriority函数

getpriority函数能够获取进程的调度优先级,也能获取进程组的调度优先级,其函数原型如下:

#include <sys/resource.h>

int getpriority(int which, id_t who);

参数:

  • which:表示要查询的进程类型,可以是以下值之一:
    • PRIO_PROCESS:查询指定进程的优先级。
    • PRIO_PGRP:查询指定进程组的优先级。
    • PRIO_USER:查询指定用户所有进程的优先级。
  • who:与 which 参数一起使用,指定要查询的进程、进程组或用户的 ID。
    • 如果 which 为 PRIO_PROCESS,则 who 是进程的 PID;
    • 如果 which 为 PRIO_PGRP,则 who 是进程组的 ID;
    • 如果 which 为 PRIO_USER,则 who 是用户的 UID。 返回值:
  • 如果成功,返回相应进程或用户的优先级值。
  • 如果失败,返回 -1,并设置 errno 以指示错误原因。

参考代码如下:

c 复制代码
/*************************************************************************
        > File Name: priority_test.c
        > Author: conbiao
        > Created Time: 2024年10月09日 星期三 10时47分06秒
 ************************************************************************/

/***********************************************************************
 *                             HEADER
 **********************************************************************/
#include <stdio.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
/***********************************************************************
 *                              MACRO
 **********************************************************************/


/***********************************************************************
 *                          GLOBAL VARIABLE
 **********************************************************************/


/***********************************************************************
 *                       FUNCTION DESCRIPTION
 **********************************************************************/
int get_nice(void);


/***********************************************************************
* FUNCTION NAME: get_nice()
 ***********************************************************************
*
* Summary:
*      get nice
* Params:
*      void
* Return:
*      if success,return nice
*      if fail,return -1
*
***********************************************************************/
int get_nice(void)
{
    pid_t pid = getpid();

    int priority = getpriority(PRIO_PROCESS, pid);

    if(priority != -1)
    {
        return priority;
    }

    return -1;
}


/***********************************************************************
 *                                MAIN
 **********************************************************************/
int main(int argc, char *argv[])
{
    int ret = 0;

    printf("%s: nice is %d\n",__func__,get_nice());

    return ret;
}

运行结果如下:

(2.2-1)

参考资料:

《UNIX环境高级编程(第3版) (史蒂文斯 (W.Richard Stevens) 拉戈 (Stephen A.Rago))(Z-Library)》

相关推荐
面向对象World4 分钟前
正点原子Mini Linux 4.3寸800x480触摸屏gt115x驱动
linux·服务器·数据库
~央千澈~23 分钟前
抖音弹幕游戏开发之第12集:添加冷却时间机制·优雅草云桧·卓伊凡
java·服务器·前端
无心水1 小时前
5、微服务快速启航:基于Pig与BladeX构建高可用分布式系统实战
服务器·分布式·后端·spring·微服务·云原生·架构
问道飞鱼3 小时前
【服务器知识】nginx配置负载均衡完全解读
服务器·nginx·负载均衡
Hello.Reader4 小时前
从 0 到 1 理解硬盘数据恢复工具原理与工程实现
linux·运维·服务器·网络·数据库
小坏坏的大世界4 小时前
VMware 虚拟机无法上网问题排查
服务器·网络
切糕师学AI6 小时前
NAT (Network Address Translation,网络地址转换)
运维·服务器·网络
Channing Lewis8 小时前
为什么部署的项目会耗尽系统句柄
服务器
山峰哥9 小时前
SQL调优实战:从索引失效到性能飙升的破局之道
服务器·数据库·sql·性能优化·编辑器·深度优先
玩具猴_wjh9 小时前
JWT优化方案
java·服务器·数据库