无法将线程策略更改为SCHED_FIFO

时间:2015-07-14 11:02:16

标签: c pthreads scheduling

我有两个线程,thread1和thread2。我首先创建了thread1然后创建了thread2。但首先安排thread2。我想在thread2之前安排thread1。我将thread1的策略更改为SCHED_FIFO,将thread2的策略更改为SCHED_RR。即使在此之前安排了这个thread2。然后我声明了两个线程,因为SCHED_FIFO分配了不同的优先级,如下面的程序所示。尽管没有变化。我想在线程中检查策略,它返回0.似乎线程策略没有改变。

请帮我解决这个问题。

  #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但它不起作用* /

//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;

 }

3 个答案:

答案 0 :(得分:1)

问题是:

 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.

请参阅http://man7.org/linux/man-pages/man3/pthread_attr_setinheritsched.3.html

答案 1 :(得分:0)

如果您要检查此行的结果

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

您注意到该呼叫已返回EPERM

来自pthread_Create()s documentation

  

<强> 错误

     

pthread_create()函数在以下情况下将失败:

     

[...]

     

[EPERM]           调用者没有适当的权限来设置所需的调度参数或调度策略。

此外,您可能希望了解这些问题及其答案:Getting EPERM when calling pthread_create() for SCHED_FIFO thread as root on Linux

答案 2 :(得分:0)

也许您应该在sudo su

之后使用root运行