c - Xenomai 中的周期性线程实时失败

标签 c linux linux-kernel posix xenomai

我正在创建一个周期线程,它在模拟输出上输出方波信号。我正在使用来自 Xenomai API 的 Posix Skin 和 Analogy。

我使用示波器测试了我的代码的实时性能,并查看了方波信号(频率为 1kHz)的延迟。我应该实现 <100us 延迟。但是,信号会受到常见中断信号的强烈干扰(>250us 延迟),例如移动鼠标、启动新程序等。

我的 makefile 中的标志是这样设置的:

gcc  -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix    
main_posix.c -Xlinker -rpath -Xlinker /usr/xenomai/lib -Wl,@/usr/xenomai/lib/posix.wrappers 
-L/usr/xenomai/lib -lpthread_rt -lxenomai -lpthread -lrt  -lanalogy -lrtdm -o main_posix

这是代码:

#define PERIOD 1e6 
#define FILENAME "analogy0"
#define ANALOG_SUBD 1
#define CHANNEL 0
#define SCAN_SIZE 2
#define DELAI 5

static char *filename = FILENAME;
static int idx_subd = ANALOG_SUBD;
static int idx_chan = CHANNEL;
static int valueUp = 450000;
static int valueDown = 98500;

void *TaskCode(void *arg)
{
   unsigned char sgnl = 0;
   unsigned long overruns_r = 0;

   a4l_desc_t dsc = { .sbdata = NULL };
   a4l_chinfo_t *chinfo;
   int err = 0;
   unsigned int scan_size = SCAN_SIZE;

   err = a4l_open(&dsc, filename);
   if (err < 0) {
        fprintf(stderr,
            "insn_write: a4l_open %s failed (err=%d)\n",
            filename, err);
        return NULL;
   }    

   while(1) {
        pthread_wait_np( &overruns_r );
    if(sgnl)
        err = a4l_sync_write(&dsc,
                 idx_subd, CHAN(idx_chan), 0, &valueUp, scan_size);
    else
        err = a4l_sync_write(&dsc,
                 idx_subd, CHAN(idx_chan), 0, &valueDown, scan_size);
    if (err < 0) {
        fprintf(stderr,
            "insn_write: a4l_sync_write failed (err=%d)\n", err);
        goto out_insn_write;
    }

    sgnl = (sgnl + 1) % 2;
   }

   out_insn_write:

    if (dsc.sbdata != NULL)
        free(dsc.sbdata);

    a4l_close(&dsc);

    return NULL;

}

int main(void)
{
   mlockall( MCL_CURRENT | MCL_FUTURE );

   pthread_t thread;
   int rc, i;
   int prio = 99;
   struct timespec rqtp, rmtp;
   rqtp.tv_sec = 0;
   rqtp.tv_nsec = PERIOD;

   struct sched_param sparam;
   sparam.sched_priority = 99;

   rc = pthread_create(&thread, NULL, TaskCode, NULL);
   assert(0 == rc);

   rc = pthread_setschedparam(&thread, SCHED_FIFO, &sparam);
   assert(0 == rc);

   rc = clock_gettime( CLOCK_REALTIME, &rmtp );
   assert(0 == rc);
   rmtp.tv_sec = rmtp.tv_sec + DELAI;   

   rc = pthread_make_periodic_np(thread, &rmtp, &rqtp);
   if(rc == ETIMEDOUT) printf("Début dépassé \n");
   else if(rc == ESRCH) printf("Thread invalide \n");
   assert(0 == rc);

   rc = pthread_join(thread, NULL);

   exit(EXIT_SUCCESS);
}

我强烈怀疑(通过查看 Xenomai 调度程序)我的程序以某种方式进入了辅助模式。我试图删除“断言”语句以及相关的 printf,但这并不成功。知道如何解决吗?

最佳答案

一如既往,细节决定成败。

我在 gcc 中启用了 -Wall 选项,它显示了所有警告。结果是 pthread_* 的 header 没有正确加载,这使我无法看到 pthread_setschedparam 的第一个参数是错误的,应该是线程而不是 &thread。

关于c - Xenomai 中的周期性线程实时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16685764/

相关文章:

c - Linux中CPU如何切换到中断栈

linux - "Unknown symbol in module"在模块插入时尽管有 EXPORT_SYMBOL

python - 如何在 C 中使用 pyarg_parsetuple 设置多个参数?

c - 使用 MinGW 的 CodeBlocks 中 strcpy 函数的异常行为

c - Linux Kernel Module程序从IP获取域名

linux - 在登录时执行 bash 并添加别名

C 编程结构访问

linux - sigreturn 如何在 SECCOMP_SET_MODE_STRICT 中阻止除 SIGKILL 和 SIGSTOP 之外的所有信号?

Android:meminfo_proc_show() 3.10.65+ 尝试执行 LOS12.1 端口时启动循环内核崩溃

无法使用 C 中的 read() 函数读取