pthread_attr_setschedparam() returns 无效参数…… (Linux, C, Ubuntu 18.04.4)

pthread_attr_setschedparam() returns invalid argument … (Linux, C, Ubuntu 18.04.4)

大家好!

我无法通过代码中的这一点。 我怀疑这是 OS 资源限制方面的问题?

我看到消息说 RTPRIO 0 最低,99 最高。 ...和其他人持相反意见。

我似乎找不到关于内核在进程中获得近实时线程的位置的好参考。

我曾希望有一个 4 核处理器,其中一个核专用于下面代码中的串行线程。 此外,当这个串行线程进行系统调用时......专用核心将任务切换到这个系统调用。

任何建议和帮助将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>



//-----------------------------------
// signal Handler stuff.
//-----------------------------------
static
struct  sigaction mySigActTerm;

volatile
int     myTerminate = 0;

void terminateHandler(int signum, siginfo_t *info, void *ptr)
{
  // set a flag here and get out.
  myTerminate = 1;
}



void getNowTime(char* str)
{
  time_t    rawtime;
  time(&rawtime);
  ctime_r(&rawtime, str);

  // clobber the unwanted newline.
  str[24] = '[=14=]';
}



void myResLimit()
{
  struct
  rlimit    procLimit;

  char      strNowTime[26];

  getrlimit(RLIMIT_RTTIME, &procLimit);
  getNowTime(strNowTime);
  fprintf(stderr, "%s - RLIMIT_RTTIME: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long)procLimit.rlim_max);

  getrlimit(RLIMIT_RTPRIO, &procLimit);
  getNowTime(strNowTime);
  fprintf(stderr, "%s - RLIMIT_RTPRIO: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);

  getrlimit(RLIMIT_CPU, &procLimit);
  getNowTime(strNowTime);
  fprintf(stderr, "%s - RLIMIT_CPU: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);
}



void*   serialThread(void* arg)
{
    while (1) {
        
    }
}



//-----------------------------------
// the one and only MAIN.
//-----------------------------------
int main()
{
  //-----------------------------------------------
  // locals.
  int               rtn;

  char              strNowTime[26];

  pthread_t         serialThdID;

  pthread_attr_t    serialAttr;

  struct
  sched_param       serialParam;


  //-----------------------------------------------
  // Log OS resource limits.
  myResLimit();

  //-----------------------------------------------
  // initialize the signals struct.
  // ... and setup signals.
  memset(&mySigActTerm, 0, sizeof(mySigActTerm));
  mySigActTerm.sa_sigaction = terminateHandler;
  mySigActTerm.sa_flags = SA_SIGINFO;

  sigaction(SIGTERM, &mySigActTerm, NULL);


  //-----------------------------------------------
  // set initial default pthread attr values.
  if ((rtn = pthread_attr_init(&serialAttr)) != 0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_init()\n%s\n", strNowTime, strerror(rtn));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // set for best near real time policy.
  if ((rtn = pthread_attr_setschedpolicy(&serialAttr, SCHED_FIFO)) !=0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setschedpolicy()\n%s\n", strNowTime, strerror(rtn));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // set to explicit inherit or attr obj will be ignored.
  if ((rtn = pthread_attr_setinheritsched(&serialAttr, PTHREAD_EXPLICIT_SCHED)) !=0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setinheritsched()\n%s\n", strNowTime, strerror(rtn));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // set to un-limited thread priority.
  serialParam.sched_priority = 0;
  if ((rtn = pthread_attr_setschedparam(&serialAttr, &serialParam)) !=0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setschedparam()\n%s\n", strNowTime, strerror(rtn));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // start the new thread.
  if ((rtn = pthread_create(&serialThdID, &serialAttr, serialThread, NULL)) == 0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - starting serial thread.\n", strNowTime);
  }
  else {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_create() returned %d\n%s\n", strNowTime, rtn, strerror(rtn));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // no need to keep this junk if pthread_create() succeeded.
  if ((rtn = pthread_attr_destroy(&serialAttr)) != 0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_destroy()\n%s\n", strNowTime, strerror(rtn));
  }




  while (myTerminate == 0) {

  }
}

输出为:

您正在尝试将调度优先级 0 与无效的 SCHED_FIFO 调度策略一起使用。

您正在使用 pthread_attr_setschedpolicy 将调度策略设置为 SCHED_FIFO。该函数的手册页指出:

The supported values for policy are SCHED_FIFO, SCHED_RR, and SCHED_OTHER, with the semantics described in sched_setscheduler(2).

sched_setscheduler 的手册页进一步说明了 SCHED_FIFO 的以下内容:

SCHED_FIFO can be used only with static priorities higher than 0, which means that when a SCHED_FIFO processes becomes runnable, it will always immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE process. SCHED_FIFO is a simple scheduling algorithm without time slicing.

因此 serialParam.sched_priority 使用大于 0 的值即可。