<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>therning.org/ magnus &#187; posix signals</title>
	<atom:link href="http://therning.org/magnus/archives/tag/posix_signals/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Mon, 09 Apr 2012 20:24:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Signals in Haskell</title>
		<link>http://therning.org/magnus/archives/285</link>
		<comments>http://therning.org/magnus/archives/285#comments</comments>
		<pubDate>Fri, 11 May 2007 22:52:08 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[posix signals]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/285</guid>
		<description><![CDATA[When playing with Haskell I sometimes get the feeling that I&#8217;m the last one in the world to figure out how some things work. It&#8217;s almost embarrassing to write about it at times, but I fear I&#8217;ll forget and end up redoing my trivial experiments if I don&#8217;ti. So, here goes&#8230; Dealing with signals in [...]]]></description>
			<content:encoded><![CDATA[<p>When playing with Haskell I sometimes get the feeling that I&#8217;m the last one in the world to figure out how some things work. It&#8217;s almost embarrassing to write about it at times, but I fear I&#8217;ll forget and end up redoing my trivial experiments if I don&#8217;t<sup><a href="http://therning.org/magnus/archives/285#footnote_0_285" id="identifier_0_285" class="footnote-link footnote-identifier-link" title="Or maybe I shouldn&amp;#8217;t compare myself to the highly (dare I say it?) academical entries usually found on the Haskell Planet. Maybe I should just consider my writing to be &amp;#8220;Haskell for mortals&amp;#8221;.">i</a></sup>. So, here goes&#8230;</p>

<p>Dealing with signals in Haskell is fairly straight forward. Well, at least as far as dealing with signals ever is straight forward. In <code>System.Posix.Signals</code> you find <code>installHandler</code>. The arguments are a little funky but they get a little clearer if you look up the man-page for <code>sigaction</code>.</p>

<p>Here&#8217;s a simple example that installs a handler for <code>SIGINT</code>:</p>

<pre><code>module Main where

import System.Posix.Signals
import Control.Concurrent

handler :: IO ()
handler = putStrLn "In handler"

doNothing :: IO ()
doNothing = do
    threadDelay 1000000
    putStrLn "Repeating"

main :: IO ()
main = do
    installHandler sigINT (Catch handler) Nothing
    sequence_ $ repeat doNothing
</code></pre>

<p>Now that&#8217;s fairly straight forward, I think. However, it&#8217;d be nice to be able to let the main thread know that we&#8217;ve recieved a signal in some way. From perusing the <a href="http://www.haskell.org/hawiki/FrontPage">Haskell Wiki</a> it seems the way to communicate between threads, at least for simple cases, is by using <code>MVar</code>s. So, extending the example above to also keep track of how many times <code>SIGINT</code> has been received could look like this:</p>

<pre><code>module Main where

import System.Posix.Signals
import Control.Concurrent
import Control.Concurrent.MVar

handler :: MVar Int -&gt; IO ()
handler mi = do
    i &lt;- takeMVar mi
    putStrLn "In handler"
    putMVar mi (i + 1)

doNothing :: MVar Int -&gt; IO ()
doNothing mi = do
    threadDelay 1000000
    i &lt;- readMVar mi
    putStrLn $ "Repeating " ++ (show i)

main :: IO ()
main = do
    mi &lt;- newMVar 0
    installHandler sigINT (Catch $ handler mi) Nothing
    sequence_ $ repeat $ doNothing mi
</code></pre>

<p>It should be noted that, and here it all hinges on me having understood things correctly, <code>handler</code> has more or less become a sort of critical section. Taking the value out of the <code>MVar</code> makes sure that the <code>readMVar</code> in <code>doNothing</code> blocks until a new value is put back. Probably not a very useful thing in this particular example, but it&#8217;s still worth keeping in mind for the future.</p>
<ol class="footnotes"><li id="footnote_0_285" class="footnote">Or maybe I shouldn&#8217;t compare myself to the highly (dare I say it?) academical entries usually found on the Haskell Planet. Maybe I should just consider my writing to be &#8220;Haskell for mortals&#8221;.</li></ol><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F285&amp;title=Signals%20in%20Haskell" id="wpa2a_2">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/285/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

