home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / wizards / 3750 < prev    next >
Encoding:
Text File  |  1992-08-31  |  3.8 KB  |  130 lines

  1. Xref: sparky comp.unix.wizards:3750 comp.unix.questions:10558
  2. Newsgroups: comp.unix.wizards,comp.unix.questions
  3. Path: sparky!uunet!mcsun!ieunet!tcdcs!unix1.tcd.ie!acourtny
  4. From: acourtny@unix1.tcd.ie (Antony A. Courtney)
  5. Subject: reliable signals under BSD / SVR4
  6. Message-ID: <acourtny.715263267@unix1.tcd.ie>
  7. Summary: looks like they're not reliable
  8. Keywords: signals, reliable, software interrupts
  9. Sender: usenet@cs.tcd.ie (NN required at ashe.cs.tcd.ie)
  10. Nntp-Posting-Host: unix1.tcd.ie
  11. Organization: Trinity College, Dublin
  12. Date: Mon, 31 Aug 1992 12:14:27 GMT
  13. Lines: 115
  14.  
  15. I wrote the following short program to test whether or not signals are reliable
  16. on our particular SVR4 implementation  (A Siemens-Nixdorf RM600 running Sinix).
  17.  
  18. The particular implementation of signal() is from Stevens' "Advanced Programming
  19. in the UNIX environment".  
  20.  
  21. The program simply installs a signal handler for SIGUSR1, fork()s, and the
  22. parent sends 20 SIGUSR1 signals to the child as fast as it can.  The child
  23. has a busy loop in the signal handler to make it deliberately slow.  The result
  24. is that I only catch a random subset of the signals posted (sometimes I don't
  25. catch any, other times I get 2 or 3).  I get similar behavior under 4.3BSD if
  26. I use BSD's implementation of signal(), which is supposedly "reliable".
  27.  
  28. What's the word?  Are signals "reliable" or aren't they?
  29.  
  30. I admit I haven't read *every* single standard written about all the various
  31. implementations and semantics of signals; that would probably take more time
  32. than writing a reliable signal implementation!
  33.  
  34. Code follows, thanks for any help...
  35.  
  36.     -antony
  37.  
  38. ------snip, snip----
  39. /*
  40.  * Program to test whether or not signals get queued.
  41.  *
  42.  * Antony A. Courtney, 21/2/92
  43.  *
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include <signal.h>
  48.  
  49. typedef void Sigfunc(int);
  50.  
  51. void handle_sig(int);
  52.  
  53. /* reliable version of signal, using POSIX sigaction */
  54. Sigfunc *signal(int signo,Sigfunc *func)
  55. {
  56.     struct sigaction act,oact;
  57.  
  58.     printf("in signal()\n");
  59.     act.sa_handler=func;
  60.     sigemptyset(&act.sa_mask);
  61.     act.sa_flags=0;
  62.     if (signo==SIGALRM) {
  63. #ifdef SA_INTERRUPT
  64.         act.sa_flags |= SA_INTERRUPT;
  65. #endif
  66.     } else {
  67. #ifdef SA_RESTART
  68.         act.sa_flags |= SA_RESTART;
  69. #endif
  70.     }
  71.     if (sigaction(signo, &act, &oact) < 0) {
  72.         return (SIG_ERR);
  73.     }
  74.     return (oact.sa_handler);
  75. }
  76.  
  77. main()
  78. {
  79.         int i,pid;
  80.  
  81.         /* install the signal handler here so that the child catches all
  82.          * signals immediately.
  83.          */
  84.         if (signal(SIGUSR1,handle_sig)==SIG_ERR) {
  85.                 perror("signal");
  86.                 exit(1);
  87.         }
  88.         pid=fork();
  89.         switch (pid) {
  90.                 case -1:
  91.                         perror("fork");
  92.                         exit(1);
  93.                         break;
  94.                 case 0: /* child */
  95.                         while (1) {
  96.                                 pause();
  97.                         }
  98.                         break;
  99.                 default: /* parent */
  100.                         for (i=0; i < 20; i++) {
  101.                                 if (kill(pid,SIGUSR1) < 0) {
  102.                                         perror("kill");
  103.                                         exit(1);
  104.                                 }
  105.                         }
  106.                         wait(0);
  107.                         break;
  108.         }
  109. }
  110.  
  111. void handle_sig(int x)
  112. {
  113.         static int count=0;
  114.     int a,j;
  115.  
  116.         count++;
  117.         printf("Got signal # %d\n",count);
  118.     for (j=0; j < 100000; j++) {
  119.         a=j;
  120.     }
  121.     printf("returning from signal handler\n");
  122. }
  123.  
  124. ---- snip, snip -----
  125. --
  126. ********************************************************************************
  127. * Antony A. Courtney                              Email: acourtny@unix1.tcd.ie *
  128. * Computer Science Department                            antony@george.lbl.gov *
  129. * Trinity College, Dublin, Ireland                Phone: 01+353+1-607389       *
  130.