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

相关推荐
x1878337584几秒前
k8s的安装和部署
linux·服务器·kubernetes
南希夜酒1 分钟前
在Linux系统安装Nginx
java·linux·运维·服务器·centos·云计算
大吱佬9 分钟前
CentOS快速配置网络&&Docker快速部署
linux·服务器·tcp/ip
FANGhelloworld24 分钟前
TCP网络通信——多线程
linux·服务器·网络·信息与通信·tcp
我的运维人生1 小时前
Apache服务器深度解析与实践应用:构建高效Web服务的基石
服务器·前端·apache·运维开发·技术共享
4647的码农历程1 小时前
Linux网络编程 -- 网络套接字预备与udp
linux·服务器·网络
职场人参2 小时前
视频声音怎么去除?高效的视频声音去除方法
java·服务器·数据库
2401_857026232 小时前
医疗革新:Spring Boot医院管理系统
服务器·数据库·spring boot
手心里的白日梦3 小时前
Linux的环境与历史
linux·运维·服务器