Most operating systems provide interrupt facilities, sometimes called signals either generated by the user or as a result of program failure or reaching a limit like file space.
Unhandled signals generally terminate a program in a disorderly manner.
Signal handlers are created so that the program behaves in a well-defined manner upon receipt of a signal.

Task

Provide a program that displays an integer on each line of output at the rate of about one per half second.
Upon receipt of the SIGINT signal (often generated by the user typing ctrl-C ( or better yet, SIGQUIT ctrl-\ )) the program will cease outputting integers, output the number of seconds the program has run, and then the program will quit.




      REM!Exefile C:\bbcsigint.exe,encrypt,console
      INSTALL @lib$+"CALLBACK"
      CTRL_C_EVENT = 0
 
      SYS "GetStdHandle", -10 TO @hfile%(1)
      SYS "GetStdHandle", -11 TO @hfile%(2)
      *INPUT 13
      *OUTPUT 14
      ON ERROR PRINT REPORT$ : QUIT ERR
 
      CtrlC% = FALSE
      handler% = FN_callback(FNsigint(), 1)
      SYS FN_syscalls("SetConsoleCtrlHandler"), handler%, 1 TO !FN_systo(res%)
      IF res%=0 PRINT "Could not set SIGINT handler" : QUIT 1
 
      PRINT "Press Ctrl+C to test...."
      TIME = 0
      Time% = 50
      REPEAT
        WAIT 1
        IF TIME > Time% THEN
          PRINT Time%
          Time% += 50
        ENDIF
      UNTIL CtrlC%
      PRINT "Ctrl+C was pressed after "; TIME/100 " seconds."
      QUIT
 
      DEF FNsigint(T%)
      CASE T% OF
        WHEN CTRL_C_EVENT: CtrlC% = TRUE : = 1
      ENDCASE
      = 0