home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!wupost!darwin.sura.net!sgiblab!public!car
- From: car@public.BTR.COM (Carlos Rimola-Sarti car@btr.com)
- Newsgroups: comp.os.os2.programmer
- Subject: Re: DosExitList Hangs Process (OS/2 V1.3)
- Message-ID: <7871@public.BTR.COM>
- Date: 1 Sep 92 06:04:41 GMT
- References: <1992Aug31.124258.980@beckman.com>
- Organization: Connective Strategies, Inc. (CSI). Mountain View, California
- Lines: 157
-
- In article <1992Aug31.124258.980@beckman.com> rariedel@beckman.com (Bob Riedel) writes:
- >I have an OS/2 (V1.3) program in which I am attempting to use DosExitList()
- >to invoke a function to perform certain cleanup operations primarily
- >involving flushing an array to disk and closing the associated file.
- >I am having some difficulty with this because the program will hang
- >(instead of exiting) when Ctrl-C terminates the program. The system has to be
- >restarted to recover.
- >
- >It appears that my exit routine is not even being called since debug
- >printf(s) never print when this situation occurs. The program will run to
- >completion normally if Ctrl-C is not hit, and the exit handler is entered
- >and functions normally.
- >
- >Any insight?
-
- I believe that what you are missing is a "signal catcher" function. I
- am not sure if an exit handler will be called or not in the event of a
- signal. If you really want to catch everything, you need both. Sample
- code that I use:
-
- /****************************************************************************\
- * *
- * Name: main *
- * *
- * Function: main entry point *
- * *
- \****************************************************************************/
-
- void main( argc, argv)
-
- int argc;
- char *argv[];
- {
- int rc;
-
-
- /*
- * Setup signal handlers and exit handlers to catch termination
- * and close log files, cleanup, etc.
- */
-
- rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_CTRLC);
-
- if (!rc)
- rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_CTRLBREAK);
-
- if (!rc)
- rc = DosSetSigHandler ( sigCatcher, 0, 0, SIGA_ACCEPT, SIG_KILLPROCESS);
-
- if (rc)
- {
- LogPrint( 0, "Unable to set Signal handler. Terminating..\n");
- exit( rc);
- }
-
- if (rc = DosExitList( EXLST_ADD, exitCatcher))
- {
- LogPrint( 0, "Unable to set Exit handler. Terminating..\n");
- exit( rc);
- }
-
- /*
- * Start the "working-bee" threads here.. Then wait indefinitely..
- */
-
- /* ..._beginthread(s)... */
-
- DosSemSetWait( &MainWaitSem, SEM_INDEFINITE_WAIT);
-
- LogPrint( 0, "Server ended via main thread\n");
- }
-
-
- /****************************************************************************\
- * *
- * Name: sigCatcher *
- * *
- * Function: catch termination signals.. *
- * *
- \****************************************************************************/
-
- VOID PASCAL FAR sigCatcher ( sigArg, sigNum)
-
- USHORT sigArg;
- USHORT sigNum;
-
- {
- RETCODE rc;
-
- LogPrint( 0, "\n");
-
- if (sigNum == SIG_CTRLC || sigNum == SIG_CTRLBREAK)
- LogPrint( 0, "Server terminated by user (Break)\n");
- else
- LogPrint( 0, "Server terminated externally (Kill)\n");
-
- if (LogDisk)
- {
- DosBufReset( -1); // flush all buffers
- fclose( LogFile); // close my log file
- LogDisk = FALSE;
- }
-
- DosBeep( 1380, 300); // beep user
- DosBeep( 1580, 300); // beep user
- DosBeep( 1180, 300); // beep user
-
- DosExit( 1, 0);
- }
-
-
- /****************************************************************************\
- * *
- * Name: exitCatcher *
- * *
- * Function: catch abnormal exits.. *
- * *
- \****************************************************************************/
-
- VOID PASCAL FAR exitCatcher ( USHORT reason)
- {
- if (reason == 1)
- LogPrint( 0, "Server ended due to Hard Error!\n");
- else if (reason == 2)
- LogPrint( 0, "Server ended due to Trap Exception!\n");
- else if (reason == 3)
- LogPrint( 0, "Server ended due to KillProcess!\n");
- else if (reason)
- LogPrint( 0, "Server ended abnormally! Reason = %d\n", reason);
-
- if (LogDisk)
- {
- DosBufReset( -1); // flush all buffers
- fclose( LogFile); // close my log file
- LogDisk = FALSE;
- }
-
- if (reason)
- {
- DosBeep( 1380, 300); // beep user
- DosBeep( 1580, 300); // beep user
- DosBeep( 1180, 300); // beep user
- }
-
- DosExitList( EXLST_EXIT, 0);
- }
-
-
-
- I apollogize for the line endings but I don't have dos2unix/os22unix handy.
- The function LogPrint is an enhanced printf.
-
- +---------------------------------------+-----------------------------------+
- | Carlos Rimola-Sarti | email: rimola@csisdn.com |
- | Connective Strategies, Inc. | car@btr.com |
- | ISDN PRI Connectivity Group | phone: 415-903-2585 |
- +---------------------------------------+-----------------------------------+
-