0

我已经安装了带有 rt 补丁的 Linux 内核 4.6,在 uname -a 之后它显示 rt 和 PREEMPT 但我必须通过编写应用程序来证明,所以我有如下想法。我会让睡眠五毫秒唤醒并检查硬件时间并存储在一个数组中,现在我只检查硬件时间的差异,如果它是实时的,它应该是相同的,现在执行大约 50 次。所以我需要知道它是否正确的方法和实施它的技巧。提前感谢任何帮助都会很棒。

这是我的代码 /* 使用“gcc -o swave swave.c -lrt -Wall”编译 */

    #
    include < stdlib.h > #include < stdio.h > #include < time.h > #include < sched.h > #include < sys / io.h >

      #define PORT 0x378# define NSEC_PER_SEC 1000000000

    /* using clock_nanosleep of librt */
    extern int clock_nanosleep(clockid_t __clock_id, int __flags,
      __const struct timespec * __req,
      struct timespec * __rem);

    /* the struct timespec consists of nanoseconds
     * and seconds. if the nanoseconds are getting
     * bigger than 1000000000 (= 1 second) the
     * variable containing seconds has to be
     * incremented and the nanoseconds decremented
     * by 1000000000.
     */
    static inline void tsnorm(struct timespec * ts) {
      while (ts - > tv_nsec >= NSEC_PER_SEC) {
        ts - > tv_nsec -= NSEC_PER_SEC;
        ts - > tv_sec++;
      }
    }

    /* increment counter and write to parallelport */
    void out() {
      static unsigned char state = 0;
      outb(state++, PORT);
    }

    int main(int argc, char * * argv) {
      struct timespec t;
      struct sched_param param;
      /* default interval = 50000ns = 50us
       * cycle duration = 100us
       */
      long array[500001], diff[500001];
      int n = 500000;
      int interval = 50000;

      /* set permissions of parallelport */
      ioperm(PORT, 1, 1);

      if (argc >= 2 && atoi(argv[1]) > 0) {
        printf("using realtime, priority: %d\n", atoi(argv[1]));
        param.sched_priority = atoi(argv[1]);
        /* enable realtime fifo scheduling */
        if (sched_setscheduler(0, SCHED_FIFO, & param) == -1) {
          perror("sched_setscheduler failed");
          exit(-1);
        }
      }
      if (argc >= 3)
        interval = atoi(argv[2]);

      /* get current time */
      clock_gettime(0, & t);
      /* start after one second */
      t.tv_sec++;
      while (n) {
        /* wait untill next shot */
        clock_nanosleep(0, TIMER_ABSTIME, & t, NULL);
        /* do the stuff */
        array[n] = t.tv_nsec;
        /* calculate next shot */
        t.tv_nsec += interval;
        tsnorm( & t);
        n--;
      }
      printf("hell\n\n");
      n = 500000;

      while (array[n]) {
        diff[n] = array[n - 1] - array[n];
        if (diff[n] != 50000) {
          printf("%ld n:%d ", diff[n], n);
        }
        n--;
      }
      return 0;
    }
4

0 回答 0