Linux semaphore.h 待处理/等待队列策略
Linux semaphore.h pending / waiting quene policy
我正在测试 linux 信号量的未决策略。
Linux 的联机帮助页没有说明未决策略:
http://linux.die.net/man/3/sem_wait,
可能是由调度程序决定的。这意味着在此信号量上挂起的最高优先级线程可以 运行 首先。
因此我创建了 4 个优先级为 10、20、30、40 的 pthread,并将调度程序策略设置为 SCHED_FIFO。
但是,测试结果显示挂起策略是先进先出。
有谁知道有没有semaphore.h等政策信息的文档或源码?
谢谢
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include "stdlib.h"
#include "stdio.h"
sem_t b_sem;
int test_routine(int my_num)
{
usleep(1+ my_num * 1000);
printf("I'm %d, going to wait sem\n", my_num);
sem_wait(&b_sem);
printf("I'm %d, I got the sem\n", my_num);
while(1)
{
sleep(1);
}
}
int main()
{
int i;
int pthrid[4] = {0,0,0,0};
pthread_attr_t attr[4];
struct sched_param prv_priority[4];
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, ¶m);
sem_init(&b_sem, 1, 0);
for( i = 0 ; i < 4 ; i++ )
{
pthread_attr_init(&attr[i]);
pthread_attr_getschedparam(&attr[i], &prv_priority[i]);
pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
prv_priority[i].sched_priority = (10 + i*10);
printf("test start, thread %d has priority %d\n", i ,prv_priority[i].sched_priority);
pthread_attr_setschedparam(&attr[i], &prv_priority[i]);
pthread_create(&pthrid[i], &attr[i], test_routine, i);
}
sleep(1);
for( i = 0 ; i < 4 ; i++ )
{
printf("give sem\n");
sem_post(&b_sem);
sleep(1);
}
sleep(100000);
}
结果:
test start, thread 0 has priority 10
test start, thread 1 has priority 20
test start, thread 2 has priority 30
test start, thread 3 has priority 40
I'm 0, going to wait sem
I'm 1, going to wait sem
I'm 2, going to wait sem
I'm 3, going to wait sem
give sem
I'm 0, I got the sem
give sem
I'm 1, I got the sem
give sem
I'm 2, I got the sem
give sem
I'm 3, I got the sem
你的测试失败了。
默认情况下,新线程会继承创建它们的线程的调度策略。您永远不会覆盖此默认值。在您致电 pthread_create
之前,请执行以下操作:
pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);
此外,您必须 运行 您的程序 root
或以其他方式授予它使用 SCHED_RR
.
的权限
顺便说一下,一个关键线索是使用 ps axlm
检查线程优先级,发现它们都是一样的。
我正在测试 linux 信号量的未决策略。
Linux 的联机帮助页没有说明未决策略: http://linux.die.net/man/3/sem_wait,
可能是由调度程序决定的。这意味着在此信号量上挂起的最高优先级线程可以 运行 首先。
因此我创建了 4 个优先级为 10、20、30、40 的 pthread,并将调度程序策略设置为 SCHED_FIFO。
但是,测试结果显示挂起策略是先进先出。 有谁知道有没有semaphore.h等政策信息的文档或源码?
谢谢
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include "stdlib.h"
#include "stdio.h"
sem_t b_sem;
int test_routine(int my_num)
{
usleep(1+ my_num * 1000);
printf("I'm %d, going to wait sem\n", my_num);
sem_wait(&b_sem);
printf("I'm %d, I got the sem\n", my_num);
while(1)
{
sleep(1);
}
}
int main()
{
int i;
int pthrid[4] = {0,0,0,0};
pthread_attr_t attr[4];
struct sched_param prv_priority[4];
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, ¶m);
sem_init(&b_sem, 1, 0);
for( i = 0 ; i < 4 ; i++ )
{
pthread_attr_init(&attr[i]);
pthread_attr_getschedparam(&attr[i], &prv_priority[i]);
pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
prv_priority[i].sched_priority = (10 + i*10);
printf("test start, thread %d has priority %d\n", i ,prv_priority[i].sched_priority);
pthread_attr_setschedparam(&attr[i], &prv_priority[i]);
pthread_create(&pthrid[i], &attr[i], test_routine, i);
}
sleep(1);
for( i = 0 ; i < 4 ; i++ )
{
printf("give sem\n");
sem_post(&b_sem);
sleep(1);
}
sleep(100000);
}
结果:
test start, thread 0 has priority 10
test start, thread 1 has priority 20
test start, thread 2 has priority 30
test start, thread 3 has priority 40
I'm 0, going to wait sem
I'm 1, going to wait sem
I'm 2, going to wait sem
I'm 3, going to wait sem
give sem
I'm 0, I got the sem
give sem
I'm 1, I got the sem
give sem
I'm 2, I got the sem
give sem
I'm 3, I got the sem
你的测试失败了。
默认情况下,新线程会继承创建它们的线程的调度策略。您永远不会覆盖此默认值。在您致电 pthread_create
之前,请执行以下操作:
pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);
此外,您必须 运行 您的程序 root
或以其他方式授予它使用 SCHED_RR
.
顺便说一下,一个关键线索是使用 ps axlm
检查线程优先级,发现它们都是一样的。