📄 test.c
字号:
/* * POSIX.1 Signals test program * * Written by J. Vidal * Copyright (C) Dec, 2002 OCERA Consortium. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2. * * Test that the execution of a signal handler * can be interrumpted by other higher priority threads * */#include <rtl.h>#include <pthread.h>#define NTASKS 3#define ITERS 1000*1000static pthread_t thread[NTASKS];static int waste_time(int param){ int j,k,l,m=0; // Consume time for (j=0;j<=param;j++) for (k=0;k<10;k++) for (l=0;l<100;l++) m=m+j-k+l; return j;}static void signal_handler(int signal){ int ret=0; rtl_printf("Signal handler called for signal %d\n",signal); rtl_printf("This signal handler just wastes time\n"); ret=waste_time(signal*100); if (ret>=(signal*100)) rtl_printf("Signal handler for signal %d about to end \n",signal); }static void *hight_prio_routine(void *arg){ int i=0; struct sched_param p; p.sched_priority=100; pthread_setschedparam (pthread_self(), SCHED_FIFO, &p); pthread_make_periodic_np (pthread_self(), gethrtime(),1*1000*1000); while(i++<ITERS){ rtl_printf("I don't want to waste time waiting for signal handlers\n"); pthread_wait_np(); } return 0;}static void *start_routine(void *arg){ struct sigaction sa; struct sched_param p; int param,signal,err,i=0; long long period=0; param=(unsigned) arg; p . sched_priority = param; pthread_setschedparam (pthread_self(), SCHED_FIFO, &p); rtl_printf("I'm here; my arg is %x\n", param); period=(long long)param*1000*1000 + (long long)50*1000*1000; pthread_make_periodic_np (pthread_self(), gethrtime(), period); sa.sa_handler=signal_handler; sa.sa_mask=0; sa.sa_flags=0; signal=RTL_SIGUSR1+param; if ((err=sigaction(signal,&sa,NULL))<0 ){ rtl_printf("sigaction(signal,&sa,NULL) FAILING, err:%d.\n",err); pthread_exit(NULL); } while (i++<ITERS) { pthread_wait_np (); pthread_kill(pthread_self(),signal); } return 0;}int init_module(void) { int i; for (i=0;i<NTASKS-1;i++) pthread_create (&thread[i], NULL, start_routine, (int *) i); // This thread will interrupt signals handlers executions of less prioritaries threads. pthread_create(&thread[NTASKS-1],NULL,hight_prio_routine,(void *)NULL); return 0;}void cleanup_module(void) { int i; for (i=0;i<NTASKS;i++) pthread_delete_np (thread[i]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -