user1177187
user1177187

Reputation: 461

Unable to change thread policy to SCHED_FIFO

I have two threads, thread1 and thread2. I created thread1 first and then thread2. But thread2 is scheduled first. I want to schedule thread1 before thread2. i changed the policy of thread1 to SCHED_FIFO and policy of thread2 to SCHED_RR. even after this thread2 scheduled before. Then i declared two threads as SCHED_FIFO asssigned different priorities as shown in the below program. Eventhough there is no change. I thought of checking the policy in threads in that it is returning 0. seems like thread policy is not getting changed.

PLease help me in solving this isssue.

      #include <stdio.h>
      #include <pthread.h>
      #include <sys/types.h>

       pthread_t thread1, thread2;

       void *
       thread1_func (void *i)
       {
       struct sched_param p3;

       int i1 = 0;

       int policy;

       i = pthread_getschedparam (thread1, (int *) &policy, &p3);

       printf ("in thread1 with priority :%u policy: %u\n", p3.sched_priority,
          policy);
     }

      void *
      thread2_func (void *i)
    {
      struct sched_param p3;

      int i1 = 0;

      int policy;

      i1 = pthread_getschedparam (thread1, (int *) &policy, &p3);

      printf ("in thread2 with priority :%u and policy   %u\n",                   p3.sched_priority,
          policy);
    }

    int
    main ()  
    {
     struct sched_param p1, p2;


     pthread_attr_t attr1, attr2;

     pthread_attr_init (&attr1);

     pthread_attr_init (&attr2);

     pthread_attr_setinheritsched (&attr1, PTHREAD_INHERIT_SCHED);

     pthread_attr_setschedpolicy (&attr1, SCHED_FIFO);

     p1.sched_priority = 20;

     pthread_attr_setschedparam (&attr1, &p1);

     //pthread_attr_setschedpolicy(&attr2, SCHED_RR);
     //pthread_attr_setschedpolicy(&attr2, SCHED_RR); 
     /* tried to set the policY of thread1 as "FIFO" and thread2 as "RR" to make thread1 run before thread2 but it is not working*/

    //pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
    //p2.sched_priority = 10;   
    //pthread_attr_setschedparam(&attr2,&p2);

      pthread_create (&thread1, &attr1, (void *) thread1_func, NULL);

      pthread_create (&thread2, NULL, (void *) thread2_func, NULL);

      /*p1.sched_priority = 0;
      int policy=1;
      struct sched_param p4;
      pthread_setschedparam(thread1,(int *)&policy,&p4);
      pthread_getschedparam(thread1,&policy,&p4);
      printf("the pri::::thread1 %d policy %d\n",p4.sched_priority,policy);
      */

      pthread_join (thread1, NULL);

      pthread_join (thread2, NULL);

      return 0;
     }

Upvotes: 1

Views: 5569

Answers (3)

skytree
skytree

Reputation: 1100

Maybe you should run with root after sudo su

Upvotes: 0

kuga
kuga

Reputation: 1775

The problem is:

 pthread_attr_setinheritsched (&attr1, PTHREAD_INHERIT_SCHED);
  The following values may be specified in inheritsched:

  PTHREAD_INHERIT_SCHED
         Threads that are created using attr inherit scheduling
         attributes from the creating thread; the scheduling attributes
         in attr are ignored.

  PTHREAD_EXPLICIT_SCHED
         Threads that are created using attr take their scheduling
         attributes from the values specified by the attributes object.

see http://man7.org/linux/man-pages/man3/pthread_attr_setinheritsched.3.html

Upvotes: 2

alk
alk

Reputation: 70981

If you'd check the result of this line

  pthread_create (&thread1, &attr1, (void *) thread1_func, NULL);

you'd notice that the call returned EPERM.

From pthread_Create()s documentation:

ERRORS

The pthread_create() function shall fail if:

[...]

[EPERM] The caller does not have appropriate privileges to set the required scheduling parameters or scheduling policy.


Also you might like to have a look at this questions along with its answers: Getting EPERM when calling pthread_create() for SCHED_FIFO thread as root on Linux

Upvotes: 0

Related Questions