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)》

相关推荐
christine-rr3 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆3 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++3 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
绿箭柠檬茶3 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
獭.獭.3 天前
Linux -- 信号【上】
linux·运维·服务器
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
ZERO_pan3 天前
服务器装机遇到的问题
运维·服务器
l1t3 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
杀气丶3 天前
Linux下运行芙蕾雅天堂2【俄文简译L2FATER】
运维·服务器·天堂2·l2fater·l2fater.cn
喵手3 天前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络