pthread_attr_setschedparam() returns 非零但 errno = 成功 ... (Linux, C, Ubuntu 18.04.4)

pthread_attr_setschedparam() returns non zero but errno = success ... (Linux, C, Ubuntu 18.04.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>



char      strNowTime[26];



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

  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;
  int               myErr;

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


  //-----------------------------------------------
  // setup the pthread attributes struct.
  if ((serialAttr = malloc(sizeof(pthread_attr_t))) == NULL) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_t malloc()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
  }
  memset(serialAttr, 0, sizeof(pthread_attr_t));

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

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

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

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

  //-----------------------------------------------
  // start the new thread.
  rtn = pthread_create(&serialThdID, serialAttr, serialThread, NULL);
  myErr = errno;
  if(rtn  == 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(myErr));
    exit(EXIT_FAILURE);
  }

  //-----------------------------------------------
  // no need to keep this junk if pthread_create() succeeded.
  if (pthread_attr_destroy(serialAttr) != 0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_destroy()\n%s\n", strNowTime, strerror(myErr));
  }
  // research proves this is needed if we malloc'ed!
  free(serialAttr);



  while (myTerminate == 0) {

  }
}

明确记录,pthread_attr_setschedparam return失败时的错误代码。它不设置 errno。 (从技术上讲,就像允许的大多数其他函数一样,允许使用任何非零值来破坏 errno ,但这不是在提供有意义的值的意义上“设置”它。)您需要保存 return 值和使用那个,而不是 errno 作为要报告的错误代码。

pthread_attr_setschedpolicy 不设置 errno。来自 manual:

RETURN VALUE

On success, these functions return 0; on error, they return a nonzero error number.

也就是说,错误编号在 return 值本身中,而不是在 errno 中。

事实上,none 的 pthread 函数集 errno。有关在 pthread 函数失败时如何调用 perror 的示例,请参阅 pthread_create 手册。它实际上首先手动设置errno

#define handle_error_en(en, msg) \
           do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)