我正在使用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
[我看到CSW
MSW
的PID
和3323
连续变化。
这是ps -e -o class,rtprio,pri,nice,cmd | grep ./main_posix
的输出
输出如下
我的问题如下
ret = pthread_setschedparam(pth,SCHED_FIFO, &sparam);
的形式返回16
的返回值,即EBUSY
。知道为什么吗?signal(SIGXCPU, warn_upon_switch);
捕捉开关信号。该函数永远不会被调用。 proc/xenomai/stat
中,我看到同一程序有两个进程。是main
和线程吗?[这是我使用的一些资源
我也想知道答案。