avarana
avarana

Reputation: 91

Problems with SCHED_FIFO in Ubuntu

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

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:

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");
    }
}

Upvotes: 0

Views: 1202

Answers (1)

Sasi V
Sasi V

Reputation: 1094

I dont see you calling pthread_attr_setinheritsched.

Did you tried "pthread_attr_setinheritsched" with PTHREAD_EXPLICIT_SCHED?

Upvotes: 1

Related Questions