了解Xenomai-Linux POSIX皮肤中的上下文切换

问题描述 投票:2回答:1

我正在使用Xenomai在BeagleBone Black上运行RT程序,并试图弄清楚如何监视/理解上下文切换(我知道上下文切换的概念),以便可以确定何时(在使用POSIX皮肤的C语言中)切换程序。从主要和次要模式。

这里是程序main_posix.c

#ifndef __XENO_SIM__
#ifndef __KERNEL__
#include <stdio.h>
#define xnarch_printf printf
#endif

#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <pthread.h>
#include <mqueue.h>
#else /* __XENO_SIM */
#include <posix/posix.h>
#endif /* __XENO_SIM */

void warn_upon_switch(){

    printf("Switched to Secondary Mode \n");


}


void *threadFunc(void *arg)
{
    char *str;
    int i = 0;
    struct timespec delay, sleep;
    unsigned long over;
    int ret;

    str=(char*)arg;

    printf("In thread \n");

    sleep.tv_sec = 1;
    sleep.tv_nsec = 0;

    #ifdef __XENO__

        ret = pthread_set_mode_np(0,  0x00040000);

        printf("Warn Bit Ret %d\n", ret);

    #endif /* __XENO__ */

    // run this for some arbitrary time
    while(i < 110000000 )
    {
        clock_nanosleep(CLOCK_REALTIME, 0, &sleep, NULL);
        printf("threadFunc says: %s\n",str);
        ++i;
    } 

    return NULL;
}

int main(void)
{


    signal(SIGXCPU, warn_upon_switch);
    signal(SIGKILL, warn_upon_switch);



    pthread_t pth;  

    double i = 0;

    int ret;

    pthread_attr_t tattr;   

    struct sched_param sparam;
    sparam.sched_priority = 99;


    ret = pthread_attr_init(&tattr);
    printf("Init Return Val %d\n", ret);

    ret = pthread_setschedparam(pth,SCHED_FIFO, &sparam);
    printf("SetSchedParam Ret Value %d\n", ret);

    pthread_create(&pth,&tattr,threadFunc,"foo");

    printf("main waiting for thread to terminate...\n");
    pthread_join(pth,NULL);

    return 0;
}

我也在通过/proc/xenomai/stat连续监视watch

enter image description here

[我看到CSW MSWPID3323连续变化。

这是ps -e -o class,rtprio,pri,nice,cmd | grep ./main_posix 的输出

enter image description here

输出如下

enter image description here

我的问题如下

  1. 我怎么知道我的程序是在主要还是次要模式下运行?
  2. 我以ret = pthread_setschedparam(pth,SCHED_FIFO, &sparam);的形式返回16的返回值,即EBUSY。知道为什么吗?
  3. 尝试使用signal(SIGXCPU, warn_upon_switch);捕捉开关信号。该函数永远不会被调用。
  4. 如果该程序可以在Linux上看到(这意味着它通过Linux内核获取PID),是否意味着它以辅助模式运行?
  5. proc/xenomai/stat中,我看到同一程序有两个进程。是main和线程吗?

[这是我使用的一些资源

  1. Periodic thread fails real-time in Xenomai
  2. Xenomai clock_nanosleep in POSIX skin jumps to Linux Kernel
  3. http://xenomai.org/2014/08/porting-a-linux-application-to-xenomai-dual-kernel/#Using_the_PTHREAD_WARNSW_bit
  4. http://www.xenomai.org/documentation/xenomai-2.6/html/api/sigxcpu_8c-example.html
c linux posix beagleboneblack xenomai
1个回答
0
投票

我也想知道答案。

© www.soinside.com 2019 - 2024. All rights reserved.