进程内无法实现实时SCHED_FIFO线程

问题描述

大家好!

我试图让第二个线程(串行线程)尽可能接近实时地运行。

在我创建的第二个串行线程中,我在串行端口上 select() 超时为 3 毫秒。

我也可以在 select() 之前获得实时时间……然后在之后获得 select() 增量时间。

问题是,有时我没有得到 select 返回 0 的迹象(我称之为超时,因为 3 毫秒过去了)……但我偶尔会得到比 3 毫秒大得多的总时间(例如 4.447) .

我必须得出结论,串行线程正在被抢占?

有什么想法吗?

我可以使用哪些 Linux 命令来查看线程是否被抢占?

谢谢!

#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 <termios.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.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] = '\0';
}



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,"%s - RLIMIT_RTPRIO: soft=%lld,(long long) proclimit.rlim_max);

  getrlimit(RLIMIT_cpu,"%s - RLIMIT_cpu: soft=%lld,(long long) proclimit.rlim_max);
}



void*   serialThread(void* arg)
{
    int     sfd;    // serial file descriptor.

    char    myChar;

    int     rtn;

    fd_set  myfds;

    struct
    timeval tm_out,start,end,delta;

    struct
    termios oldtio,newtio;

    sfd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY);

    tcgetattr(sfd,&oldtio);

    newtio = oldtio;

    cfmakeraw(&newtio);

    newtio.c_cflag |= CREAD;
    newtio.c_cflag |= CLOCAL;
    newtio.c_cflag &= ~CSTOPB;
    newtio.c_cflag &= ~CRTSCTS;
    newtio.c_cflag &= ~CSIZE;
    newtio.c_cflag |= CS7;
    newtio.c_cflag |= PARENB;
    newtio.c_cflag &= ~PARODD;

    newtio.c_cc[VTIME] = 1;
    newtio.c_cc[VMIN] = 1;

    tcflush(sfd,TCIFLUSH);


    while (1) {
        FD_ZERO(&myfds);
        FD_SET(sfd,&myfds);

        tm_out.tv_sec = 0;
        tm_out.tv_usec = 3000;

        // get sys call start time.
        gettimeofday(&start,NULL);

        rtn = select(sfd+1,&myfds,NULL,&tm_out);

        // get sys call end time.
        gettimeofday(&end,NULL);

        timersub(&end,&start,&delta);

        if (rtn == 0) {
            fprintf(stderr,"tm_out = %02d.%06d    delta = %02d.%06d\n",tm_out.tv_sec,tm_out.tv_usec,delta.tv_sec,delta.tv_usec);
        }
        else
          read(sfd,&myChar,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,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",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",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",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",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",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",strerror(rtn));
  }




  while (myTerminate == 0) {

  }
}

解决方法

知道的人似乎知道为什么这会发生在我身上,但是...... ......经过近一年的挣扎...... ...我发现了一篇帖子,这正是我发生的事情。

我发布了一个链接,所以没有其他人会遭受我的命运。

C - select() seems to block for longer than timeout