pthread_setschedparam 设置线程的权限
int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param)
参数 1. target_thread是使用pthread_create所获得的线程ID。
2.线程的调度有三种策略:SCHED_OTHER、SCHED_RR和SCHED_FIFO。Policy用于指明使用哪种策略。下面我们简单的说明一下这三种调度策略。
SCHED_OTHER
它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的。简单地说,如果系统使用这种调度策略,程序将无法设置线程的优先级。请注意,这种调度策略也是抢占式的,当高优先级的线程准备运行的时候,当前线程将被抢占并进入等待队列。这种调度策略仅仅决定线程在可运行线程队列中的具有相同优先级的线程的运行次序。
SCHED_FIFO
它是一种实时的先进先出调用策略,且只能在超级用户下运行。这种调用策略仅仅被使用于优先级大于0的线程。它意味着,使用SCHED_FIFO的可运行线程将一直抢占使用SCHED_OTHER的运行线程J。此外SCHED_FIFO是一个非分时的简单调度策略,当一个线程变成可运行状态,它将被追加到对应优先级队列的尾部((POSIX 1003.1)。当所有高优先级的线程终止或者阻塞时,它将被运行。对于相同优先级别的线程,按照简单的先进先运行的规则运行。我们考虑一种很坏的情况,如果有若干相同优先级的线程等待执行,然而最早执行的线程无终止或者阻塞动作,那么其他线程是无法执行的,除非当前线程调用如pthread_yield之类的函数,所以在使用SCHED_FIFO的时候要小心处理相同级别线程的动作。
SCHED_RR
鉴于SCHED_FIFO调度策略的一些缺点,SCHED_RR对SCHED_FIFO做出了一些增强功能。从实质上看,它还是SCHED_FIFO调用策略。它使用最大运行时间来限制当前进程的运行,当运行时间大于等于最大运行时间的时候,当前线程将被切换并放置于相同优先级队列的最后。这样做的好处是其他具有相同级别的线程能在“自私“线程下执行。返回值 0表示设置成功 其他表示设置不成功
#define _MULTI_THREADED #include <pthread.h> #include <sched.h> #include <stdio.h> #include "check.h" #define BUMP_PRIO 1 int thePriority = 0; int showSchedParam(pthread_t thread) { struct sched_param param; int policy; int rc; printf("Get scheduling parameters\n"); rc = pthread_getschedparam(thread, &policy, ¶m); checkResults("pthread_getschedparam()\n", rc); printf("The thread scheduling parameters indicate:\n" "priority = %d\n", param.sched_priority); return param.sched_priority; } void *threadfunc(void *parm) { int rc; printf("Inside secondary thread\n"); thePriority = showSchedParam(pthread_self()); sleep(5); /* Sleep is not a very robust way to serialize threads */ return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc=0; struct sched_param param; int policy = SCHED_OTHER; int theChangedPriority=0; printf("Enter Testcase - %s\n", argv[0]); printf("Create thread using default attributes\n"); rc = pthread_create(&thread, NULL, threadfunc, NULL); checkResults("pthread_create()\n", rc); sleep(2); /* Sleep is not a very robust way to serialize threads */ memset(¶m, 0, sizeof(param)); /* Bump the priority of the thread a small amount */ if (thePriority - BUMP_PRIO >= PRIORITY_MIN_NP) { param.sched_priority = thePriority - BUMP_PRIO; } printf("Set scheduling parameters, prio=%d\n", param.sched_priority); rc = pthread_setschedparam(thread, policy, ¶m); checkResults("pthread_setschedparam()\n", rc); /* Let the thread fill in its own last priority */ theChangedPriority = showSchedParam(thread); if (thePriority == theChangedPriority || param.sched_priority != theChangedPriority) { printf("The thread did not get priority set correctly, " "first=%d last=%d expected=%d\n", thePriority, theChangedPriority, param.sched_priority); exit(1); } sleep(5); /* Sleep is not a very robust way to serialize threads */ printf("Main completed\n"); return 0; }
check.h
#ifndef _CHECK_H
2 #define _CHECK_H
3 /* headers used by a majority of the example program */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <errno.h>
8
9 /* Simple function to check the return code and exit the program
10 if the function call failed
11 */
12 static void checkResults(char *string, int rc) {
13 if (rc) {
14 printf("Error on : %s, rc=%d\n",
15 string, rc);
16 exit(EXIT_FAILURE);
17 }
18 return;
19 }
20
21 #endif
编译 gcc -o pthread_setschedparam -lpthread pthread_setschedparam.c
运行结果
Enter Testcase - ./pthread_setschedparam
Create thread using default attributes
Inside secondary thread
Get scheduling parameters
The thread scheduling parameters indicate:
priority = 0
Set scheduling parameters, prio=0
Get scheduling parameters
The thread scheduling parameters indicate:
priority = 0
The thread did not get priority set correctly, first=0 last=0 expected=0