xenomai线程测试

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

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

    点击查看代码
    //main.c
    #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
    
    posted @   相对维度  阅读(176)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
    · 地球OL攻略 —— 某应届生求职总结
    · 周边上新:园子的第一款马克杯温暖上架
    · 提示词工程——AI应用必不可少的技术
    · Open-Sora 2.0 重磅开源!
    点击右上角即可分享
    微信分享提示