<?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>Guy Rutenberg &#187; Tutorials</title>
	<atom:link href="http://www.guyrutenberg.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Sat, 14 Jan 2012 11:30:25 +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>Creating a Deb for an Updated Version</title>
		<link>http://www.guyrutenberg.com/2011/12/17/creating-a-deb-for-an-updated-version/</link>
		<comments>http://www.guyrutenberg.com/2011/12/17/creating-a-deb-for-an-updated-version/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 16:16:04 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=928</guid>
		<description><![CDATA[Say you&#8217;ve an existing package like gitg and you want to use the new version of gitg or even apply your own patches. You could directly make install but you will probably regret it as soon as you&#8217;ll want to upgrade/uninstall, and you want to create a better package than the one created by checkinstall. [...]]]></description>
			<content:encoded><![CDATA[<p>Say you&#8217;ve an existing package like <code>gitg</code> and you want to use the new version of <code>gitg</code> or even apply your own patches. You could directly <code>make install</code> but you will probably regret it as soon as you&#8217;ll want to upgrade/uninstall, and you want to create a better package than the one created by <a href="">checkinstall</a>. Apperantly, creating a <code>deb</code> package for a new version of already packaged <code>deb</code> isn&#8217;t complicated.</p>
<h3>Getting Started</h3>
<p>Start by pulling the sources for the already available package. I&#8217;ll by using <code>gitg</code> as an example throughout this tutorial.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">apt-get source gitg</pre></div></div>

<p>This will create a folder according to the version of the package, something like <code>gitg-0.2.4</code>. Extract the new version besides it and <code>cd</code> into its directory. The next step is to copy the <code>debian/</code> directory from the old source package the code you&#8217;ve just extracted.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">cp -R ../gitg-0.2.4/debian/ .</pre></div></div>

<h3>Update <code>debian/</code> Files</h3>
<p>The next step is to update the files under the <code>debian/</code> sub-directory.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DEBEMAIL=&quot;Guy Rutenberg &lt;myemail@domain.com&gt;&quot; debchange --nmu</pre></div></div>

<p>This will update the <code>debian/changelog</code> and set the new version. <code>--nmu</code> will create a new &#8220;non maintainer upload&#8221; version, meaning if the current version was <code>0.2.4-0ubuntu1</code>, it will change it to <code>0.2.4-0ubuntu1.1</code>. This will make sure that there won&#8217;t be any collision between your package and an official one. If you update to a new upstream version. It might be more suitable to use something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">debchange --newversion 0.2.5+20111211.git.20391c4</pre></div></div>

<p>If necessary, update the <code>Build-Depends</code> and <code>Depends</code> sections of <code>debian/control</code>.</p>
<h3>Building the Package</h3>
<p>If your building a package directly from version control and not part of an official release, you may need to run</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./autogen</pre></div></div>

<p>at this point.</p>
<p>Now to the actual building:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">debuild -us -uc -i -I -B</pre></div></div>

<p><code>-us -uc</code> tells the script not to sign the <code>.dsc</code> and <code>.changes</code> files accordingly. <code>-i</code> and <code>-I</code> makes the script ignore common version control files. <code>-B</code> tells <code>debuild</code> to only create binary packages. You can also pass <code>-j</code> followed by the number of simultaneous jobs you wish to allow (e.g. <code>-j3</code>, like in <code>make</code>) which an significantly speed things up.</p>
<h3>Installing the Package</h3>
<p>The package will reside in the parent directory, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">../gitg_0.2.5+20111211.git.20391c4_amd64.deb</pre></div></div>

<p>At this point you&#8217;re basically done. If you want to install the package you can use</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">sudo debi</pre></div></div>

<p>while you&#8217;re still inside the build directory.</p>
<h3>References</h3>
<ul>
<li><a href="https://help.ubuntu.com/community/UpdatingADeb">UpdatingADeb</a></li>
<li>Man pages for <code>debuild</code>, <code>dpgk-genchanges</code>, <code>dpgk-buildpackage</code>.<br />
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2011/12/17/creating-a-deb-for-an-updated-version/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WordPress Administration over SSL on Lighttpd</title>
		<link>http://www.guyrutenberg.com/2010/12/03/wordpress-administration-over-ssl-on-lighttpd/</link>
		<comments>http://www.guyrutenberg.com/2010/12/03/wordpress-administration-over-ssl-on-lighttpd/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 09:46:07 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Lighttpd]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=791</guid>
		<description><![CDATA[In this tutorial we&#8217;ll walk through the steps of enabling SSL (https) for the WordPress&#8217; admin panel when using Lighttpd as a webserver. The tutorial consists of two stages, the first is enabling SSL at the Lighttpd level and the second is in the WordPress level. Lighttpd Setup The first thing to do is to [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we&#8217;ll walk through the steps of enabling SSL (https) for the WordPress&#8217; admin panel when using Lighttpd as a webserver. The tutorial consists of two stages, the first is enabling SSL at the Lighttpd level and the second is in the WordPress level.</p>
<p><a href="/wp-content/uploads/2010/12/ssl-admin.png"><img src="/wp-content/uploads/2010/12/ssl-admin.png" alt="" title="ssl-admin" width="494" height="37" class="aligncenter size-full wp-image-792" /></a><br />
<span id="more-791"></span></p>
<h3> Lighttpd Setup</h3>
<p>The first thing to do is to check that your lighttpd does support SSL. This should be the case for most distros. Look for the <code>(ssl)</code> after the version string.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ lighttpd -v
lighttpd/1.4.26 (ssl) - a light and fast webserver
Build-Date: Jul 17 2010 15:02:18</pre></div></div>

<p>We&#8217;ll generate a self-signed SSL certificate using <code>openssl</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/etc/lighttpd$ sudo openssl req -new -x509 -newkey rsa:2048 -keyout server.pem -out server.pem -days 365 -nodes</pre></div></div>

<p>It will ask you to provide some details. You should specify your domain name in the <code>Common Name</code> section. If you would like to use the domain for multiple subdomains (like <code>guyrutenberg.com</code> and <code>www.guyrutenberg.com</code>), you can use wildcards, e.g. <code>*.guyrutenberg.com</code>.</p>
<p>Now you should change the permissions of the certificate so it&#8217;s read-only and accessible by the root user:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/etc/lighttpd$ sudo chmod 0400 server.pem</pre></div></div>

<p>Now you&#8217;re ready to enable the SSL in the Lighttpd&#8217;s configuration. Edit <code>/etc/lighttpd/lighttpd.conf</code> and insert the following section:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$SERVER[&quot;socket&quot;] == &quot;:443&quot; {
    ssl.engine                  = &quot;enable&quot; 
    ssl.pemfile                 = &quot;/etc/lighttpd/server.pem&quot; 
}</pre></div></div>

<p>Now make sure you didn&#8217;t do any dumb typing/copying mistake and restart the lighttpd service.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ lighttpd -tf /etc/lighttpd/lighttpd.conf
$ sudo /etc/init.d/lighttpd restart</pre></div></div>

<p>Now everything should be set correctly. However, if you experience the following error (happened on my Ubuntu box)</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"> * Starting web server lighttpd
2010-12-03 08:30:05: (network.c.336) SSL: error:00000000:lib(0):func(0):reason(0) 
   ...fail!</pre></div></div>

<p>you should add the following lines to the socket section you&#8217;ve added to the <code>lighttpd.conf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">    ssl.use-sslv2 = &quot;enable&quot;
    ssl.cipher-list = &quot;TLSv1+HIGH RC4+MEDIUM !SSLv2 !3DES !aNULL @STRENGTH&quot;</pre></div></div>

<p>The workaround was taken from <a href="https://bugzilla.redhat.com/show_bug.cgi?id=577546#c1">here</a>. </p>
<p>Now last, but not least, don&#8217;t forget to let HTTPS (port 443) thourgh your firewall.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ sudo ufw allow https</pre></div></div>

<h3>WordPress Setup</h3>
<p>This part is pretty short. Basically you have two options:</p>
<ol>
<li>Enabling SSL for the whole admin session.</li>
<li>Enabling SSL only for login.</li>
</ol>
<p>The former is more secure, while the latter is easier on server performance-wise. I would recommend the former, unless you find it very slow. Enabling SSL for the whole session can be done by adding the following line</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">define('FORCE_SSL_ADMIN', true);</pre></div></div>

<p>to your <code>wp-config.php</code>. Enabling SSL only for login is done by adding</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">  define('FORCE_SSL_LOGIN', true);</pre></div></div>

<p>instead.</p>
<p>If you&#8217;ve followed so far, you should be done and able to access your WordPress admin panel in a secure manner.</p>
<p>Further resources:<br />
<a href="http://redmine.lighttpd.net/wiki/1/Docs:SSL">http://redmine.lighttpd.net/wiki/1/Docs:SSL</a><br />
<a href="http://codex.wordpress.org/Administration_Over_SSL">http://codex.wordpress.org/Administration_Over_SSL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/12/03/wordpress-administration-over-ssl-on-lighttpd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Capturing Video and Converting to H.264 using ffmpeg</title>
		<link>http://www.guyrutenberg.com/2010/09/10/capturing-video-and-converting-to-h-264-using-ffmpeg/</link>
		<comments>http://www.guyrutenberg.com/2010/09/10/capturing-video-and-converting-to-h-264-using-ffmpeg/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 16:21:15 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[H.264]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=726</guid>
		<description><![CDATA[8-millimeter video tapes seem to slowly fade to oblivion. In order to save old family videos recorded in this format, I&#8217;ve decided to digitize them. After a quick try with vlc, I&#8217;ve understood that it wasn&#8217;t the right tool for the task. It crashed with a cryptic error message every time I&#8217;ve tried to encode [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/8_mm_video_format">8-millimeter video tapes</a> seem to slowly fade to oblivion. In order to save old family videos recorded in this format, I&#8217;ve decided to digitize them.</p>
<p>After a quick try with <code>vlc</code>, I&#8217;ve understood that it wasn&#8217;t the right tool for the task. It crashed with a cryptic error message every time I&#8217;ve tried to encode H.264 video, and it seemed that it best suited for real time encoding. Doing real time encoding, is sub-optimal as I can&#8217;t reach high quality encoding is a reasonable bit rate.</p>
<p>So I looked for another tool and recalled <a href="http://ffmpeg.org/">ffmpeg</a>. While ffmpeg provided everything I looked: high quality video encoding using H.264 and stability, it wasn&#8217;t an easy start. ffmpeg&#8217;s defaults are notoriously ill-chosen. After hours of going through man pages, I&#8217;ve managed to capture and convert video tapes into high quality (encoded) digital video.</p>
<p>Basically the process involved capturing the raw video into a temporary file and then preform a two-pass encoding using H.264.<br />
<span id="more-726"></span></p>
<h3>Capturing the raw video</h3>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ffmpeg -i /dev/video0 -t 1:30:00 -vcodec copy /tmp/video.mpg</pre></div></div>

<p>The <code>-vcodec copy</code> tells <code>ffmpeg</code> to copy the raw codec data as is. It allows you to make an identical copy of the video from the capturing device <code>/dev/video0</code>. The <code>-t</code> flag allows you to restrict the video capturing duration.</p>
<h3>Performing the first encoding pass</h3>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ffmpeg -i /tmp/video.mpg -vcodec libx264 -vpre max -threads 0 \
-b 2100k -pass 1 -f mp4 -y /dev/null</pre></div></div>

<p>The <code>-vcodec libx264</code> tells <code>ffmpeg</code> to encode the video using <a href="http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC">H.264</a> encoding. The -vpre flag allows you to set a predefind presets that control (among other things) the quality of the encoded value. I used <code>max</code>, other presets include <code>hq</code> and <code>normal</code>. <code>-threads 0</code> tells <code>ffmpeg</code> to spawn as many threads it deems necessary. <code>-b 2100k</code> tells it to target a bit rate of 2100kbps for video encoding. -pass 1 allows you to perform the first pass out two in a 2-pass encoding. In the first pass <code>ffmpeg</code> will collect statistics and write them to a special log. When doing the second pass <code>ffmpeg</code> can use this special log to better distribute the bit rate so it will achieve a constant quality at the specified average bit rate. The output of the first pass isn&#8217;t important so it&#8217;s directed to <code>/dev/null</code>. The <code>-y</code> switch tells <code>ffmpeg</code> to ignore that this file already exists. As <code>ffmpeg</code> can&#8217;t tell the format of the file by it&#8217;s name (<code>/dev/null</code>) we specify it manually using <code>-f mp4</code>.</p>
<h3>Performing the final encoding pass</h3>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ffmpeg -i /tmp/video.mpg -vcodec libx264 -vpre max -threads 0 \
-b 2100k -pass 2 ~/Videos/video-200108-200201-r1.mp4</pre></div></div>

<p>This one is similiar to the last command except that we finally save the video. The <code>-pass 2</code> tells ffmpeg to perform the second and final pass. It will use the statistics log created by the previous command in order to optimize the encoding. The <code>-f</code> flag isn&#8217;t necessary as the format is indicated by the output file&#8217;s name.</p>
<p>The encoding time is greatly effected by the preset that you chose. The <code>max</code> preset is very slow, which wasn&#8217;t a problem for me in this case, but is something worth remembering. See <a href="http://juliensimon.blogspot.com/2009/01/howto-ffmpeg-x264-presets.html">Julian Simon&#8217;s x264 presets comparison</a> for more information regarding the different presets available.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/09/10/capturing-video-and-converting-to-h-264-using-ffmpeg/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>phpMyAdmin + Lighttpd in Gentoo</title>
		<link>http://www.guyrutenberg.com/2009/01/20/phpmyadmin-lighttpd-in-gentoo/</link>
		<comments>http://www.guyrutenberg.com/2009/01/20/phpmyadmin-lighttpd-in-gentoo/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 16:14:57 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Lighttpd]]></category>
		<category><![CDATA[phpMyAdmin]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=248</guid>
		<description><![CDATA[Usually installing software in Gentoo is a piece of cake. Just emerge what you want and (with the right USE flags) and everything will be ready for you. However, as today I&#8217;ve found out today, installing phpMyAdmin with Lighttpd isn&#8217;t trivial as it should be. In this post I&#8217;ll try to walk you through the [...]]]></description>
			<content:encoded><![CDATA[<p>Usually installing software in Gentoo is a piece of cake. Just <code>emerge</code> what you want and (with the right USE flags) and everything will be ready for you. However, as today I&#8217;ve found out today, installing phpMyAdmin with Lighttpd isn&#8217;t trivial as it should be.</p>
<p>In this post I&#8217;ll try to walk you through the necessary steps to install phpMyAdmin with Lighttpd in Gentoo.<br />
<span id="more-248"></span></p>
<h3>Installing Lighttpd and PHP</h3>
<p>Before installing Lighttpd and PHP you&#8217;ll need to enable some USE flags. For Lighttpd you&#8217;ll need to at least enable the <code>php</code> and <code>fastcgi</code> USE flags. For PHP enable the <code>cgi</code>, <code>crypt</code>, <code>ctype</code>, <code>pcre</code>, <code>session</code>, <code>unicode</code>. The first one is required for working with Lighttpd, while the others are required for phpMyAdmin.</p>
<p>See the <a href="http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&#038;chap=2">handbook</a> for information about enabling USE flags.</p>
<p>Now install the packages</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">emerge -av lighttpd php</pre></div></div>

<p>After installation is complete, start the Lighttpd process</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/etc/init.d/lighttpd start</pre></div></div>

<p>and you should have a working Lighttpd server with PHP support.</p>
<h3>Installing phpMyAdmin</h3>
<p>phpMyAdmin depends on a package called <code>webapp-config</code>. While usually we let Portage take care of dependencies, this time we&#8217;ll have to install <code>webapp-config</code> manually before we install phpMyAdmin, so we can change its configuration for suit Lighttpd.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">emerge -av --oneshot webapp-config</pre></div></div>

<p>After installation is done edit using your favourite editor the <code>/etc/vhosts/webapp-config</code> and change the line:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vhost_server=&quot;apache&quot;</pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">vhost_server=&quot;lighttpd&quot;</pre></div></div>

<p>Now we are ready to install phpMyAdmin.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">emerge -av phpmyadmin</pre></div></div>

<p>After installation is complete, you&#8217;ll need to create a phpMyAdmin configuration file</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">cp /var/www/localhost/htdocs/phpmyadmin/config.sample.inc.php /var/www/localhost/htdocs/phpmyadmin/config.inc.php</pre></div></div>

<p>Now open the newly created configuration file and edit the line with the <code>blowfish_secret</code> (at the top of the file) as explained in it. An easy way to generate the password would be to use <a href="http://www.guyrutenberg.com/2008/05/04/spass-11-secure-password-generator/"><code>spass</code></a>.</p>
<p>Now if you use the default Lighttpd configurations you&#8217;re done. You can access the phpMyAdmin via <code>http://localhost/phpmyadmin</code>. If however you&#8217;ve changed the document root, there is one more step for you. Enable the <code>mod_alias</code> module in <code>/etc/lighttpd/lighttpd.conf</code> and append to the file</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">alias.url += (&quot;/phpmyadmin&quot; =&gt; &quot;/var/www/localhost/htdocs/phpmyadmin&quot;)</pre></div></div>

<p>and don&#8217;t forget to restart the Lighttpd server after making the changes.</p>
<p>You should have by now everything installed and configured to work. If you have any further questions don&#8217;t hesitate to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/01/20/phpmyadmin-lighttpd-in-gentoo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding load average &#8211; A Practitioner Guide</title>
		<link>http://www.guyrutenberg.com/2008/07/25/understanding-load-average-a-practitioner-guide/</link>
		<comments>http://www.guyrutenberg.com/2008/07/25/understanding-load-average-a-practitioner-guide/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 08:50:34 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=61</guid>
		<description><![CDATA[The term &#8220;load average&#8221; is used in many Linux/UNIX utilities. Everybody knows that the numbers the term &#8220;load average&#8221; refers to, usually three numbers, somehow represent the load on the system&#8217;s CPU. In this post I&#8217;ll try making this three numbers clearer and understandable. The easiest way to see the &#8220;load average&#8221; of your system [...]]]></description>
			<content:encoded><![CDATA[<p>The term &#8220;load average&#8221; is used in many Linux/UNIX utilities. Everybody knows that the numbers the term &#8220;load average&#8221; refers to, usually three numbers, somehow represent the load on the system&#8217;s CPU. In this post I&#8217;ll try making this three numbers clearer and understandable.<br />
<span id="more-61"></span><br />
The easiest way to see the &#8220;load average&#8221; of your system is by <code>uptime</code>.It also appears in <code>top</code> and can be graphed in the console by <code>tload</code> . In all three cases the load average refers to a group of three numbers. For example, in the following output of <code>uptime</code></p>
<pre>
10:41:47 up 5 days, 48 min,  1 user,  load average: 0.82, 0.71, 0.66
</pre>
<p>the last three numbers are the &#8220;load average&#8221;. Each number represent the systems load as a moving average over 1, 5 and 15 minutes respectively. Now, the important thing is to understand what is being averaged, the load metric.</p>
<p>The metric that represent the load at a given point in time is  how many process are queued for running at each given time (including the process that is currently being ran). Generally speaking, on a single core machine, this can be looked at as CPU utilization percentage when multiplied by 100. For example if I had a load-average of 0.50 in the last minute, this means that over the last minute half of the time the CPU was idle as it had no running process. On the other hand if I had load average of 2.50 it means that over the last minute an average of 1.5 process were waiting to their turn to run. so the CPU was overloaded by 150%.</p>
<p>On a multi-core systems things are a bit different, but in order to avoid unnecessary complications one can usually divide the load-average by the number of cores an treat the result as the load average of single core machine. For example let&#8217;s say the load average of a two-core machine was 3.00 2.00 0.50. This means that over the last minute we had an average of three runnable process, this means that one process, in average, was queued as there are two core in the machine that can run to process at a time. So the machine was overloaded had a load of 150% its capability. Over the last 5 minutes the load average of 2.00 means that we roughly had 2 process running each time, so the machine was fully utilized but wasn&#8217;t overloaded by work. On the other hand over last 15 minutes the load-average of 0.50 means that we could handle 4 time that load without overloading the CPU, we only had (0.50/2)*100=25% CPU utilization in that 15 minutes.</p>
<p>I hope I made the load-average a bit more clearer using the above example. Load-average is an important metric for measuring a system performance, and good understanding of it is beneficial. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/07/25/understanding-load-average-a-practitioner-guide/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim</title>
		<link>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/</link>
		<comments>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 19:27:11 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[wxWidgets]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/</guid>
		<description><![CDATA[I use Vim as my main IDE for C/C++ related development (as well as for almost all other development). If you use (or thinking about using) vim as as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by the OmniComplete, which is available since Vim 7.0. Just having [...]]]></description>
			<content:encoded><![CDATA[<p>I use Vim as my main IDE for C/C++ related development (as well as for almost all other development). If you use (or thinking about using) vim as as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by the OmniComplete, which is available since Vim 7.0. Just having  the OmniComplete is a nice thing, but it&#8217;s much more helpful if configured properly to work with the libraries you use, such as wxWidgets. In this post I will show you how to get working the OmniComplete for wxWidgets, however, the procedure I will show can be easily adapted to almost all libraries.<br />
<span id="more-41"></span></p>
<p>The first step is to install <code>ctags</code> (if you haven&#8217;t got it already installed), this provides the tags file, on which the autocompletion is based. <code>ctags</code> is available directly from the package manager of all popular distro&#8217;s.</p>
<p>The next step is to install the <a href="http://www.vim.org/scripts/script.php?script_id=1520">OmniCppComplete</a> plugin for vim. The plugin uses the <code>ctags</code>&#8216; generated tags file for the autocompletion. In order for the autocompletion to properly work with classes, you need to create the <code>~/.ctags</code> file and add some default options. Each option should be listed in a newline. Your <code>~/.ctags</code> file should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">--c++-kinds=+p
--fields=+iaS
--extra=+q</pre></div></div>

<p>These options will now be used as default when running <code>ctags</code>.</p>
<p>The next step is to create a tag file for the wxWidgets library. This can be done using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"> ctags -f ~/.vim/wxwidgetstags -R /usr/include/wx-2.8</pre></div></div>

<p>Don&#8217;t forget to replace <code>/usr/include/wx-2.8</code> with the path to the header files of wxWidgets. This will create the tags file under your <code>~/.vim</code> directory.</p>
<p>The next step is to tell vim to use this tags file. This is done by adding the following line to your <code>~/.vimrc</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">:set tags+=~/.vim/wxwidgetstags</pre></div></div>

<p>And now your done. OmniComplete will now work for wxWidgets, listing members and functions for any of the wxWidgets classes.<br />
Remember you can repeat the last two steps for any other libraries you use in order for the OmniComplete to work with them too.</p>
<p>N.B. don&#8217;t forget to run <code>ctags -R .</code> in your projects root dir if you want OmniComplete to work for classes you defined.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating Local SVN Repository (Home Repository)</title>
		<link>http://www.guyrutenberg.com/2007/10/29/creating-local-svn-repository-home-repository/</link>
		<comments>http://www.guyrutenberg.com/2007/10/29/creating-local-svn-repository-home-repository/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 06:39:36 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/10/29/creating-local-svn-repository-home-repository/</guid>
		<description><![CDATA[In this tutorial I will explain how to create a local Subversion (SVN) repository, intended for a single user. I assume that you already know the benefits of keeping track of old revision of projects or important documents such as a resume or a thesis you have been writing. Subversion offers you a very convenient [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will explain how to create a local Subversion (SVN) repository, intended for a single user. I assume that you already know the benefits of keeping track of old revision of projects or important documents such as a resume or a thesis you have been writing. Subversion offers you a very convenient yet strong method to do so, and the easiest way to do so with Subversion (SVN) is to create a local, home, repository intended for a single user &#8211; you.<br />
<span id="more-26"></span><br />
The repository we are going to create will be used by a single user, working locally on the machine. In this tutorial I will assume the repository will be created for a project called &#8220;project1&#8243;. &#8220;project1&#8243; can be a real project you are doing, a paper you are writing or any thing else that can be stored under revision control (that almost everything).</p>
<h4>Creating the repository</h4>
<p>We will start by creating the repository. From the command line do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ mkdir /home/user/svnrep
$ cd ~/svnrep
$ svnadmin create project1</pre></div></div>

<p>In the first line we create a directory to house all of your repositories (I assume you are working under the username &#8220;user&#8221;). I prefer to use different repositories for different projects that are unrelated, no matter how small they are. Because I use many repositories, I prefer to have a single directory  underneath all repositories will reside in an organized way (i.e. no junk files or any other kind of stuff except directories). Next thing, is to <code>cd</code> into the directory we created, and actually create the repository using the <code>svnadmin</code> command. To repository we&#8217;ve created is called &#8220;project1&#8243;. </p>
<p>The brand new &#8220;project1&#8243; repository is currently empty and in revision 0. This will change once we put some data in it. The first thing you do with a new repository is to import initial project data into it.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn import /home/user/project1 file:///home/user/svnrep/project1/trunk -m &quot;Initial import of project1&quot;</pre></div></div>

<p>Will import the current project1&#8242;s file into the repository (assuming that project1 indeed resides in <code>/home/user/project1</code>). The </code>trunk</code> appended to the end of the repository URL, is part of the directory layout convention used by many Subversion users. The last part of the command is the message that will be attached to the import in the SVN log.</p>
<p>Now the repository holds data and you are ready to checkout the code from it, and start working.</p>
<h4>Checkout a Working Copy and Start Working</h4>
<p>When using SVN (as well as in most revision control systems) you don't work directly on the repository. Instead you checkout a working copy from it, and work on this copy. To checkout a working copy from the repository, use:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn co file:///home/user/svnrep/project1/trunk /home/user/project1_work</pre></div></div>

<p>This will create a working copy of the repository under <code>/home/user/project1_work</code>. You can edit this copy safely. </p>
<p>After you've done editing your working copy, you will want to commit those changes back to the repository. Assuming you are already inside the directory of the working copy, just do:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn commit -m &quot;Some log message&quot;</pre></div></div>

<p>This will send you changes back to the repository and store them there. Change the "Some log message" to some useful short description of the changes you've made.</p>
<h4>Some Useful Commands</h4>
<p>To view a list of log messages that were attached to the operations on the repository, use:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn log
$ svn log -r 5:HEAD</pre></div></div>

<p>The first command will print all log messages. The second command will print the log messages from revision 5 to the latest revision. You can substitute the <code>HEAD</code> with a number to get the log messages of the revisions up to a specific one.</p>
<p>To view the changes you made before committing your code, use:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn diff</pre></div></div>

<p>Another important command is the one to update your working copy with the latest revision from the repository. This can be done using:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ svn update</pre></div></div>

<p>However, since your are the only user of the local repository, you won't have to use this function often (if at all), unless you use two, or more, working copies for your project.</p>
<p>This concludes this tutorial. I hope you know by now how to create and use, a single user local SVN repository. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/10/29/creating-local-svn-repository-home-repository/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>ssh-keygen Tutorial &#8211; Generating RSA and DSA keys</title>
		<link>http://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/</link>
		<comments>http://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 09:32:26 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ssh-keygen]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/</guid>
		<description><![CDATA[In this post I will walk you through generating RSA and DSA keys using ssh-keygen. Public key authentication for SSH sessions are far superior to any password authentication and provide much higher security. ssh-keygen is the basic way for generating keys for such kind of authentication. I will also explain how to maintain those keys [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will walk you through generating RSA and DSA keys using <code>ssh-keygen</code>. Public key authentication for SSH sessions are far superior to any password authentication and provide much higher security. <code>ssh-keygen</code> is the basic way for generating keys for such kind of authentication. I will also explain how to maintain those keys by changing their associated comments and more importantly by changing the passphrases using this handy utility.<br />
<span id="more-22"></span></p>
<h4>Generating Keys</h4>
<p>Generating public keys for authentication is the basic and most often used feature of <code>ssh-keygen</code>. <code>ssh-keygen</code> can generate both RSA and DSA keys. RSA keys have a minimum key length of 768 bits and the default length is 2048. When generating new RSA keys you should use at least 2048 bits of key length unless you really have a good reason for using a shorter and less secure key. The key length for DSA is always 1024 bits as specified in <a href="http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf">FIPS 186-2</a>. Because DSA key length is limited to 1024, and RSA key length isn&#8217;t limited, so one can generate much stronger RSA keys than DSA keys, I prefer using RSA over DSA. Another reason for not using DSA is that DSA is a government standard and one may wonder if the key length was limited deliberately so it will be possible for government agencies to decrypt it.</p>
<p>To generate a pair of public and private keys execute the following command:<br />
<code>ssh-keygen -t rsa -b 2048</code><br />
You can use &#8220;dsa&#8221; instead of the &#8220;rsa&#8221; after the <code>-t</code> to generate a DSA key. The number after the <code>-b</code> specifies the key length in bits.</p>
<p>After executing the command it may take some time to generate the keys (as the program waits for enough entropy to be gathered to generate random numbers). When the key generation is done you would be prompted to enter a filename in which the key will be saved. The public key will have the same filename but it will end with <code>.pub</code>. You should make sure that the key can only be read by you and not by any other user for security reasons. </p>
<p>Next you&#8217;ll be prompted to enter a passphrase. Each generated key can be protected by a passphrase. When a key is generated with a passphrase, the key can&#8217;t be used without the passphrase, so by using a passphrase one can prevent others from using his private keys without first guessing the passphrase. A good passphrase should be at least 10 characters long. One should stay away from English sentences as their entropy level is just too low to be used as a safe passphrase. I usually use a randomly generated passphrase, as this kind is considered the most secure. If you intend to use the key for accessing a remote machine from inside an automated script you may wish to enter an empty password, so the script won&#8217;t need user interaction. In this case just press <code>&lt;ENTER&gt;</code> twice.<br />
If you create a passphrase-less key just make sure you only put it on trusted hosts as it may compromise the remote machine if the key falls to the wrong hands.</p>
<p>After entering you passphrase twice the program will print the key fingerprint, which is some kind of hashing used to distinguish different keys, followed by the default key comment (more on key comments later). After printing the key information the program will terminate. Congratulations, you&#8217;ve just created you own public key using <code>ssh-keygen</code>.</p>
<h4>Comments</h4>
<p>Adding comments to keys can allow you to organize your keys more easily. The comments are stored in end of the public key file and can be viewed in clear text. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">cat id_rsa2.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyyA8wePstPC69PeuHFtOwyTecByonsHFAjHbVnZ+h0dpomvLZxUtbknNj3+c7MPYKqKBOx9gUKV/diR/mIDqsb405MlrI1kmNR9zbFGYAAwIH/Gxt0Lv5ffwaqsz7cECHBbMojQGEz3IH3twEvDfF6cu5p00QfP0MSmEi/eB+W+h30NGdqLJCziLDlp409jAfXbQm/4Yx7apLvEmkaYSrb5f/pfvYv1FEV1tS8/J7DgdHUAWo6gyGUUSZJgsyHcuJT7v9Tf0xwiFWOWL9WsWXa9fCKqTeYnYJhHlqfinZRnT/+jkz0OZ7YmXo6j4Hyms3RCOqenIX1W6gnIn+eQIkw== This is the key's comment</pre></div></div>

<p>As you can see the comment is appended in clear text to the end of the public key file. To alter the comment just edit the public key file with a plain text editor such as <code>nano</code> or <code>vim</code>.<br />
To add a comment to the public key file when generating the key add to the key generation command <code>-C "you comment"</code>. For example to generate 4048 bit RSA key with &#8220;home machine&#8221; as a comment you will do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ssh-keygen -b 4048 -t rsa -C &quot;home machine&quot;</pre></div></div>

<p>Notice that each copy of a public key can have its own comment and you cannot retrieve the comment from the private key.</p>
<h4>Passphrases</h4>
<p>Passphrases allow you to prevent unauthorized usage of your key by meaning of protecting the key itself by a password. Although your ssh directory holding the private keys should be unaccessible to other users, the root user of the system, or anyone who can achieve is privileges can access your key and copy it. In this case the passphrase will prevent him from using it. </p>
<p>To add a passphrase to a key just type it when prompted during the key generation process. Keep in mind that the password must be at least 5 characters long. A good passphrase, as I said before, should be at least 10 characters long, and consist of random upper and lower case letters, numbers and symbols.</p>
<p>While the passphrase boosts the security of the key, under some conditions you may want to leave it empty. Leaving the passphrase empty allows you to use the key from within scripts, for example to transfer a file via <code>scp</code>. While passphraseless keys are very useful for scripts just remember to only use them at trusted machines.</p>
<p>You can change the passphrase of key after it&#8217;s been created, and you should do it at least annually. To change the passphrase execute:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ssh-keygen -p</pre></div></div>

<p>After this you will be prompted to enter the location of your private key and enter twice the new passphrase. If you don&#8217;t want a passphrase just enter empty one.</p>
<h4>End Notes</h4>
<p>Feel free to share your public key, as its name suggests, it should be public. Keep in mind that your private key should be kept private. If someone gets hold of your private key, change it imediatly, even if it&#8217;s passphrase protected.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Profiling Code Using clock_gettime</title>
		<link>http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/</link>
		<comments>http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 08:55:16 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/</guid>
		<description><![CDATA[After raising the issue of the low resolution problem of the timer provided by clock() in Resolution Problems in clock(), I&#8217;ve ended the post by mentioning to two more functions that should provide high-resolution timers suitable for profiling code. In this post I will discuss one of them, clock_gettime(). The clock_gettime() provides access to several [...]]]></description>
			<content:encoded><![CDATA[<p>After raising the issue of the low resolution problem of the timer provided by <code>clock()</code> in <a href="">Resolution Problems in <code>clock()</code></a>, I&#8217;ve ended the post by mentioning to two more functions that should provide high-resolution timers suitable for profiling code. In this post I will discuss one of them, <code>clock_gettime()</code>.<br />
<span id="more-18"></span><br />
The <code>clock_gettime()</code> provides access to several useful timers with the resolution of nanoseconds. First, the prototype for the function is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> clock_gettime<span style="color: #009900;">&#40;</span>clockid_t clk_id<span style="color: #339933;">,</span> <span style="color: #993333;">struct</span> timespect <span style="color: #339933;">*</span>tp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>clk_id</code> allows us to select a specific clock from the several<br />
offered by the system, which includes:</p>
<ul>
<li>
	<code>CLOCK_REALTIME</code>, a system-wide realtime clock.
	</li>
<li>
	<code>CLOCK_PROCESS_CPUTIME_ID</code>, high-resolution timer provided by the CPU for each process.
	</li>
<li>
	<code>CLOCK_THREAD_CPUTIME_ID</code>, high-resolution timer provided by the CPU for each of the threads.
	</li>
</ul>
<p>Usually, there are more clocks provided, but I find these three the most useful as they allow to get the execution time spent in the system level, process level and the thread level.</p>
<p>The current clock time, for the chosen clock is stored int the struct provided by the <code>*tp</code> pointer. The <code>timespec</code> struct is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> timespec <span style="color: #009900;">&#123;</span>
	time_t tv_sec<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* seconds */</span>
	<span style="color: #993333;">long</span> tv_nsec<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* nanoseconds */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To time the processing time some function took, one should call <code>clock_gettime()</code> twice. Once before the function call and once right after it and subtract the returned timings to get the actual runtime.</p>
<p>Getting the difference between two timespec structs isn&#8217;t very complicated and can be acheived using the function <code>diff()</code> defined bellow:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">timespec diff<span style="color: #009900;">&#40;</span>timespec start<span style="color: #339933;">,</span> timespec end<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	timespec temp<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		temp.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		temp.<span style="color: #202020;">tv_nsec</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1000000000</span><span style="color: #339933;">+</span>end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		temp.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">;</span>
		temp.<span style="color: #202020;">tv_nsec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> temp<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now let&#8217;s move to some real example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;iostream&gt;</span>
<span style="color: #339933;">#include &lt;time.h&gt;</span>
using namespace std<span style="color: #339933;">;</span>
&nbsp;
timespec diff<span style="color: #009900;">&#40;</span>timespec start<span style="color: #339933;">,</span> timespec end<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	timespec time1<span style="color: #339933;">,</span> time2<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> temp<span style="color: #339933;">;</span>
	clock_gettime<span style="color: #009900;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>time1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">242000000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		temp<span style="color: #339933;">+=</span>temp<span style="color: #339933;">;</span>
	clock_gettime<span style="color: #009900;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>time2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	cout<span style="color: #339933;">&lt;&lt;</span>diff<span style="color: #009900;">&#40;</span>time1<span style="color: #339933;">,</span>time2<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;:&quot;</span><span style="color: #339933;">&lt;&lt;</span>diff<span style="color: #009900;">&#40;</span>time1<span style="color: #339933;">,</span>time2<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">&lt;&lt;</span>endl<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
timespec diff<span style="color: #009900;">&#40;</span>timespec start<span style="color: #339933;">,</span> timespec end<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	timespec temp<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		temp.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		temp.<span style="color: #202020;">tv_nsec</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1000000000</span><span style="color: #339933;">+</span>end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		temp.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_sec</span><span style="color: #339933;">;</span>
		temp.<span style="color: #202020;">tv_nsec</span> <span style="color: #339933;">=</span> end.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">-</span>start.<span style="color: #202020;">tv_nsec</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> temp<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To use <code>clock_gettime</code> you need to include <code>time.h</code> and to link to <code>librt.a</code>. If you use <code>gcc</code> just make sure you add <code>-lrt</code> to your list of arguments.</p>
<p>Play a bit with the length of the for loop. As you can see <code>clock_gettime</code> provides much more accurate results and can register very short processing time too. Just remember that as the case with any profiling functions, this function adds a little overhead to your program, so make sure you disable the profiling code, using preprocessor commands for example, in the production release.</p>
<h4>26/9/2007 &#8211; Update</h4>
<p>You may want to take a look at <a href="/2007/09/26/the-revised-string-iteration-benchmark/">The Revised String Iteration Benchmark</a> post for another, larger, example of using <code>clock_gettime</code> to time performance of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Introduction to C++ CGI &#8211; Processing Forms</title>
		<link>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/</link>
		<comments>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/#comments</comments>
		<pubDate>Fri, 07 Sep 2007 19:11:59 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cgi]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/</guid>
		<description><![CDATA[In this post I will show you how to process HTML forms easily using CGIs in C++. I assume you have already basic knowledge of writing CGIs in C++, if you don&#8217;t go a head and read Introduction to C++ CGI. Processing forms is the basic function of any CGI script and the main purpose [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will show you how to process HTML forms easily using CGIs in C++. I assume you have already basic knowledge of writing CGIs in C++, if you don&#8217;t go a head and read <a href="/2007/08/10/introduction-to-c-cgi/">Introduction to C++ CGI</a>.</p>
<p>Processing forms is the basic function of any CGI script and the main purpose of CGIs. As you probably know there are two common ways to send form data back to the web server: &#8220;post&#8221; and &#8220;get&#8221;. When form data is sent with the &#8220;get&#8221; method it is appended to the URL string of the form submission URL. The &#8220;post&#8221; method is much like the &#8220;get&#8221; except the data is transmitted via http headers and not via the URL itself. When a form uses &#8220;get&#8221; it allows the user to easily bookmark the query created by the form as the data is transmitted in URL itself, on the other hand the &#8220;post&#8221; method allows to send much more data and spares to user from seeing the data in the URL.</p>
<p>Getting the &#8220;post&#8221; and &#8220;get&#8221; data is relatively easy. To get the data sent by &#8220;get&#8221; you can just call <code>getenv("QUERY_STRING")</code> and you will receive a pointer to null-terminated string containing the &#8220;get&#8221; data. Reading the &#8220;post&#8221; data is a bit more complicated. The data needs to be read from the standard input, but the program won&#8217;t receive an EOF when it reaches the end of the data but instead it should stop reading after reading a specified amount of bytes, which is defined in the environment variable &#8220;<code>CONTENT_LENGTH</code>&#8220;. So you should read <code>getenv("CONTENT_LENGTH")</code> bytes from the standard input to receive the &#8220;post&#8221; data. </p>
<p><span id="more-13"></span></p>
<p>But here it&#8217;s get tricky. The data is received in an encoded way. The data for both &#8220;post&#8221; and &#8220;get&#8221; is a string in the form of &#8220;<code>var1=value1&#038;var2=value2&#038;var3=value3</code>&#8221; and so on, where the &#8220;<code>var*</code>&#8221; is the names of the submitted form elements and &#8220;<code>value*</code>&#8221; is their values respectively. And now it gets a bit more tricky, both the elements&#8217; names and values are <a href="http://en.wikipedia.org/wiki/Url_encoding">url-encoded</a>. That means that almost every non alphanumeric character will be encoded as %XX, where XX represents its ASCII value (see the linked Wikipedia article for more information and extra reading). So to sum up the process of processing forms in CGI you should do the following:</p>
<ol>
<li>Get the data (either by &#8220;post&#8221; or &#8220;get&#8221;).</li>
<li>Parse the data to get key-value pairs.</li>
<li>Decode the url-encoded key-value pairs.</li>
<li>Process the data.</li>
</ol>
<p>Getting the first and last step done is easy. The second and third step require some work, but fortunately when done properly they should only have to be done once, as you will create functions/classes to handle those, so it is really only a one time job. That leaves you only to do yourself the first and last steps. </p>
<p>As I said the second and third steps are troublesome only when not done before. <a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>Here is</a> an implementation for those steps that I&#8217;ve written as a part of (yet unreleased) CGI library for C++. It implements functions that take the care of the first three steps and return to the user an STL map containing the form elements&#8217; names in the key and their value in the map pair&#8217;s value. This functions allows to process forms in a similar way to processing forms in PHP using <code>$_GET</code> and <code>$_POST</code>. </p>
<p>Here is a simple example of processing a &#8220;get&#8221; form using the above <code><a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>getpost.h</a></code> header file.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;map&gt;</span>
<span style="color: #339900;">#include &quot;getpost.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	map<span style="color: #000080;">&lt;</span>string,string<span style="color: #000080;">&gt;</span> Get<span style="color: #008080;">;</span>
	initializeGet<span style="color: #008000;">&#40;</span>Get<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//notice that the variable is passed by reference!</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Content-type: text/html&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;html&gt;&lt;body&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;h1&gt;Processing forms&lt;/h1&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;form method=<span style="color: #000099; font-weight: bold;">\&quot;</span>get<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;label for=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;First name: &lt;/label&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;br&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;label for=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Last name: &lt;/label&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;br&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Get.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;fname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>Get.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> Get.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;lname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>Get.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello &quot;</span><span style="color: #000080;">&lt;&lt;</span>Get<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;fname&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &quot;</span><span style="color: #000080;">&lt;&lt;</span>Get<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;lname&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;, isn<span style="color: #000099; font-weight: bold;">\'</span>t &quot;</span>
			<span style="color: #FF0000;">&quot;processing CGI forms with C++ quite easy?&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Fill up the above from and press submit&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>While this is a simple example, it&#8217;s enough to demonstarte how stuff is done. The same way you can process the &#8220;post&#8221; variables. Feel free to take a look at the <code><a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>getpost.h</a></code> source-code to see how the URL decoding and the other implementation aspects were done.</p>
<p><strong>Update 2009-09-18:</strong> There was a bug in <code>getpost.h</code>, see my comment bellow.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.568 seconds -->

