[xenomai] 线程测试

// 启动多个xenomai进程,并通过top指令查看各个xn进程的切换数据,启动程序时增加一个task的优先级,默认是最高的99

//通过watch  cat /proc/xenomai/sched/stat 动态查看 MSW(模式切换次数) 和 CSW(上下文切换次数)

#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/timer.h>
#include <rtdk.h>
#define TASK_PRIO  99 /* Highest RT priority */
#define TASK_MODE  0  /* No flags */
#define TASK_STKSZ 0  /* Stack size (use default one) */
RT_TASK task_desc;
static int flag_run=1;
static int priority = 99;

void task_process (void *cookie)
{
    int err;
    int acc,time=0;
    err=rt_task_set_periodic(NULL,TM_NOW,1000000ULL); //1ms
    acc = 0;
    while(flag_run) {
        rt_task_wait_period(NULL);
        if(acc++>1000){
          acc = 0;
          time++;
          rt_printf("--------> tims is %d\n",time);
        }
     
    }
}
void signal_handler(int sig)
{
        flag_run=0;
        printf("---> get sigal = %d \n", sig );
}
 
void cleanup ()
{
    rt_task_delete(&task_desc);
}
int main (int argc, char *argv[])
{
    char stderr[100]={0};
    int err,ret;
    rt_print_auto_init(1);
    //catch the signal in waiting...
    signal(SIGTERM,signal_handler);
   	signal(SIGINT,signal_handler);  //Ctrl+C =2
   	signal(SIGQUIT,signal_handler);  //Ctrl+\ =3
   	signal(SIGTSTP,signal_handler); //Ctrl+z =20 
    mlockall(MCL_CURRENT|MCL_FUTURE);
    if(argc>1){
        priority =atoi(argv[1]);
        if(priority<0 || priority>99 )
            priority = 99 ;
    }
    pid_t pid = getpid();
    pthread_t tid = pthread_self();
    printf("----------start!----------------\n");
    printf("xenomai_main: pid=%d, tid=%lu , priority = %d \n", pid, tid , priority);
    ret = rt_task_create(&task_desc, "my_task", 0, priority, T_FPU);
    if (ret < 0) {
        fprintf(stderr, "Failed to create task: %s\n", strerror(-ret));
        return -1;
    }
 
    printf("Starting my_task...\n");
    ret = rt_task_start(&task_desc, &task_process, NULL);
    if (ret < 0) {
        fprintf(stderr, "Failed to start task: %s\n", strerror(-ret));
        return -1;
    }
   while(flag_run)
   {
      sched_yield(); //yield cpu
   }
    cleanup();
    printf("------------end!--------------\n");
    return 0;
 
}
                                                                                       
 

 

 

#############Makefile (目标文件名main.c)#################################

 

###### CONFIGURATION ######

### List of applications to be build
APPLICATIONS = main

### Note: to override the search path for the xeno-config script, use "make XENO=..."

###### USER SPACE BUILD (no change required normally) ######
ifneq ($(APPLICATIONS),)

### Default Xenomai installation path
XENO ?= /usr/xenomai

XENOCONFIG=$(shell PATH=$(XENO):$(XENO)/bin:$(PATH) which xeno-config 2>/dev/null)
	
### Sanity check
ifeq ($(XENOCONFIG),)
all::
        @echo ">>> Invoke make like this: \"make XENO=/path/to/xeno-config\" <<<"
        @echo "==========Finish-0==========================="
endif


CC=$(shell $(XENOCONFIG) --cc) 
CFLAGS=$(shell $(XENOCONFIG) --skin=native --cflags) $(MY_CFLAGS)

LDFLAGS=$(MY_LDFLAGS) 
LDLIBS=$(shell $(XENOCONFIG) --skin=native --ldflags) \
        $(shell $(XENOCONFIG) --skin=rtdm --ldflags --no-auto-init)

# This includes the library path of given Xenomai into the binary to make live
# easier for beginners if Xenomai's libs are not in any default search path.
LDFLAGS+=-Xlinker -rpath -Xlinker $(shell $(XENOCONFIG) --libdir) 
	

all:: $(APPLICATIONS)
	@echo "==========Finish-1==========================="

clean::
	$(RM) $(APPLICATIONS) *.o

        
endif

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值