Handle a signal: Difference between revisions

Content added Content deleted
(Add SIGQUIT since per the task description SIGQUIT is more appropriate that SIGINT (see perl example) see discussion.)
Line 14: Line 14:
{{omit from|XSLT}}
{{omit from|XSLT}}


Most general purpose operating systems provide interrupt facilities, sometimes called signals.
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.
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.
Signal handlers are created so that the program behaves in a well-defined manner upon receipt of a signal.



;Task:
;Task:
Provide a program that displays a single integer on each line of output at the rate of one integer in each half second.
Provide a program that displays an integer on each line of output at the rate of about one per half second.
<!-- some systems have difficulty with 1/2 second and that's not the point of this subject anyway DG-->

Upon receipt of the SigInt signal (often created by the user typing ctrl-C) the program will cease printing integers to its output, print the number of seconds the program has run, and then the program will terminate.
Upon receipt of the SIGINT signal (often generated by the user typing ctrl-C <!-- on unix see stty -a . on windows SIGBREAK DG -->( 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. <!-- outputting the number if seconds is also unduly complicated for this topic . Hope nobody minds these comments and I didn't really want to change the task since there were so many examples already written. see the Perl example for a more extensive use of signals. PS See discussion. If you find these edits inappropriate let me know DG-->
<br><br>
<br><br>