home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / os2 / programm / 4628 < prev    next >
Encoding:
Text File  |  1992-08-31  |  5.7 KB  |  168 lines

  1. Path: sparky!uunet!usc!wupost!darwin.sura.net!sgiblab!public!car
  2. From: car@public.BTR.COM (Carlos Rimola-Sarti  car@btr.com)
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: Re: DosExitList Hangs Process (OS/2 V1.3)
  5. Message-ID: <7871@public.BTR.COM>
  6. Date: 1 Sep 92 06:04:41 GMT
  7. References: <1992Aug31.124258.980@beckman.com>
  8. Organization: Connective Strategies, Inc. (CSI).  Mountain View, California
  9. Lines: 157
  10.  
  11. In article <1992Aug31.124258.980@beckman.com> rariedel@beckman.com (Bob Riedel) writes:
  12. >I have an OS/2 (V1.3) program in which I am attempting to use DosExitList()
  13. >to invoke a function to perform certain cleanup operations primarily
  14. >involving flushing an array to disk and closing the associated file.
  15. >I am having some difficulty with this because the program will hang
  16. >(instead of exiting) when Ctrl-C terminates the program. The system has to be
  17. >restarted to recover.
  18. >
  19. >It appears that my exit routine is not even being called since debug
  20. >printf(s) never print when this situation occurs. The program will run to
  21. >completion normally if Ctrl-C is not hit, and the exit handler is entered
  22. >and functions normally.
  23. >
  24. >Any insight?
  25.  
  26. I believe that what you are missing is a "signal catcher" function.  I
  27. am not sure if an exit handler will be called or not in the event of a
  28. signal.  If you really want to catch everything, you need both.  Sample
  29. code that I use:
  30.  
  31. /****************************************************************************\
  32. *                                                                            *
  33. * Name:         main                                                         *
  34. *                                                                            *
  35. * Function:     main entry point                                             *
  36. *                                                                            *
  37. \****************************************************************************/
  38.  
  39. void main( argc, argv)
  40.  
  41.     int     argc;
  42.     char   *argv[];
  43. {
  44.     int         rc;
  45.  
  46.  
  47.     /*
  48.      *    Setup signal handlers and exit handlers to catch termination
  49.      *    and close log files, cleanup, etc.
  50.      */
  51.  
  52.     rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_CTRLC);
  53.  
  54.     if (!rc)
  55.         rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_CTRLBREAK);
  56.  
  57.     if (!rc)
  58.         rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_KILLPROCESS);
  59.  
  60.     if (rc)
  61.     {
  62.         LogPrint( 0, "Unable to set Signal handler.  Terminating..\n");
  63.          exit( rc);
  64.     }
  65.  
  66.     if (rc = DosExitList( EXLST_ADD, exitCatcher))
  67.     {
  68.         LogPrint( 0, "Unable to set Exit handler.  Terminating..\n");
  69.          exit( rc);
  70.     }
  71.  
  72.     /*
  73.      *    Start the "working-bee" threads here..  Then wait indefinitely..
  74.      */
  75.  
  76.     /* ..._beginthread(s)... */
  77.  
  78.     DosSemSetWait( &MainWaitSem, SEM_INDEFINITE_WAIT); 
  79.  
  80.     LogPrint( 0, "Server ended via main thread\n");
  81. }
  82.  
  83.  
  84. /****************************************************************************\
  85. *                                                                            *
  86. * Name:         sigCatcher                                                   *
  87. *                                                                            *
  88. * Function:     catch termination signals..                                  *
  89. *                                                                            *
  90. \****************************************************************************/
  91.  
  92. VOID PASCAL FAR sigCatcher ( sigArg, sigNum)
  93.    
  94.     USHORT      sigArg;
  95.     USHORT      sigNum;
  96.  
  97. {
  98.        RETCODE     rc;
  99.  
  100.     LogPrint( 0, "\n");
  101.  
  102.     if (sigNum == SIG_CTRLC || sigNum == SIG_CTRLBREAK)
  103.         LogPrint( 0, "Server terminated by user (Break)\n");
  104.     else
  105.         LogPrint( 0, "Server terminated externally (Kill)\n");
  106.  
  107.     if (LogDisk)
  108.     {
  109.         DosBufReset( -1);                // flush all buffers
  110.         fclose( LogFile);                // close my log file
  111.         LogDisk = FALSE;
  112.     }
  113.  
  114.     DosBeep( 1380, 300);                // beep user
  115.     DosBeep( 1580, 300);                // beep user
  116.     DosBeep( 1180, 300);                // beep user
  117.  
  118.     DosExit( 1, 0);
  119. }
  120.  
  121.  
  122. /****************************************************************************\
  123. *                                                                            *
  124. * Name:         exitCatcher                                                  *
  125. *                                                                            *
  126. * Function:     catch abnormal exits..                                       *
  127. *                                                                            *
  128. \****************************************************************************/
  129.  
  130. VOID PASCAL FAR exitCatcher ( USHORT reason)
  131. {
  132.     if (reason == 1)
  133.         LogPrint( 0, "Server ended due to Hard Error!\n");
  134.     else if (reason == 2)
  135.         LogPrint( 0, "Server ended due to Trap Exception!\n");
  136.     else if (reason == 3)
  137.         LogPrint( 0, "Server ended due to KillProcess!\n");
  138.     else if (reason)
  139.         LogPrint( 0, "Server ended abnormally!  Reason = %d\n", reason);
  140.  
  141.     if (LogDisk)
  142.     {
  143.         DosBufReset( -1);                // flush all buffers
  144.         fclose( LogFile);                // close my log file
  145.         LogDisk = FALSE;
  146.     }
  147.  
  148.     if (reason)
  149.     {
  150.         DosBeep( 1380, 300);                // beep user
  151.         DosBeep( 1580, 300);                // beep user
  152.         DosBeep( 1180, 300);                // beep user
  153.     }
  154.  
  155.        DosExitList( EXLST_EXIT, 0);
  156. }
  157.  
  158.  
  159.  
  160. I apollogize for the line endings but I don't have dos2unix/os22unix handy.
  161. The function LogPrint is an enhanced printf.
  162.  
  163. +---------------------------------------+-----------------------------------+
  164. | Carlos Rimola-Sarti                   |         email: rimola@csisdn.com  |
  165. | Connective Strategies, Inc.           |                      car@btr.com  |
  166. | ISDN PRI Connectivity Group           |         phone:      415-903-2585  |
  167. +---------------------------------------+-----------------------------------+
  168.