⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sig_intr.c

📁 fsmlabs的real time linux的内核
💻 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. * * Testing receiving RTL_SIGNAL_SUSPEND while executing a signal handler. * Thread will be suspended after executing the signal handler. * The same thing happens if inside a signal handler it is called pthread_suspend_np. * Also, timing functions like usleep, nanosleep and clock_nanosleep doesn't work inside * signal handlers. */#include <rtl.h>#include <pthread.h>#define SIGUSR1 RTL_SIGUSR1#define NTASKS 2static pthread_t thread[NTASKS];#define ITERS 20static int count=0;int begin=0;static void signal_handler(int signal){  int i,j,k,m=0;  hrtime_t t,t2;  rtl_printf("Signal handler %d. Begining with proving usleep function for 1000000 nanosecs on th:%d\n",signal,pthread_self());  t=gethrtime();  usleep(1000);  t2=gethrtime();  rtl_printf("EXPECT FAILURE: Elapsed time %d\n",(int)t2-t);  begin=1;  rtl_printf("Signal handler called for signal %d, just wasting time\n",signal);  for (i=0;i<ITERS;i++){    rtl_printf(" Sig hdl m:%d ",m);    for (j=0;j<ITERS;j++)      for (k=0;k<ITERS;k++)	m=i+k-j;      }  rtl_printf("Signal handler %d. Before ending\n",signal);   }static void *hight_prio_routine(void *arg){  struct sched_param p;  p.sched_priority=100;  pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);  pthread_make_periodic_np(pthread_self(), gethrtime(), 100*1000LL);  pthread_kill(thread[0],SIGUSR1);  while (count<ITERS){    if (begin){      rtl_printf("INTR  hdl exec i: %d\n",count);      if (count==ITERS/10) {	rtl_printf("\n\n SUSPENDING THREAD EXECECUTING SIGNAL HANDLER.");	rtl_printf("EXPECTING GETTING SUSPENDED AFTER FINISHING SIGNAL HANDLER EXECUTION.\n\n\n");	pthread_suspend_np(thread[0]);      }      else 	if (count== ITERS/2) {	  rtl_printf("WAKING UP THREAD EXECUTING SIGNAL HANDLER\n");	  pthread_wakeup_np(thread[0]);	}    }    count++;  }    pthread_wait_np();   return 0;}static void *start_routine(void *arg){  struct sched_param p;  int param,err;  struct sigaction sa;  rtl_sigset_t mask;  param=(unsigned) arg;  p . sched_priority = param;  pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);    sa.sa_handler=signal_handler;  sa.sa_mask=0;  sa.sa_flags=0;    if ((err=sigaction(SIGUSR1,&sa,NULL))<0 ){    rtl_printf("sigaction(SIGUSR1,&sa,NULL) FAILING, err:%d.\n",err);    pthread_exit(NULL);  }    rtl_sigfillset(&mask);  rtl_sigdelset(&mask,SIGUSR1);  sigsuspend(&mask);  count=ITERS+1;  rtl_printf("terminating hight priority routine \n");  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 lower priority thread signal handler execution.  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 + -