{"id":3457,"date":"2019-01-19T00:01:52","date_gmt":"2019-01-19T08:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3457"},"modified":"2019-01-23T10:34:53","modified_gmt":"2019-01-23T18:34:53","slug":"ignoring-signals","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3457","title":{"rendered":"Ignoring Signals"},"content":{"rendered":"<p>Have you ever had your C code run amok and had to press Ctrl+C to cancel?<\/p>\n<p>Yeah, I&#8217;ve never had that happen to me either.<br \/>\n<!--more--><br \/>\nBut seriously, what happens when you press Ctrl+C? Do you know?<\/p>\n<p>Internally, pressing Ctrl+C sends a termination signal to the program. This signal is just one of many that can communicate with a running program. In fact, all your programs are constantly scanning for signals. Some of these signals can be intercepted and acted upon.<\/p>\n<p>As an example, the Ctrl+C signal is defined as <code>SIGINT<\/code>, the program interruption signal. I know of a few others, such as Ctrl+Z that exits a program temporarily. (And it can wreck havoc when your code doesn&#8217;t know how to handle it.) For this Lesson, I&#8217;ll stick with Ctrl+C.<\/p>\n<p>One time Ctrl+C becomes necessary is when the code gets stuck in an infinite loop. Here&#8217;s a deliberate example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char a = 0;\r\n\r\n    for(;;)\r\n    {\r\n        printf(\"%d\\n\",a);\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop in this code keeps outputting the value of <em>char<\/em> variable <code>a<\/code>, cycling between -128 and 127 over and over until you press Ctrl+C (send the <code>SIGINT<\/code> signal) to terminate the program. (The program can also be terminated by sending it the kill signal.)<\/p>\n<p>By the way, the Ctrl+S and Ctrl+Q keyboard shortcuts don&#8217;t send signals. These commands pause and continue text output (respectively), but they&#8217;re not signals. Ctrl+C, however, is a signal, and it can be intercepted in your code by using the <em>signal()<\/em> function.<\/p>\n<p>The <em>signal()<\/em> function is prototyped in the <code>signal.h<\/code> header file. Its format is odd:<\/p>\n<p><code>(void) signal(type,funct);<\/code><\/p>\n<p>The <code>(void)<\/code> typecast is required. The first argument, <em>type<\/em>, is a constant representing the signal to trap, such as <code>SIGINT<\/code> for Ctrl+C. The second argument is the name of a function elsewhere in your code that handles the signal. The function accepts a single <em>int<\/em> argument, traditionally named <code>sig<\/code>.<\/p>\n<p>In the following code, the <em>ctrap()<\/em> function is called when Ctrl+C attempts to break the endless loop. Within the function, a message is output and program flow is paused until Enter is pressed. Then, a <em>signal()<\/em> function re-instates the <code>SIGINT<\/code> (Ctrl+C) signal, so that the endless loop can finally be terminated.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;signal.h&gt;\r\n\r\nvoid ctrap(int sig)\r\n{\r\n    puts(\"Ctrl-C intercepted!\");\r\n    getchar();\r\n    <span class=\"comments\">\/* restore the default *\/<\/span>\r\n    (void) signal(SIGINT,SIG_DFL);\r\n}\r\n\r\nint main()\r\n{\r\n    char a = 0;\r\n\r\n    <span class=\"comments\">\/* intercept Ctrl+C *\/<\/span>\r\n    (void) signal(SIGINT,ctrap);\r\n\r\n    for(;;)\r\n    {\r\n        printf(\"%d\\n\",a);\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If you run this code, press Ctrl+C and you&#8217;ll see output pause. Press Enter to continue, then press Ctrl+C a second time to terminate. (This action may not work on all systems.)<\/p>\n<p>I&#8217;ve used the <em>signal()<\/em> function in a text-mode game I wrote. It captures the <code>SIGTSTP<\/code> signal, activated by pressing Ctrl+Z in a Unix, to suspend the program and use the shell. That way someone can suspend the game, do something in the shell, then return to the program (use the <em>bg<\/em> shell command) to continue the game.<\/p>\n<p>If the Ctrl+Z (<code>SIGTSTP<\/code>) signal weren&#8217;t intercepted, the game gets suspended when the user presses Ctrl+Z, but upon return it&#8217;s confused and doesn&#8217;t know what to do. Only when captured properly, can the code successfully suspend itself and recover. This action is made possible, thanks to the <em>signal()<\/em> function and its interception of the <code>SIGTSTP<\/code> signal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You probably don&#8217;t know what a signal is, but you have definitely seen its effect. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3457\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3457","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3457"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3457\/revisions"}],"predecessor-version":[{"id":3480,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3457\/revisions\/3480"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}