Linux設置進程的調度策略和優先級

系統調用接口

/**
 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
 * @p: the task in question.
 * @policy: new policy.
 * @param: structure containing the new RT priority.
 *
 * Return: 0 on success. An error code otherwise.
 *
 * NOTE that the task may be already dead.
 */

int sched_setscheduler(struct task_struct *p, int policy,
		       const struct sched_param *param);
	函數返回值0爲成功 其他值爲失敗

測試代碼

#include <stdio.h>

#include <sched.h>


int main()
{
        struct sched_param my_param;
        my_param.sched_priority =20;
        if(sched_setscheduler(getpid(),SCHED_FIFO,&my_param)){
                printf("set schedller fail....\n");
                perror("\n");
                return -1;
        }
        printf("set scheduler success\n");
        while(1)
        {};
        return 0;
}


編譯之後運行 運行時需要加上sudo 否則會設置失敗。

然後終端查看進程的調度策略
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章