gpt4 book ai didi

linux - Ubuntu 中 SCHED_FIFO 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:11:40 26 4
gpt4 key购买 nike

我编写了一个创建 pthread 并为 pthread 设置 SCHED_FIFO 策略的小程序。代码如下:

int main(int argc, char *argv[]){

...

pthread_attr_t attr;
pthread_attr_init(&attr);
retVal = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
if(retVal){
fprintf(stderr, "pthread_attr_setschedpolicy error %d\n", retVal);
}
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);

if (retVal)
{
fprintf(stderr, "pthread_attr_setinheritsched error %d\n", retVal);
exit(1);
}
retVal = pthread_attr_setschedparam(&attr, &param);
if (retVal)
{
fprintf(stderr, "pthread_attr_setschedparam error %d\n", retVal);
exit(1);
}

err = pthread_create(&tid[i], &attr, mesaureTime, (void *) cpu_pointer);


if (err != 0)
printf("can't create thread :[%s]", strerror(err));
else
printf("Success\n");
pthread_join(tid[0], NULL);
printf("\n Finish\n");
return 0;
}

当我执行这段代码时我没有任何错误,但在线程内部我检查了他的调度程序策略并获得了 SCHED_OTHER。我不明白为什么不应用 SCHED_FIFO。我用下一个函数检查策略:

void get_thread_policy() {
int ret;

// We'll operate on the currently running thread.
pthread_t this_thread = pthread_self();
// struct sched_param is used to store the scheduling priority
struct sched_param params;
int policy = 0;
ret = pthread_getschedparam(this_thread, &policy, &params);
if (ret != 0) {
printf("Problems with the params\n");
return;
}
// Check the correct policy was applied
if(policy == SCHED_FIFO) {
printf("La política de planificación es SCHED_FIFO\n");
} else if (policy == SCHED_OTHER){
printf("La política de planificación es SCHED_OTHER\n");
}
}

最佳答案

我没看到你在调用 pthread_attr_setinheritsched。

您是否尝试过使用 PTHREAD_EXPLICIT_SCHED 执行“pthread_attr_setinheritsched”?

关于linux - Ubuntu 中 SCHED_FIFO 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22833630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com