当线程具有相同优先级时,对 SCHED_FIFO 策略的 theads 调度感到困惑

标签 c multithreading linux-kernel

我在主线程中创建了两个线程ThreadA和ThreadB,并且三个线程具有相同的优先级。调用顺序是ThreadA在ThreadB前面(我看不懂)。
但是,当增加主线程优先级时,调用顺序是ThreadB在ThreadA之前(我的理解)。 我从手册页收到此消息,如下所示: 调用 sched_setscheduler(2)、sched_setparam(2) 或 sched_setattr(2) 将把 pid 标识的 SCHED_FIFO(或 SCHED_RR)线程放在 列表的开头(如果它是可运行的)。结果,它可能会抢先 当前正在运行的线程(如果具有相同的优先级)。 (POSIX.1 指定线程应该转到列表的末尾。)

代码如下:

/*
 * critical.c
 *
 * compile with  gcc critical.c -o critical -lrt -lpthread
 *
 *  result is aaaaabbbbb when three thread have same priority 
 *  result is bbbbbaaaaa when main thread has higher priority 
 */

#include <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* header file for standard input/outputlibrary */
#define _REENTRANT      /* macro to ensure system calls are reentrant */

void *threadA(void *);   /* predefine threadA routine */
void *threadB(void *);   /* predefine threadB routine */

pthread_t threadA_id,threadB_id,main_id;   /* thread identifiers */
pthread_attr_t attrA,attrB;        /* thread attribute structures */
struct sched_param param;          /* scheduling structure for thread attributes */

int policy=SCHED_FIFO;
int priority_min,priority_max;     /* for range of priority levels */

/* main routine */
int main()
{
  struct timespec start;
  int status;                        /* check that system calls return ok */

  clock_gettime(CLOCK_REALTIME, &start);        /* get the time   */
  printf("Start time is: %d seconds %d nano_seconds\n",start.tv_sec,start.tv_nsec);

  /* Set processor affinity */
  unsigned long mask = 1; /* use only 1 CPU core */
  unsigned int len = sizeof(mask);
  status = sched_setaffinity(0, len, &mask);
  if (status < 0) perror("sched_setaffinity");
  status = sched_getaffinity(0, len, &mask);
  if (status < 0) perror("sched_getaffinity");

  /* Find priority limits */

  priority_max = sched_get_priority_max(policy);
  priority_min = sched_get_priority_min(policy);

  /* Change priority and policy of main thread */

  main_id = pthread_self();
  param.sched_priority=priority_min;
  status = pthread_setschedparam(main_id, policy, &param);
  if (status != 0) perror("pthread_setschedparam"); /* error check */

  /* Create threadA */

  param.sched_priority = priority_min;
  pthread_attr_init(&attrA);
  status = pthread_attr_setschedpolicy(&attrA,policy);
  if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */
  status = pthread_attr_setschedparam(&attrA,&param);
  if (status != 0) perror("pthread_attr_setschedparam"); /* error check */
  status = pthread_create(&threadA_id, &attrA, threadA, NULL);
  if (status != 0) perror("pthread_create"); /* error check */
  status = pthread_setschedparam(threadA_id,policy,&param);
  if (status != 0) perror("pthread_setschedparam");

  /* Create threadB */
  param.sched_priority = priority_min; /* so that B runs with a higher priority than A */
  pthread_attr_init(&attrB);
  status = pthread_attr_setschedpolicy(&attrB,policy);
  if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */
  status = pthread_attr_setschedparam(&attrB,&param);
  if (status != 0) perror("pthread_attr_setschedparam"); /* error check */
  status = pthread_create(&threadB_id, &attrB, threadB, NULL);
  if (status != 0) perror("pthread_create"); /* error check */
  status = pthread_setschedparam(threadB_id,policy,&param);
  if (status != 0) perror("pthread_setschedparam");

  /* Join threads - force main to wait for the thread to terminate */
  printf("main() waiting for threads\n");

  status = pthread_join(threadA_id, NULL);
  if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */
  status = pthread_join(threadB_id, NULL);
  if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */

  printf("\nmain() reporting that all threads have terminated\n");
  return(0);

}  /* end of main */

void *threadA(void *arg)
{
  int j;
  for(j=1;j<=5;j++){
    printf("a");                    
  }
  return (NULL);
}

void *threadB(void *arg)
{
  int j;
  for(j=1;j<=5;j++){
    printf("b");                     
  }
  return (NULL);
}

最佳答案

示例是创建一个优先级为 1、采用 FIFO 调度的线程:

int retVal;
pthread_attr_t attr;
struct sched_param schedParam;
pthread_t thread;

retVal = pthread_attr_init(&attr);
if (retVal)
{
    fprintf(stderr, "pthread_attr_init error %d\n", retVal);
    exit(1);
}

retVal = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (retVal)
{
    fprintf(stderr, "pthread_attr_setinheritsched error %d\n", retVal);
    exit(1);
}

retVal = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
if (retVal)
{
    fprintf(stderr, "pthread_attr_setschedpolicy error %d\n", retVal);
    exit(1);
}

schedParam.sched_priority = 1;
retVal = pthread_attr_setschedparam(&attr, &schedParam);
if (retVal)
{
    fprintf(stderr, "pthread_attr_setschedparam error %d\n", retVal);
    exit(1);
}

retVal = pthread_create(&thread,
                        &attr,
                        _Thread,
                        NULL);    
if (retVal)
{
    fprintf(stderr, "pthread_create error %d\n", retVal);
    exit(1);
}

如果您的实时优先级软限制过于严格。

在运行代码之前,在 shell 中调用 ulimit -r unlimited 。或者直接在代码中调用 setrlimit(RLIMIT_RTPRIO, ...)

系统范围的限制在/etc/security/limits.conf中设置。

关于当线程具有相同优先级时,对 SCHED_FIFO 策略的 theads 调度感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967909/

相关文章:

java - 如何从 jar 文件创建 C 库?

.net - 为什么 Thread.Sleep(0) 可以解决我的问题,以及如何避免它?

Linux 设备驱动程序使用的次要编号

c++ - 设计可扩展的多线程应用程序

c - 打印网络数据包中的用户数据

linux - 哪些系统调用不会被信号中断?

c - 请求提示 : Possibilities to log files from a router to a server

c - select 中可以使用相同的 fd 进行读写

c - 使用 lccwin64 编译器链接到 GSL 库

c# - 如何在 Application_BeginRequest 中执行击中网络服务器的第一个请求的部分代码?