Ubuntu中SCHED_FIFO的问题

时间:2021-04-27 02:14:48

I have written a small program that creates a pthread and set a SCHED_FIFO policy for the pthread. The code is the next:

我编写了一个小程序,用于创建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;
}

When I execute this code I don't have any error, but inside the thread I check his scheduler policy and I obtain SCHED_OTHER. I don't understand why the SCHED_FIFO is not applied. I check the policy with the next function:

当我执行此代码时,我没有任何错误,但在线程中我检查他的调度程序策略,我获得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");
    }
}

1 个解决方案

#1


1  

I dont see you calling pthread_attr_setinheritsched.

我没有看到你调用pthread_attr_setinheritsched。

Did you tried "pthread_attr_setinheritsched" with PTHREAD_EXPLICIT_SCHED?

你用PTHREAD_EXPLICIT_SCHED试过“pthread_attr_setinheritsched”吗?

#1


1  

I dont see you calling pthread_attr_setinheritsched.

我没有看到你调用pthread_attr_setinheritsched。

Did you tried "pthread_attr_setinheritsched" with PTHREAD_EXPLICIT_SCHED?

你用PTHREAD_EXPLICIT_SCHED试过“pthread_attr_setinheritsched”吗?