<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://try-as400.pocnet.net/index.php?action=history&amp;feed=atom&amp;title=Properly_ending_a_daemon_written_in_C</id>
	<title>Properly ending a daemon written in C - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://try-as400.pocnet.net/index.php?action=history&amp;feed=atom&amp;title=Properly_ending_a_daemon_written_in_C"/>
	<link rel="alternate" type="text/html" href="http://try-as400.pocnet.net/index.php?title=Properly_ending_a_daemon_written_in_C&amp;action=history"/>
	<updated>2026-04-20T15:43:12Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>http://try-as400.pocnet.net/index.php?title=Properly_ending_a_daemon_written_in_C&amp;diff=1171&amp;oldid=prev</id>
		<title>PoC: /* Blocking Calls */ Completing the sentence</title>
		<link rel="alternate" type="text/html" href="http://try-as400.pocnet.net/index.php?title=Properly_ending_a_daemon_written_in_C&amp;diff=1171&amp;oldid=prev"/>
		<updated>2022-08-13T14:34:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Blocking Calls: &lt;/span&gt; Completing the sentence&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:34, 13 August 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l41&quot;&gt;Line 41:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 41:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If the application program is &amp;quot;stuck&amp;quot; in kind of a wait condition, it depends on the wait condition if the blocking call is cancelled, allowing the application code to take control again.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If the application program is &amp;quot;stuck&amp;quot; in kind of a wait condition, it depends on the wait condition if the blocking call is cancelled, allowing the application code to take control again.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This code snippet shows an example how to restart  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This code snippet shows an example how to restart &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;an interrupted recv() from a socket by restarting the &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  /* Main loop. */&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  /* Main loop. */&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l60&quot;&gt;Line 60:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 60:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Some OS&#039; automatically restart the last operation if it became interrupted by a signal. OS/400 V4R5 does not, so we restart in a loop, making this example a generic restart function, if &amp;lt;code&amp;gt;EINTR&amp;lt;/code&amp;gt; is given as error reason.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;blockquote&amp;gt;&#039;&#039;&#039;Note&#039;&#039;&#039;: &lt;/ins&gt;Some OS&#039; automatically restart the last operation if it became interrupted by a signal. OS/400 V4R5 does not, so we restart in a loop, making this example a generic restart function, if &amp;lt;code&amp;gt;EINTR&amp;lt;/code&amp;gt; is given as error reason.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category: Programming]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category: Programming]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key tryas400:diff:1.41:old-1170:rev-1171:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>PoC</name></author>
	</entry>
	<entry>
		<id>http://try-as400.pocnet.net/index.php?title=Properly_ending_a_daemon_written_in_C&amp;diff=1170&amp;oldid=prev</id>
		<title>PoC: New</title>
		<link rel="alternate" type="text/html" href="http://try-as400.pocnet.net/index.php?title=Properly_ending_a_daemon_written_in_C&amp;diff=1170&amp;oldid=prev"/>
		<updated>2022-08-13T14:23:05Z</updated>

		<summary type="html">&lt;p&gt;New&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Properly ending a daemon&amp;#039;&amp;#039;&amp;#039;, or background task can be done in multiple ways. Since we&amp;#039;re using C as programming language, using the UNIX way of handling this affair proved to be most easy.&lt;br /&gt;
&lt;br /&gt;
OS/400 incorporates quite a bit of UNIX APIs, including signals. If you use option 4 on &amp;lt;code&amp;gt;wrkactjob&amp;lt;/code&amp;gt;, or end the subsystem the job in question is running within, SIGTERM is being sent to tell the application program to cleanly end.&lt;br /&gt;
&lt;br /&gt;
== Using the signal API ==&lt;br /&gt;
In short, declare the necessary infrastructure for a signal, and make it do something.&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 /* Using this globally eases handling. */&lt;br /&gt;
 int exit_flag = 0;&lt;br /&gt;
 &lt;br /&gt;
 void set_exit_flag(int signum) {&lt;br /&gt;
   /* We ignore signum, because we only watch for SIGTERM. */&lt;br /&gt;
   exit_flag = 1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main() {&lt;br /&gt;
   struct sigaction termaction;&lt;br /&gt;
 &lt;br /&gt;
   /* Create signal handler for SIGTERM. */&lt;br /&gt;
   memset(&amp;amp;termaction, 0, sizeof(struct sigaction));&lt;br /&gt;
   termaction.sa_handler = set_exit_flag;&lt;br /&gt;
   sigaction(SIGTERM, &amp;amp;termaction, NULL);&lt;br /&gt;
 &lt;br /&gt;
   /* Main loop. */&lt;br /&gt;
   while( exit_flag == 0 ) {&lt;br /&gt;
     ...&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /* Add cleanup routines here. */&lt;br /&gt;
 &lt;br /&gt;
   exit(0);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In our case, we set &amp;lt;code&amp;gt;exit_flag&amp;lt;/code&amp;gt; to 1, so the program exits from its main loop at the next iteration.&lt;br /&gt;
&lt;br /&gt;
=== Blocking Calls ===&lt;br /&gt;
If the application program is &amp;quot;stuck&amp;quot; in kind of a wait condition, it depends on the wait condition if the blocking call is cancelled, allowing the application code to take control again.&lt;br /&gt;
&lt;br /&gt;
This code snippet shows an example how to restart &lt;br /&gt;
&lt;br /&gt;
 /* Main loop. */&lt;br /&gt;
 while( exit_flag == 0 ) {&lt;br /&gt;
 &lt;br /&gt;
   /* Handle incoming network data. */&lt;br /&gt;
   if ( (n = recv(sockfd, rcvBuf, BUFSIZE, 0)) == -1) {&lt;br /&gt;
     if ( errno == EINTR ) {&lt;br /&gt;
       continue;&lt;br /&gt;
     } else {&lt;br /&gt;
       /* Some other error happened. Handle next request. */&lt;br /&gt;
       Qp0zLprintf(&amp;quot;recv(): %s\n&amp;quot;, strerror(errno));&lt;br /&gt;
       break;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 /* Do things with the received data. */&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some OS&amp;#039; automatically restart the last operation if it became interrupted by a signal. OS/400 V4R5 does not, so we restart in a loop, making this example a generic restart function, if &amp;lt;code&amp;gt;EINTR&amp;lt;/code&amp;gt; is given as error reason.&lt;br /&gt;
&lt;br /&gt;
[[Category: Programming]]&lt;/div&gt;</summary>
		<author><name>PoC</name></author>
	</entry>
</feed>