<?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>Evolution Systems &#187; Sleep</title>
	<atom:link href="http://evolution-systems.co.uk/tag/sleep/feed/" rel="self" type="application/rss+xml" />
	<link>http://evolution-systems.co.uk</link>
	<description>Web Development, Software Development and Linux Consultancy services</description>
	<lastBuildDate>Fri, 08 Aug 2025 09:18:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Simple Sleeping Linux Kernel Thread</title>
		<link>http://evolution-systems.co.uk/2009/05/26/simple-sleeping-linux-kernel-thread-2/</link>
		<comments>http://evolution-systems.co.uk/2009/05/26/simple-sleeping-linux-kernel-thread-2/#comments</comments>
		<pubDate>Tue, 26 May 2009 12:56:35 +0000</pubDate>
		<dc:creator><![CDATA[Matthew Jakeman]]></dc:creator>
				<category><![CDATA[Matts Blog]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Sleep]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://evolution-systems.co.uk/wordpress/?p=485</guid>
		<description><![CDATA[I am currently implementing the timeout functionality for the NP++ kernel module. Each physical mapping has a function pointer defined in its descriptive struct (struct np_sw) as described here. Until now this hasn&#8217;t actually been implemented but I wanted to create a kernel thread that simply sleeps for 500 milliseconds, then awakes and looks through [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I am currently implementing the timeout functionality for the NP++ kernel module. Each physical mapping has a function pointer defined in its descriptive struct (struct np_sw) as described <a href="http://wiki.mjakeman.co.uk/index.php?title=Implementing_An_NP%2B%2B_Mapping_Module_HOWTO">here</a>.</p>
<p>Until now this hasn&#8217;t actually been implemented but I wanted to create a kernel thread that simply sleeps for 500 milliseconds, then awakes and looks through all the physical mappings available on the system and calls their timeout function.</p>
<p>This is a fairly simple process but in order to achieve it we need to include a couple of new headers :</p>
<p><code>#include &lt;linux/kthread.h&gt;<br />#include &lt;linux/delay.h&gt;</code><br />
The first of these is for the thread related functions needed and the second is to allow us to sleep the thread.</p>
<p>Now we have those included we need to declare a function that will be called when we start the thread as follows :</p>
<p><code>int timeout_thread(void *data)</code><br />
Any kernel thread function needs ro return an int and take a void pointer as the only argument to conform with the function pointer defined the kthread_create() function in kthread.h which the kthread_run() macro is a wrapper for.</p>
<p>Now we need to actually create the thread. I have done this in the modules init_module() function as follows:</p>
<p><code>kthread_run(timeout_thread, NULL, "NP++ Timeout Thread");</code><br />
Now this is all done we simply need to put some implementation into the timeout_thread() function to perform the sleep as follows:</p>
<p><code>while(1)<br />{<br />  set_current_state(TASK_RUNNING);<br />  //Some Code Here<br />  set_current_state(TASK_INTERRUPTIBLE);<br />  msleep(500);<br />}</code><br />
This sets the state of the thread to TASK_RUNNING to tell the kernel that the thread is working. The code to call the mappings individual timeout functions will go here. Then we set the state to TASK_INTERRUPTIBLE to tell the kernel we aren&#8217;t doing anything at which point we call the msleep() function to sleep the thread for 500 milliseconds.</p>
<p>Compile the module and you have a nice thread that will run roughly every 500 milliseconds. If you need a longer delay that can be measured in seconds the delay.h header also defines the following function that takes the number of seconds as the only argument:</p>
<p><code>void ssleep(unsigned int seconds);</code></p>
]]></content:encoded>
			<wfw:commentRss>http://evolution-systems.co.uk/2009/05/26/simple-sleeping-linux-kernel-thread-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
