c - 无法将线程策略更改为 SCHED_FIFO

标签 c pthreads scheduling

我有两个线程,线程 1 和线程 2。我先创建thread1,然后创建thread2。但是thread2是先调度的。我想在线程 2 之前安排线程 1。我将线程 1 的策略更改为 SCHED_FIFO,将线程 2 的策略更改为 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 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;
     }

最佳答案

问题是:

 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

关于c - 无法将线程策略更改为 SCHED_FIFO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404770/

相关文章:

c - FFTW3 2D,传递 fftw_complex 数组

c - 大家好,我在下面的程序中遇到段错误,但我无法弄清楚

创建双链表问题

c++ - 按执行顺序创建pthread

c - 为用户空间线程库编写调度程序

c - C 中 SLL 的 Delete(node ) 的正确语法是什么?

linux - Centos 7 与 PHP 7.2 Pthreads 无法加载 redis.so

linux - 我是否需要做任何特别的事情才能使用 NPTL(相对于 pthreads)?

java - @Schedules spring boot 错误

c++ - 使 Thread 本地队列带有计数器