home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / tests / benchmarks / csw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-06  |  902 b   |  54 lines

  1. /*
  2.  * Context switching benchmark.
  3.  *
  4.  * Force system to context switch 2*nsigs
  5.  * times by forking and exchanging signals.
  6.  * To calculate system overhead for a context
  7.  * switch, the signocsw program must be run
  8.  * with nsigs.  Overhead is then estimated by
  9.  *    t1 = time csw <n>
  10.  *    t2 = time signocsw <n>
  11.  *    overhead = t1 - 2 * t2;
  12.  */
  13. #include <signal.h>
  14.  
  15. int    sigsub();
  16. int    otherpid;
  17. int    nsigs;
  18.  
  19. main(argc, argv)
  20.     char *argv[];
  21. {
  22.     int pid;
  23.  
  24.     if (argc < 2) {
  25.         printf("usage: %s nsignals\n", argv[0]);
  26.         exit(1);
  27.     }
  28.     nsigs = atoi(argv[1]);
  29.     signal(SIGALRM, sigsub);
  30.     otherpid = getpid();
  31.     pid = fork();
  32.     if (pid != 0) {
  33.         otherpid = pid;
  34.         kill(otherpid, SIGALRM);
  35.     }
  36.     for (;;)
  37.         sigpause(0);
  38. }
  39.  
  40. sigsub()
  41. {
  42.     static mustreset = 1;
  43.     int (*osig)();
  44.  
  45.     if (mustreset) {
  46.         osig = signal(SIGALRM, sigsub);
  47.         if (osig == sigsub)
  48.             mustreset = 0;
  49.     }
  50.     kill(otherpid, SIGALRM);
  51.     if (--nsigs <= 0)
  52.         exit(0);
  53. }
  54.