<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0"><channel><title>Codingfreak</title><link>http://codingfreak.blogspot.com/</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingfreak" /><description></description><language>en</language><managingEditor>noreply@blogger.com (Ajith)</managingEditor><lastBuildDate>Tue, 31 Jan 2012 20:17:45 PST</lastBuildDate><generator>Blogger</generator><atom:id xmlns:atom="http://www.w3.org/2005/Atom">tag:blogger.com,1999:blog-4070434216759110691</atom:id><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">71</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingfreak" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codingfreak" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>12.9833</geo:lat><geo:long>77.5833</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">codingfreak</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/http://feeds.feedburner.com/codingfreak" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://my.feedlounge.com/external/subscribe?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://static.feedlounge.com/buttons/subscribe_0.gif">Subscribe with FeedLounge</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.addtoany.com/?linkname=Codingfreak&amp;linkurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak&amp;type=feed" src="http://www.addtoany.com/addfr-b.gif">Add to Any Feed Reader</feedburner:feedFlare><item><title>Notification Chains in Linux Kernel - Part 02</title><link>http://codingfreak.blogspot.com/2012/01/notification-chains-in-linux-kernel.html</link><category>Linux</category><category>Kernel</category><author>noreply@blogger.com (Ajith)</author><pubDate>Mon, 16 Jan 2012 19:32:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2998264715813985257</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/e06pcderibvokUtxQHWAh2PhdE4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/e06pcderibvokUtxQHWAh2PhdE4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/e06pcderibvokUtxQHWAh2PhdE4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/e06pcderibvokUtxQHWAh2PhdE4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;b&gt;Blocking Notifier chains&lt;/b&gt;&lt;br /&gt;
A blocking notifier chain runs in the process context. The calls in the notification list could be blocked as it runs in the process context. Notifications that are not highly time critical could use blocking notifier chains. Linux modules use blocking notifier chains to inform the modules on a change in QOS value or the addition of a new device.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

186 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
187         struct notifier_block *n)
188 {
.
199     down_write(&amp;amp;nh-&amp;gt;rwsem);
200     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
201     up_write(&amp;amp;nh-&amp;gt;rwsem);
202     return ret;
203 }
204 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register)
.
216 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
217         struct notifier_block *n)
218 {
.
229     down_write(&amp;amp;nh-&amp;gt;rwsem);
230     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
231     up_write(&amp;amp;nh-&amp;gt;rwsem);
232     return ret;
233 }
234 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;&lt;span class="fullpost"&gt;
&lt;b&gt;Raw Notifier chains&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
A raw notifier chain does not manage the locking and protection of the callers. Also, there are no restrictions on callbacks, registration, or de-registration. It provides flexibility to the user to have individual lock and protection mechanisms. Linux uses the raw notifier chain in low-level events. &lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

297 int raw_notifier_chain_register(struct raw_notifier_head *nh,
298         struct notifier_block *n)
299 {
300     return notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
301 }
302 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
.
314 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
315         struct notifier_block *n)
316 {
317     return notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
318 }
319 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;SRCU Notifier chains&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Sleepable Read Copy Update (SRCU)&lt;/b&gt; notifier chains are similar to the blocking notifier chain and run in the process context. It differs in the way it handles locking and protection. The SRCU methodology brings in less overhead when we notify the registered callers. On the flip side, it consumes more resource while unregistering. So it is advisable to choose this methodology where we use the notifier call often and where there’s very little requirement for removing from the chain.&amp;nbsp;
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

370 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
371         struct notifier_block *n)
372 {
.
383     mutex_lock(&amp;amp;nh-&amp;gt;mutex);
384     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
385     mutex_unlock(&amp;amp;nh-&amp;gt;mutex);
386     return ret;
387 }
388 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register)
.
400 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
401         struct notifier_block *n)
402 {
.
413     mutex_lock(&amp;amp;nh-&amp;gt;mutex);
414     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
415     mutex_unlock(&amp;amp;nh-&amp;gt;mutex);
416     synchronize_srcu(&amp;amp;nh-&amp;gt;srcu);
417     return ret;
418 }
419 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Registering and Un-registering with a Chain&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
When a kernel component is interested in the events of a given notification chain, it can register it with the general function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_register&lt;/span&gt; and to unregister we can use the function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_unregister&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; The kernel also provides a set of wrappers around &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_register&lt;/span&gt; which we have discussed in notifier chain types.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 20 static int notifier_chain_register(struct notifier_block **nl,
 21         struct notifier_block *n)
 22 {
 23     while ((*nl) != NULL) {
 24         if (n-&amp;gt;priority &amp;gt; (*nl)-&amp;gt;priority)
 25             break;
 26         nl = &amp;amp;((*nl)-&amp;gt;next);
 27     }
 28     n-&amp;gt;next = *nl;
 29     rcu_assign_pointer(*nl, n);
 30     return 0;
 31 }&lt;/pre&gt;
&lt;br /&gt;
For each chain, the notifier_block instances are inserted into a list, which is sorted by priority. Elements with the same priority are sorted based on insertion time: new ones go to the tail.

Accesses to the notification chains are protected by the notifier_lock lock. The use of a single lock for all the notification chains is not a big constraint and does not affect performance, because subsystems usually register their notifier_call functions only at boot time or at module load time, and from that moment on access the lists in a read-only manner (that is, shared).&lt;br /&gt;
&lt;br /&gt;
Because the notifier_chain_register function is called to insert callbacks into all lists, it requires that the list be specified as an input parameter. However, this function is rarely called directly; generic wrappers are used instead.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 33 static int notifier_chain_unregister(struct notifier_block **nl,
 34         struct notifier_block *n)
 35 {
 36     while ((*nl) != NULL) {
 37         if ((*nl) == n) {
 38             rcu_assign_pointer(*nl, n-&amp;gt;next);
 39             return 0;
 40         }
 41         nl = &amp;amp;((*nl)-&amp;gt;next);
 42     }
 43     return -ENOENT;
 44 }&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-2998264715813985257?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=OfnjyIlDWfM:g236sv3ybiM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=OfnjyIlDWfM:g236sv3ybiM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/OfnjyIlDWfM" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-01-17T09:04:33.922+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Notification Chains in Linux Kernel - Part 01</title><link>http://codingfreak.blogspot.com/2012/01/notification-chains-in-linux-part-01.html</link><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Mon, 16 Jan 2012 05:03:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6847357699726064053</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/GIDRHyKZ2Yr_v2XJDbvxOi41mUo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GIDRHyKZ2Yr_v2XJDbvxOi41mUo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/GIDRHyKZ2Yr_v2XJDbvxOi41mUo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GIDRHyKZ2Yr_v2XJDbvxOi41mUo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Linux is a monolithic kernel. Its subsystems or modules help to keep the kernel light by being flexible enough to load and unload at runtime. In most cases, the kernel modules are interconnected to one another. An event captured by a certain module might be of interest to another module. &lt;br /&gt;
&lt;br /&gt;
Typically, communication systems implement request-reply messaging, or polling. In such models, a program that receives a request will have to send the data available since the last transaction. Such methods sometimes require high bandwidth or they waste polling cycles.&lt;br /&gt;
&lt;br /&gt;
To fulfill the need for interaction, Linux uses so called &lt;b&gt;notification chains&lt;/b&gt;. These notifier chains work in a publish-subscribe model. This model is more effective when compared to polling or the request-reply model.&lt;br /&gt;
&lt;br /&gt;
For each notification chain there is a passive side (&lt;b&gt;the notified&lt;/b&gt;) and an active side (&lt;b&gt;the notifier&lt;/b&gt;), as in the so-called publish-and-subscribe model:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The notified are the subsystems that ask to be notified about the event and that provide a callback function to invoke.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;The notifier is the subsystem that experiences an event and calls the callback function.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;struct notifier_block&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
The elements of the notification chain's list are of type &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_block&lt;/span&gt;:&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;include/linux/notifier.h&amp;gt;

 50 struct notifier_block {
 51     int (*notifier_call)
(struct notifier_block *, unsigned long, void *);
 52     struct notifier_block *next;
 53     int priority;
 54 };
&lt;/pre&gt;
&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call&lt;/span&gt; - function to execute.&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;
&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;next&lt;/span&gt; - used to link together the elements of the list.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;priority&lt;/span&gt; - the priority of the function. Functions with higher priority are executed first. But in practice, almost all registrations leave the priority out of the notifier_block definition, which means it gets the default value of 0 and execution order ends up depending only on the registration order (i.e., it is a semirandom order). 
&lt;/li&gt;&lt;/ul&gt;
&lt;span class="fullpost"&gt;
The &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_block&lt;/span&gt; data structure is a simple linked list of function pointers. The function pointers are registered with ‘functions’ that are to be called when an event occurs. Each module needs to maintain a notifier list. The functions are registered to this notification list.

The notification module (publisher) maintains a list head that is used to manage and traverse the notifier block list. The function that subscribes to a module is added to the head of the module’s list by using the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;register_xxxxxx_notifier&lt;/span&gt; API and deletion from the list is done using &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;unregister_xxxxxx_notifier&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Types of Notifier Chains&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
Notifier chains are broadly classified based on the context in which they are executed and the lock/protect mechanism of the calling chain. Based on the need of the module, the notifiers can be executed in the process context or interrupt/atomic context. Thus, notifier chains are classified into four types:
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: small;"&gt;Atomic Notifier Chains&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
As the name indicates, this notifier chain is executed in interrupt or atomic context. Normally, events that are time critical use this notifier. This also means it is a non-blockable call. Linux modules use atomic notifier chains to inform watchdog timers or message handlers. For example, register_keyboard_notifier uses atomic_notifier_chain_register to get called back on keyboard events. This notifier is usually called from the interrupt context.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 96 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
 97         struct notifier_block *n)
 98 {
.
102     spin_lock_irqsave(&amp;amp;nh-&amp;gt;lock, flags);
103     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
104     spin_unlock_irqrestore(&amp;amp;nh-&amp;gt;lock, flags);
105     return ret;
106 }
107 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
.
118 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
119         struct notifier_block *n)
120 {
.
124     spin_lock_irqsave(&amp;amp;nh-&amp;gt;lock, flags);
125     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
126     spin_unlock_irqrestore(&amp;amp;nh-&amp;gt;lock, flags);
127     synchronize_rcu();
128     return ret;
129 }
130 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);&lt;/pre&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-6847357699726064053?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=7PZHlz24aEY:tVbCQOSzAjo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=7PZHlz24aEY:tVbCQOSzAjo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/7PZHlz24aEY" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-01-16T18:51:55.186+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Printing logs based on log levels in C</title><link>http://codingfreak.blogspot.com/2010/08/printing-logs-based-on-log-levels-in-c.html</link><category>C</category><category>Programming</category><author>noreply@blogger.com (Ajith)</author><pubDate>Sat, 07 Aug 2010 07:35:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7501749357954447631</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7YVi5KZTcU6zHQI1TzsqNtGTNDY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7YVi5KZTcU6zHQI1TzsqNtGTNDY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7YVi5KZTcU6zHQI1TzsqNtGTNDY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7YVi5KZTcU6zHQI1TzsqNtGTNDY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-weight: bold; color: rgb(255, 0, 0);"&gt;LOG LEVELS ??&lt;/span&gt;&lt;br /&gt;As per my definition LOG LEVEL means a way to differentiate the importance of logs in our application. We can divide the logs into categories based on their importance and effect for e.g. &lt;span style="font-weight: bold;"&gt;ERROR&lt;/span&gt; logs are more important than &lt;span style="font-weight: bold;"&gt;DEBUG&lt;/span&gt; logs.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 0, 0);"&gt;Why do we need to print logs based on LOG LEVEL ??&lt;/span&gt;&lt;br /&gt;It is really helpful in projects with millions of lines of source code where the user can't use &lt;span style="font-weight:bold;"&gt;#defines&lt;/span&gt; or #ifdef's in order to maintain DEBUG prints. It is really tiresome to maintain &lt;span style="font-weight:bold;"&gt;#defines&lt;/span&gt; and #ifdef atleast for printing logs.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;printk&lt;/span&gt; which is a part of LINUX KERNEL supports printing logs based on LOG LEVEL and it is really helpful in debugging kernel.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;printf&lt;/span&gt; or any of its brothers &amp;amp; sisters don't support the option to print logs depending upon the log levels.&lt;br /&gt;&lt;br /&gt;I have written a sample program where we can create our own printf function with LOG LEVEL. At present I am just supporting the option to print a character, string and a integer.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;&lt;br /&gt;//LOG LEVELS&lt;br /&gt;typedef enum&lt;br /&gt;{&lt;br /&gt;  LOG_DEFAULT,&lt;br /&gt;  LOG_INFO,&lt;br /&gt;  LOG_ERROR,&lt;br /&gt;  LOG_DEBUG&lt;br /&gt;}LOG_LEVEL;&lt;br /&gt; &lt;br /&gt;void LOG_TRACE(LOG_LEVEL lvl, char *fmt, ... );&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;  int i =10;&lt;br /&gt;  char *string="Hello World";&lt;br /&gt;  char c='a';&lt;br /&gt;       &lt;br /&gt;  LOG_TRACE(LOG_INFO, "String - %s\n", string);&lt;br /&gt;  LOG_TRACE(LOG_DEBUG, "Integer - %d\n", i);&lt;br /&gt;  LOG_TRACE(LOG_INFO, "Character - %c\n", c);&lt;br /&gt;       &lt;br /&gt;  LOG_TRACE(LOG_INFO, "\nTOTAL DATA: %s - %d - %c\n", string, i, c);&lt;br /&gt;  return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* LOG_TRACE(log level, format, args ) */&lt;br /&gt;void LOG_TRACE(LOG_LEVEL lvl, char *fmt, ... )&lt;br /&gt;{&lt;br /&gt;  va_list  list;&lt;br /&gt;  char *s, c;&lt;br /&gt;  int i;&lt;br /&gt;&lt;br /&gt;  if( (lvl==LOG_INFO) || (lvl==LOG_ERROR))&lt;br /&gt;  {&lt;br /&gt;     va_start( list, fmt );&lt;br /&gt;&lt;br /&gt;     while(*fmt)&lt;br /&gt;     {&lt;br /&gt;        if ( *fmt != '%' )&lt;br /&gt;           putc( *fmt, stdout );&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;           switch ( *++fmt )&lt;br /&gt;           {&lt;br /&gt;              case 's':&lt;br /&gt;                 /* set r as the next char in list (string) */&lt;br /&gt;                 s = va_arg( list, char * );&lt;br /&gt;                 printf("%s", s);&lt;br /&gt;                 break;&lt;br /&gt;&lt;br /&gt;              case 'd':&lt;br /&gt;                 i = va_arg( list, int );&lt;br /&gt;                 printf("%d", i);&lt;br /&gt;                 break;&lt;br /&gt;&lt;br /&gt;              case 'c':&lt;br /&gt;                 c = va_arg( list, int);&lt;br /&gt;                 printf("%c",c);&lt;br /&gt;                 break;&lt;br /&gt;&lt;br /&gt;              default:&lt;br /&gt;                 putc( *fmt, stdout );&lt;br /&gt;                 break;&lt;br /&gt;           }&lt;br /&gt;        }&lt;br /&gt;        ++fmt;&lt;br /&gt;     }&lt;br /&gt;     va_end( list );&lt;br /&gt;  }&lt;br /&gt;  fflush( stdout );&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-7501749357954447631?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Pawu7Y-PZT0:IYzQVWbVWQ4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Pawu7Y-PZT0:IYzQVWbVWQ4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/Pawu7Y-PZT0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2011-05-23T09:49:18.624+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>Implementation of Stack using Singly Linked Lists</title><link>http://codingfreak.blogspot.com/2010/07/implementation-of-stack-using-singly.html</link><category>C</category><category>DataStructures</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 07 Jul 2010 00:02:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1125104915856385785</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dfltLJBWT4zXt5ZeRLyKOFPFD4A/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dfltLJBWT4zXt5ZeRLyKOFPFD4A/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dfltLJBWT4zXt5ZeRLyKOFPFD4A/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dfltLJBWT4zXt5ZeRLyKOFPFD4A/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size:130%;"&gt;S&lt;/span&gt;tacks are linear data structures which means the data is stored in what looks like a line (although vertically). In simple words we can say&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-style: italic;"&gt;A stack is a last in, first out (LIFO) abstract data type and data structure.&lt;/span&gt;&lt;/blockquote&gt;Basic usage of stack at the Architecture level is as a means of allocating and accessing memory.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_MQa_rKnACRw/TDQnRhoO91I/AAAAAAAAB6A/n-X6lY_l8d8/s1600/Stack_4.jpg"&gt;&lt;img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 208px; height: 320px;" src="http://3.bp.blogspot.com/_MQa_rKnACRw/TDQnRhoO91I/AAAAAAAAB6A/n-X6lY_l8d8/s320/Stack_4.jpg" alt="Stack" id="BLOGGER_PHOTO_ID_5491057027709859666" border="0" /&gt;&lt;/a&gt;We can only perform two fundamental operations on a stack: &lt;span style="font-weight: bold;"&gt;push&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;pop&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.&lt;br /&gt;&lt;br /&gt;A stack is a &lt;span style="font-style: italic;"&gt;restricted data structure&lt;/span&gt;, because only a small number of operations are performed on it.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Let us 'C' code for implementing a stack using Singly Linked Lists.&lt;pre name="code" class="cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;//Structure containing a Data part &amp;amp; a&lt;br /&gt;//Link part to the next node in the List&lt;br /&gt;struct Node&lt;br /&gt;{&lt;br /&gt;int Data;&lt;br /&gt;struct Node *Next;&lt;br /&gt;}*Head;&lt;br /&gt;&lt;br /&gt;//Poping from a Stack&lt;br /&gt;void popStack()&lt;br /&gt;{&lt;br /&gt;struct Node *tmp_ptr=NULL, *cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;if(cur_ptr)&lt;br /&gt;{&lt;br /&gt;   Head = Head-&amp;gt;Next;&lt;br /&gt;   free(cur_ptr);&lt;br /&gt;}     &lt;br /&gt;else  &lt;br /&gt;   printf("\nStack is Empty");&lt;br /&gt;}   &lt;br /&gt;&lt;br /&gt;//Pushing into Stack&lt;br /&gt;void pushIntoStack(int num)&lt;br /&gt;{&lt;br /&gt;struct Node *tmp_ptr=NULL;&lt;br /&gt;&lt;br /&gt;if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)&lt;br /&gt;   tmp_ptr-&amp;gt;Data=num;&lt;br /&gt;else&lt;br /&gt;   printf("\nMemory Allocation Failed");&lt;br /&gt;&lt;br /&gt;if(Head)&lt;br /&gt;{&lt;br /&gt;   tmp_ptr-&amp;gt;Next=Head;&lt;br /&gt;   Head=tmp_ptr;&lt;br /&gt;}&lt;br /&gt;else { //List is Empty&lt;br /&gt;   Head=tmp_ptr;&lt;br /&gt;   Head-&amp;gt;Next=NULL;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Displaying Stack&lt;br /&gt;void displayStack()&lt;br /&gt;{&lt;br /&gt;struct Node *cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;if(cur_ptr)&lt;br /&gt;{&lt;br /&gt;    printf("\nElements in Stack:\n");&lt;br /&gt;    while(cur_ptr)&lt;br /&gt;    {&lt;br /&gt;        printf("\t%d\n",cur_ptr-&amp;gt;Data);&lt;br /&gt;        cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;    }&lt;br /&gt;    printf("\n");&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;   printf("\nStack is Empty");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt;int i=0;&lt;br /&gt;&lt;br /&gt;//Set HEAD as NULL&lt;br /&gt;Head=NULL;&lt;br /&gt;&lt;br /&gt;while(1)&lt;br /&gt;{&lt;br /&gt;  printf("\n####################################################\n");&lt;br /&gt;  printf("MAIN MENU\n");&lt;br /&gt;  printf("####################################################\n");&lt;br /&gt;  printf(" \n1. Push into stack");&lt;br /&gt;  printf(" \n2. Pop from Stack");&lt;br /&gt;  printf(" \n3. Display Stack");&lt;br /&gt;  printf(" \n4. Exit\n");&lt;br /&gt;  printf(" \nChoose Option: ");&lt;br /&gt;  scanf("%d",&amp;amp;i);&lt;br /&gt;&lt;br /&gt;  switch(i)&lt;br /&gt;  {&lt;br /&gt;    case 1:&lt;br /&gt;    {&lt;br /&gt;        int num;&lt;br /&gt;        printf("\nEnter a Number to push into Stack: ");&lt;br /&gt;        scanf("%d",&amp;amp;num);&lt;br /&gt;        pushIntoStack(num);&lt;br /&gt;        displayStack();&lt;br /&gt;        break;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    case 2:&lt;br /&gt;    {&lt;br /&gt;        popStack();&lt;br /&gt;        displayStack();&lt;br /&gt;        break;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    case 3:&lt;br /&gt;    {&lt;br /&gt;        displayStack();&lt;br /&gt;        break;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    case 4:&lt;br /&gt;    {&lt;br /&gt;        struct Node *temp;&lt;br /&gt;&lt;br /&gt;        while(Head!=NULL)&lt;br /&gt;        {&lt;br /&gt;            temp = Head-&amp;gt;Next;&lt;br /&gt;            free(Head);&lt;br /&gt;            Head=temp;&lt;br /&gt;        }&lt;br /&gt;        exit(0);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    default:&lt;br /&gt;    {&lt;br /&gt;        printf("\nWrong Option choosen");&lt;br /&gt;    }&lt;br /&gt;  }/* end if switch */&lt;br /&gt;}/* end of while */&lt;br /&gt;}/* end of main */&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;References:&lt;/span&gt;&lt;br /&gt;1. &lt;a href="http://en.wikipedia.org/wiki/Stack_%28data_structure%29"&gt;Wikipedia&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-1125104915856385785?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=SsLRfx9qm5s:wFMV06UCxaw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=SsLRfx9qm5s:wFMV06UCxaw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/SsLRfx9qm5s" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-07-07T13:37:53.915+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_MQa_rKnACRw/TDQnRhoO91I/AAAAAAAAB6A/n-X6lY_l8d8/s72-c/Stack_4.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>strace - diagnostic, debugging and reverse engineering tool</title><link>http://codingfreak.blogspot.com/2010/06/strace-useful-diagnostic-debugging-and.html</link><category>Linux</category><category>Debugging</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 30 Jun 2010 05:46:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-345754322455455334</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/tyvTSO6MmHYrDaYsXjqhCCRWBYw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tyvTSO6MmHYrDaYsXjqhCCRWBYw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/tyvTSO6MmHYrDaYsXjqhCCRWBYw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tyvTSO6MmHYrDaYsXjqhCCRWBYw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size:150%;"&gt;M&lt;/span&gt;any times we come across hopeless situations where a program when compiled and installed in GNU/Linux just fails to run. Then we have to trace the output of the misbehaving program. But tracing the output of a program throws up a lot of data and it is a daunting task to go through volumes of data. Still there are cases where we are not fruitful in pin pointing the cause of error.&lt;br /&gt;&lt;br /&gt;In this situation &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; also known as system-call tracer comes for rescue. It is a debugging tool that monitors the system calls used by a program and all the signals it receives. &lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-style:italic;"&gt;System administrators, diagnosticians and trouble-shooters will find it invaluable for solving problems with programs for which the source is not readily available since they do not need to be recompiled in order to trace them. Students, hackers and the overly-curious will find that a great deal can be learned about a system and its system calls by tracing even ordinary programs. And programmers will find that since system calls and signals are events that happen at the user/kernel interface,  a close examination of this boundary is very useful for bug isolation, sanity checking and attempting to capture race conditions.&lt;/span&gt;&lt;/blockquote&gt;A system call is the most common way programs communicate with the kernel. System calls include reading and writing data, opening and closing files and all kinds of network communication. Under Linux, a system call is done by calling a special interrupt with the number of the system call and its parameters stored in the CPU's registers.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Using &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; is quite simple. There are two ways to let &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; monitor a program.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;FIRST METHOD:&lt;/span&gt; &lt;br /&gt;Start a program using &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; command as shown below which prints the list of system calls made by the program.&lt;pre name="code" class="cpp"&gt;strace program-name&lt;/pre&gt;For example let us trace &lt;span style="font-weight:bold;"&gt;uname&lt;/span&gt; command.&lt;pre name="code" class="cpp"&gt;$strace uname&lt;br /&gt;execve(&amp;quot;/bin/uname&amp;quot;, [&amp;quot;uname&amp;quot;], [/* 23 vars */]) = 0&lt;br /&gt;brk(0)                                  = 0x871d000&lt;br /&gt;mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f30000&lt;br /&gt;access(&amp;quot;/etc/ld.so.preload&amp;quot;, R_OK)      = -1 ENOENT (No such file or directory)&lt;br /&gt;open(&amp;quot;/etc/ld.so.cache&amp;quot;, O_RDONLY)      = 3&lt;br /&gt;fstat64(3, {st_mode=S_IFREG|0644, st_size=59428, ...}) = 0&lt;br /&gt;mmap2(NULL, 59428, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f21000&lt;br /&gt;close(3)                                = 0&lt;br /&gt;open(&amp;quot;/lib/libc.so.6&amp;quot;, O_RDONLY)        = 3&lt;br /&gt;read(3, &amp;quot;\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0p\220\272&amp;quot;..., 512) = 512&lt;br /&gt;fstat64(3, {st_mode=S_IFREG|0755, st_size=1577052, ...}) = 0&lt;br /&gt;mmap2(0x45b93000, 1295780, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x45b93000&lt;br /&gt;mmap2(0x45cca000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x137) = 0x45cca000&lt;br /&gt;mmap2(0x45ccd000, 9636, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x45ccd000&lt;br /&gt;close(3)                                = 0&lt;br /&gt;mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f20000&lt;br /&gt;set_thread_area({entry_number:-1 -&amp;gt; 6, base_addr:0xb7f206c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0&lt;br /&gt;mprotect(0x45cca000, 8192, PROT_READ)   = 0&lt;br /&gt;mprotect(0x451dd000, 4096, PROT_READ)   = 0&lt;br /&gt;munmap(0xb7f21000, 59428)               = 0&lt;br /&gt;brk(0)                                  = 0x871d000&lt;br /&gt;brk(0x873e000)                          = 0x873e000&lt;br /&gt;open(&amp;quot;/usr/lib/locale/locale-archive&amp;quot;, O_RDONLY|O_LARGEFILE) = 3&lt;br /&gt;fstat64(3, {st_mode=S_IFREG|0644, st_size=55511552, ...}) = 0&lt;br /&gt;mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7d20000&lt;br /&gt;close(3)                                = 0&lt;br /&gt;uname({sys=&amp;quot;Linux&amp;quot;, node=&amp;quot;localhost.localdomain&amp;quot;, ...}) = 0&lt;br /&gt;fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0&lt;br /&gt;mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f2f000&lt;br /&gt;write(1, &amp;quot;Linux\n&amp;quot;, 6Linux&lt;br /&gt;)                  = 6&lt;br /&gt;close(1)                                = 0&lt;br /&gt;munmap(0xb7f2f000, 4096)                = 0&lt;br /&gt;exit_group(0)                           = ?&lt;br /&gt;Process 7477 detached&lt;/pre&gt;Even though output from &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; looks very complicated, but this is only due to many system calls made when loading shared libraries. However, once we have found which system calls are the important ones (mainly open, read, write and the like), the results will look fairly intuitive to us.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;SECOND METHOD:&lt;/span&gt;&lt;br /&gt;We can use the &lt;span style="font-weight:bold;"&gt;-p&lt;/span&gt; flag to attach to a running process and debug a running process. Let us try out on the sample code shown below.&lt;pre name="code" class="cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   sleep(20);&lt;br /&gt;   return 0;&lt;br /&gt;}&lt;/pre&gt;We will compile the above code and run it as a background process. Then using its process id and -p flag we will run &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; on it.&lt;pre name="code" class="cpp"&gt;$ gcc main.c&lt;br /&gt;&lt;br /&gt;$ ./a.out &amp;amp;&lt;br /&gt;[1] 7609&lt;br /&gt;&lt;br /&gt;$ strace -p 7609&lt;br /&gt;Process 7609 attached - interrupt to quit&lt;br /&gt;restart_syscall(&amp;lt;... resuming interrupted call ...&amp;gt;) = 0&lt;br /&gt;exit_group(0)                           = ?&lt;br /&gt;Process 7609 detached&lt;br /&gt;[1]+  Done                    ./a.out&lt;/pre&gt;Using &lt;span style="font-weight:bold;"&gt;-p&lt;/span&gt; flag we even debug daemon processes. In contrast to a debugger, &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; does not need a program's source code to produce human-readable output.&lt;br /&gt;&lt;br /&gt;But sometimes &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; produces great amount of data which creates problems in scrolling through huge output. In this case we can use &lt;span style="font-weight:bold;"&gt;-o&lt;/span&gt; option as shown below.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$ strace -o uname_output uname&lt;/pre&gt;If you use the &lt;span style="font-weight:bold;"&gt;-t&lt;/span&gt; option, then &lt;span style="font-weight:bold;"&gt;strace&lt;/span&gt; will prefix each line of the trace with the time of day. We can even specify the system call functions to trace using the &lt;span style="font-weight:bold;"&gt;-e&lt;/span&gt; option. For example, to trace only open() and close() function system calls use the following command:&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$ strace -o uname_output -e trace=open,close uname&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;References:&lt;/span&gt;&lt;br /&gt;1. &lt;a href="http://www.osreviews.net/reviews/admin/strace" target="_blank"&gt;OS Review&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-345754322455455334?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Dr2RXY1iIL8:-I2bXGBk-Z0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Dr2RXY1iIL8:-I2bXGBk-Z0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Dr2RXY1iIL8:-I2bXGBk-Z0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Dr2RXY1iIL8:-I2bXGBk-Z0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Dr2RXY1iIL8:-I2bXGBk-Z0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Dr2RXY1iIL8:-I2bXGBk-Z0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/Dr2RXY1iIL8" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-08-18T11:12:51.474+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Static Functions in C</title><link>http://codingfreak.blogspot.com/2010/06/static-functions-in-c.html</link><category>C</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 30 Jun 2010 03:53:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5647335868503007491</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/h4bk-5pLjmfkTh2zMb9uWxJfBt0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/h4bk-5pLjmfkTh2zMb9uWxJfBt0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/h4bk-5pLjmfkTh2zMb9uWxJfBt0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/h4bk-5pLjmfkTh2zMb9uWxJfBt0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size:150%;"&gt;B&lt;/span&gt;y default all functions are implicitly declared as &lt;span style="font-weight: bold;"&gt;extern&lt;/span&gt;, which means they're visible across translation units. But when we use &lt;span style="font-weight: bold;"&gt;static&lt;/span&gt; it restricts visibility of the function to the translation unit in which it's defined. So we can say&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-style: italic;"&gt;Functions that are visible only to other functions in the same file are known as static functions.&lt;/span&gt;&lt;/blockquote&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Let use try out some code about static functions.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;main.c&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;#include "header.h"&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;hello();&lt;br /&gt;return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;func.c&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;#include "header.h"&lt;br /&gt;&lt;br /&gt;void int hello()&lt;br /&gt;{&lt;br /&gt;printf("HELLO WORLD\n");&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;header.h&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;static int hello();&lt;/pre&gt;If we compile above code it fails as shown below&lt;pre name="code" class="cpp"&gt;$gcc main.c func.c&lt;br /&gt;header.h:4: warning: "hello" used but never defined&lt;br /&gt;/tmp/ccaHx5Ic.o: In function `main':&lt;br /&gt;main.c:(.text+0x12): undefined reference to `hello'&lt;br /&gt;collect2: ld returned 1 exit status&lt;/pre&gt;It fails in Linking since function hello() is declared as static and its definition is accessible only within func.c file but not for main.c file. All the functions within func.c can access hello() function but not by functions outside func.c file.&lt;br /&gt;&lt;br /&gt;Using this concept we can restrict others from accessing the internal functions which we want to hide from outside world. Now we don't need to create private header files for internal functions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note:&lt;/span&gt;&lt;br /&gt;For some reason, static has different meanings in in different contexts.&lt;br /&gt;&lt;br /&gt;1. When specified on a function declaration, it makes the function local to the file.&lt;br /&gt;2. When specified with a variable inside a function, it allows the variable to retain its value between calls to the function. See static variables.&lt;br /&gt;&lt;br /&gt;It seems a little strange that the same keyword has such different meanings...&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-5647335868503007491?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=So1snKloohg:pmU3ea894Sg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=So1snKloohg:pmU3ea894Sg:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=So1snKloohg:pmU3ea894Sg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=So1snKloohg:pmU3ea894Sg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=So1snKloohg:pmU3ea894Sg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=So1snKloohg:pmU3ea894Sg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/So1snKloohg" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-06-30T17:18:47.217+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Singly Linked List in C</title><link>http://codingfreak.blogspot.com/2010/04/singly-linked-list-in-c.html</link><category>C</category><category>DataStructures</category><author>noreply@blogger.com (Ajith)</author><pubDate>Sat, 24 Apr 2010 11:43:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5463986111631321538</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lg-CsQhPD2kR44AdrufgTf89IrQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lg-CsQhPD2kR44AdrufgTf89IrQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lg-CsQhPD2kR44AdrufgTf89IrQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lg-CsQhPD2kR44AdrufgTf89IrQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size:180%;"&gt;C&lt;/span&gt;heck &lt;a href="http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html" target="_blank"&gt;Implementation of Singly Linked List&lt;/a&gt; for theoretical explanation regarding implementation of singly linked lists.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;//Structure containing a Data part &amp;amp; a&lt;br /&gt;//Link part to the next node in the List&lt;br /&gt;&lt;br /&gt;struct Node&lt;br /&gt;{&lt;br /&gt; int Data;&lt;br /&gt; struct Node *Next;&lt;br /&gt;}*Head;&lt;br /&gt;&lt;br /&gt;// Counting number of elements in the List&lt;br /&gt;&lt;br /&gt;int length()&lt;br /&gt;{&lt;br /&gt;  struct Node *cur_ptr;&lt;br /&gt;  int count=0;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;  while(cur_ptr != NULL)&lt;br /&gt;  {&lt;br /&gt;     cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;     count++;&lt;br /&gt;  }&lt;br /&gt;  return(count);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Deleting a node from List depending upon the data in the node.&lt;br /&gt;&lt;br /&gt;int delNodeData(int num)&lt;br /&gt;{&lt;br /&gt;  struct Node *prev_ptr, *cur_ptr;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;  while(cur_ptr != NULL)&lt;br /&gt;  {&lt;br /&gt;     if(cur_ptr-&amp;gt;Data == num)&lt;br /&gt;     {&lt;br /&gt;        if(cur_ptr==Head)&lt;br /&gt;        {&lt;br /&gt;           Head=cur_ptr-&amp;gt;Next;&lt;br /&gt;           free(cur_ptr);&lt;br /&gt;           return 0;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;           prev_ptr-&amp;gt;Next=cur_ptr-&amp;gt;Next;&lt;br /&gt;           free(cur_ptr);&lt;br /&gt;           return 0;&lt;br /&gt;        }&lt;br /&gt;     }&lt;br /&gt;     else&lt;br /&gt;     {&lt;br /&gt;        prev_ptr=cur_ptr;&lt;br /&gt;        cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;     }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  printf("\nElement %d is not found in the List", num);&lt;br /&gt;  return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Deleting a node from List depending upon the location in the list.&lt;br /&gt;&lt;br /&gt;int delNodeLoc(int loc)&lt;br /&gt;{&lt;br /&gt;  struct Node *prev_ptr, *cur_ptr;&lt;br /&gt;  int i;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;  if(loc &amp;gt; (length()) || loc &amp;lt;= 0)&lt;br /&gt;  {&lt;br /&gt;      printf("\nDeletion of Node at given location is not possible\n ");&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;      // If the location is starting of the list&lt;br /&gt;      if (loc == 1)&lt;br /&gt;      {&lt;br /&gt;          Head=cur_ptr-&amp;gt;Next;&lt;br /&gt;          free(cur_ptr);&lt;br /&gt;          return 0;&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;          for(i=1;i&amp;lt;loc;i++)&lt;br /&gt;          {&lt;br /&gt;              prev_ptr=cur_ptr;&lt;br /&gt;              cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          prev_ptr-&amp;gt;Next=cur_ptr-&amp;gt;Next;&lt;br /&gt;          free(cur_ptr);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Adding a Node at the end of the list&lt;br /&gt;&lt;br /&gt;void addEnd(int num)&lt;br /&gt;{&lt;br /&gt;  struct Node *temp1, *temp2;&lt;br /&gt;&lt;br /&gt;  temp1=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;  temp1-&amp;gt;Data=num;&lt;br /&gt;&lt;br /&gt;  // Copying the Head location into another node.&lt;br /&gt;  temp2=Head;&lt;br /&gt;&lt;br /&gt;  if(Head == NULL)&lt;br /&gt;  {&lt;br /&gt;     // If List is empty we create First Node.&lt;br /&gt;     Head=temp1;&lt;br /&gt;     Head-&amp;gt;Next=NULL;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;     // Traverse down to end of the list.&lt;br /&gt;     while(temp2-&amp;gt;Next != NULL)&lt;br /&gt;     temp2=temp2-&amp;gt;Next;&lt;br /&gt;&lt;br /&gt;     // Append at the end of the list.&lt;br /&gt;     temp1-&amp;gt;Next=NULL;&lt;br /&gt;     temp2-&amp;gt;Next=temp1;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Adding a Node at the Beginning of the List&lt;br /&gt;&lt;br /&gt;void addBeg(int num)&lt;br /&gt;{&lt;br /&gt;  struct Node *temp;&lt;br /&gt;&lt;br /&gt;  temp=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;  temp-&amp;gt;Data = num;&lt;br /&gt;&lt;br /&gt;  if (Head == NULL)&lt;br /&gt;  {&lt;br /&gt;     //List is Empty&lt;br /&gt;     Head=temp;&lt;br /&gt;     Head-&amp;gt;Next=NULL;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;     temp-&amp;gt;Next=Head;&lt;br /&gt;     Head=temp;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Adding a new Node at specified position&lt;br /&gt;&lt;br /&gt;void addAt(int num, int loc)&lt;br /&gt;{&lt;br /&gt;  int i;&lt;br /&gt;  struct Node *temp, *prev_ptr, *cur_ptr;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;  if(loc &amp;gt; (length()+1) || loc &amp;lt;= 0)&lt;br /&gt;  {&lt;br /&gt;     printf("\nInsertion at given location is not possible\n ");&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;      // If the location is starting of the list&lt;br /&gt;      if (loc == 1)&lt;br /&gt;      {&lt;br /&gt;          addBeg(num);&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;          for(i=1;i&amp;lt;loc;i++)&lt;br /&gt;          {&lt;br /&gt;              prev_ptr=cur_ptr;&lt;br /&gt;              cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          temp=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;          temp-&amp;gt;Data=num;&lt;br /&gt;&lt;br /&gt;          prev_ptr-&amp;gt;Next=temp;&lt;br /&gt;          temp-&amp;gt;Next=cur_ptr;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Displaying list contents&lt;br /&gt;&lt;br /&gt;void display()&lt;br /&gt;{&lt;br /&gt;  struct Node *cur_ptr;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;  if(cur_ptr==NULL)&lt;br /&gt;  {&lt;br /&gt;     printf("\nList is Empty");&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;      printf("\nElements in the List: ");&lt;br /&gt;      //traverse the entire linked list&lt;br /&gt;      while(cur_ptr!=NULL)&lt;br /&gt;      {&lt;br /&gt;          printf(" -&amp;gt; %d ",cur_ptr-&amp;gt;Data);&lt;br /&gt;          cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;      }&lt;br /&gt;      printf("\n");&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Reversesing a Linked List&lt;br /&gt;&lt;br /&gt;void reverse()&lt;br /&gt;{&lt;br /&gt;  struct Node *prev_ptr, *cur_ptr, *temp;&lt;br /&gt;&lt;br /&gt;  cur_ptr=Head;&lt;br /&gt;  prev_ptr=NULL;&lt;br /&gt;&lt;br /&gt;  while(cur_ptr != NULL)&lt;br /&gt;  {&lt;br /&gt;     temp=prev_ptr;&lt;br /&gt;     prev_ptr=cur_ptr;&lt;br /&gt;&lt;br /&gt;     cur_ptr=cur_ptr-&amp;gt;Next;&lt;br /&gt;     prev_ptr-&amp;gt;Next=temp;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  Head=prev_ptr;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt; int i=0;&lt;br /&gt;&lt;br /&gt; //Set HEAD as NULL&lt;br /&gt; Head=NULL;&lt;br /&gt;&lt;br /&gt; while(1)&lt;br /&gt; {&lt;br /&gt;    printf("\n####################################################\n");&lt;br /&gt;    printf("MAIN MENU\n");&lt;br /&gt;    printf("####################################################\n");&lt;br /&gt;    printf(" \nInsert a number \n1. At the Beginning");&lt;br /&gt;    printf(" \n2. At the End");&lt;br /&gt;    printf(" \n3. At a Particular Location in the List");&lt;br /&gt;    printf(" \n\n4. Print the Elements in the List");&lt;br /&gt;    printf(" \n5. Print number of elements in the List");&lt;br /&gt;    printf(" \n6. Reverse the linked List");&lt;br /&gt;    printf(" \n\nDelete a Node in the List");&lt;br /&gt;    printf(" \n7. Delete a node based on Value");&lt;br /&gt;    printf(" \n8. Delete a node based on Location\n");&lt;br /&gt;    printf(" \n\n9. Exit\n");&lt;br /&gt;    printf(" \nChoose Option: ");&lt;br /&gt;    scanf("%d",&amp;amp;i);&lt;br /&gt;&lt;br /&gt;    switch(i)&lt;br /&gt;    {&lt;br /&gt;      case 1:&lt;br /&gt;      {&lt;br /&gt;          int num;&lt;br /&gt;          printf(" \nEnter a Number to insert in the List: ");&lt;br /&gt;          scanf("%d",&amp;amp;num);&lt;br /&gt;          addBeg(num);&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 2:&lt;br /&gt;      {&lt;br /&gt;          int num;&lt;br /&gt;          printf(" \nEnter the Number to insert: ");&lt;br /&gt;          scanf("%d",&amp;amp;num);&lt;br /&gt;          addEnd(num);&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 3:&lt;br /&gt;      {&lt;br /&gt;          int num, loc;&lt;br /&gt;          printf("\nEnter the Number to insert: ");&lt;br /&gt;          scanf("%d",&amp;amp;num);&lt;br /&gt;          printf("\nEnter the location Number in List at which \&lt;br /&gt;          the Number is inserted: ");&lt;br /&gt;          scanf("%d",&amp;amp;loc);&lt;br /&gt;          addAt(num,loc);&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 4:&lt;br /&gt;      {&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 5:&lt;br /&gt;      {&lt;br /&gt;          display();&lt;br /&gt;          printf(" \nTotal number of nodes in the List: %d",length());&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 6:&lt;br /&gt;      {&lt;br /&gt;          reverse();&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 7:&lt;br /&gt;      {&lt;br /&gt;          int num;&lt;br /&gt;          printf(" \nEnter the number to be deleted from List: ");&lt;br /&gt;          scanf("%d",&amp;amp;num);&lt;br /&gt;          delNodeData(num);&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 8:&lt;br /&gt;      {&lt;br /&gt;          int num;&lt;br /&gt;          printf(" \nEnter the location of the node to \&lt;br /&gt;          be deleted from List: ");&lt;br /&gt;          scanf("%d",&amp;amp;num);&lt;br /&gt;          delNodeLoc(num);&lt;br /&gt;          display();&lt;br /&gt;          break;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      case 9:&lt;br /&gt;      {&lt;br /&gt;          struct Node *temp;&lt;br /&gt;&lt;br /&gt;          while(Head!=NULL)&lt;br /&gt;          {&lt;br /&gt;              temp = Head-&amp;gt;Next;&lt;br /&gt;              free(Head);&lt;br /&gt;              Head=temp;&lt;br /&gt;          }&lt;br /&gt;          exit(0);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      default:&lt;br /&gt;      {&lt;br /&gt;          printf("\nWrong Option choosen");&lt;br /&gt;      }&lt;br /&gt;    }/* end if switch */&lt;br /&gt; }/* end of while */&lt;br /&gt;}/* end of main */&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-5463986111631321538?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=oiW1gWi-IB0:Nf3Z8x9ZBw8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=oiW1gWi-IB0:Nf3Z8x9ZBw8:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=oiW1gWi-IB0:Nf3Z8x9ZBw8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=oiW1gWi-IB0:Nf3Z8x9ZBw8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=oiW1gWi-IB0:Nf3Z8x9ZBw8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=oiW1gWi-IB0:Nf3Z8x9ZBw8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/oiW1gWi-IB0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-06-30T17:16:14.121+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total></item><item><title>Fedora 12 on HP Pavilion dv6340eu</title><link>http://codingfreak.blogspot.com/2010/03/fedora-12-on-hp-pavilion-dv6340eu.html</link><category>Open Source</category><category>Linux</category><category>Laptop</category><author>noreply@blogger.com (Ajith)</author><pubDate>Sat, 27 Mar 2010 12:51:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7405815151189610032</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/oLUBqppzAG3wYvdB-uej-PWoTbI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oLUBqppzAG3wYvdB-uej-PWoTbI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/oLUBqppzAG3wYvdB-uej-PWoTbI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oLUBqppzAG3wYvdB-uej-PWoTbI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Atlast the day has come, LINUX running on my laptop(pre-loaded with M$ VISTA). Time for celebrations.&lt;br /&gt;&lt;br /&gt;So I decided to drop a post on how the journey took place towards installing and using LINUX in my laptop. Initially I faced a serious problem, which LINUX flavour to use ... UBUNTU or FEDORA (sorry these are only my preferred flavours for long time). I got UBUNTU successfully running on my old desktop, on my sister's latest desktop and some more desktops of friends. I got FEDORA running successfully on my SERVER and other desktops. I never tried or used LINUX flavours on a LAPTOP (as I see communities filled with posts saying problems with LINUX on their laptops) so I have to choose one ...&lt;br /&gt;&lt;br /&gt;Atlast I decided to go with FEDORA 12 .. not sure why I dumped UBUNTU over FEDORA ... but since I am comfortable with FEDORA as I work more with FEDORA .. and FEDORA 12 got many new open-source technologies and pre-packed with latest kernel in the market (Hmm I agree that a UBUNTU release is 6 months older to a FEDORA release). But still I love UBUNTU :)&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Laptop Specs &lt;/span&gt;&lt;br /&gt;HP Pavilion dv6340eu (2007 European model)&lt;br /&gt;AMD Turion 64 X2 processor, &lt;br /&gt;1GB RAM (really insane), &lt;br /&gt;NVIDIA GEOFORCE 7200M graphics card, &lt;br /&gt;BROADCOM wireless chipset and other HP stuff. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Off the Road ... M$ VISTA&lt;/span&gt;&lt;br /&gt;Boot up and complete M$ VISTA loading takes around 10 to 15 minutes (low RAM might be the cause) until then don't even think of opening any other application it will mess up. Sometimes I use a NOKIA mobile to connect to WIRELESS GPRS INTERNET of AIRTEL and it requires PC-SUITE software from NOKIA. Apart from this inorder to protect myself from viruses and online threats I need a third party security application(s) like ANTIVIRUS, FIREWALL (resource hogs on 1GB RAM machine running M$ VISTA). Even in idle state more than 60% of the RAM is always busy and processor(s) utilization is always more than 50%. I faced strange problems on M$ VISTA with External harddisks .. when I use "safely remove hardware" option and then disconnect the hardware it always pop up message saying some information might be lost ... eventhough I wait for sometime to disconnect them?? and it really messed up my external storage devices many times.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Back to the Road ...&lt;/span&gt;&lt;br /&gt;I decided to go with a 32 bit FEDORA OS installation ... after seeing many problems reported with 64 bit OS when compared to 32 bit OS. Downloaded a LIVECD and installed FEDORA 12 in dual boot without any hiccups ... that's a plain install no magic spells or hacking cheats used :). Voilla great its really simple on my laptop.&lt;br /&gt;&lt;br /&gt;Automatic reboot of laptop once the installation is complete and booted in FEDORA12 ... Wow it just took less than 2 minutes to boot up completely ... RAM and CPU usage has been drastically reduced.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_MQa_rKnACRw/S65ptSWB-2I/AAAAAAAAB4U/BabLq-zqyQE/s1600/01.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 246px; height: 120px;" src="http://3.bp.blogspot.com/_MQa_rKnACRw/S65ptSWB-2I/AAAAAAAAB4U/BabLq-zqyQE/s320/01.png" border="0" alt="system monitor preview codingfreak"id="BLOGGER_PHOTO_ID_5453412425531521890" /&gt;&lt;/a&gt;&lt;br /&gt;I connected my NOKIA mobile to laptop and it is detected automatically. I created a new wireless broadband connection under my subscriber AIRTEL and it simply connected like a charm. Damn who needs NOKIA PC-SUITE crap for connecting to internet. I am not using any of the fancy COMPIZ stuff and simply using the NOUVEAU drivers for my graphic cards. My wirelan connection is also working like a charm no drivers from HP or any third party stuff.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Problems in Journey ..&lt;/span&gt;&lt;br /&gt;Small usage problems with openoffice ... as my brain is corrupted with M$ OFFICE. Eventhough LIVE CD is downloaded recently it has got many security updates and patches (around 412 I guess .. of 300MB) to be downloaded from internet ... I wonder why don't they update the FEDORA build atleast once in a month with latest patches :(. Livecd doesn't have any compilers for C, C++, Java and so on and they have to be updated from internet :(. Fedora by default has music/movie players but none of them have any codecs to play a normal MP3 file (hmm licensing problems)...&lt;br /&gt;&lt;br /&gt;Seems there are some problems with power management .. as my laptop makes me feel as if it is burning ... My harddisk temperature is at 60'c and processor cores are above 65'c eventhough internal fans are running at full speed.&lt;br /&gt;&lt;br /&gt;&lt;strike&gt;After all the problems and happiness I decided to drop this post in BLOGGER from a LINUX machine ... my FIRST one. Now my laptop proudly carry the logo of FEDORA on its chest.&lt;/strike&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/S65tsUUGFcI/AAAAAAAAB4c/Mm3js_PsTEE/s1600/fedora_logo.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 140px; height: 240px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/S65tsUUGFcI/AAAAAAAAB4c/Mm3js_PsTEE/s320/fedora_logo.png" border="0" alt="codingfreak fedora"id="BLOGGER_PHOTO_ID_5453416806926915010" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Update 1:&lt;/span&gt;&lt;/span&gt; Do check Fedora 12 common bugs before you install Fedora 12 on your machine - &lt;a href="https://fedoraproject.org/wiki/Common_F12_bugs" target="_blank"&gt;https://fedoraproject.org/wiki/Common_F12_bugs&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Update 2:&lt;/span&gt;&lt;/span&gt; Integrated Webcam is still not working .. looking for workarounds.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Update 3:&lt;/span&gt;&lt;/span&gt; Do not use Nvidia Proprietary drivers replacing &lt;span style="font-weight:bold;"&gt;NOUVEAU&lt;/span&gt; drivers. Atleast I am seeing a system crash after update. Raised a bug at &lt;a href="https://bugzilla.redhat.com/show_bug.cgi?id=594439" target="_blank"&gt;https://bugzilla.redhat.com/show_bug.cgi?id=594439&lt;/a&gt; and waiting for the real reason behind the crash and any workarounds.&lt;br /&gt;&lt;br /&gt;Hmm ... haven't found any solution until now ... Only solution available is REINSTALL OS again ... stupid workaround. Why should I stuck with FEDORA 12 which seems to be crappy .. UBUNTU 10.04 with 5 years of support here I come.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Update 4:&lt;/span&gt;&lt;/span&gt;BYE BYE FEDORA 12 .... I am really disappointed by the support and help provided by Fedora Community.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-7405815151189610032?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=5UKjTL5Jfd8:Cm3u2WEtIKA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=5UKjTL5Jfd8:Cm3u2WEtIKA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=5UKjTL5Jfd8:Cm3u2WEtIKA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=5UKjTL5Jfd8:Cm3u2WEtIKA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=5UKjTL5Jfd8:Cm3u2WEtIKA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=5UKjTL5Jfd8:Cm3u2WEtIKA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/5UKjTL5Jfd8" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-06-30T16:54:45.112+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_MQa_rKnACRw/S65ptSWB-2I/AAAAAAAAB4U/BabLq-zqyQE/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>iptables - Rate-limit incoming connections</title><link>http://codingfreak.blogspot.com/2010/01/iptables-rate-limit-incoming.html</link><category>Linux</category><category>iptables</category><author>noreply@blogger.com (Ajith)</author><pubDate>Mon, 04 Jan 2010 04:57:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2721042669156653879</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IM7JW5xYZQaKGeIF8GRl6M-srf0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IM7JW5xYZQaKGeIF8GRl6M-srf0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IM7JW5xYZQaKGeIF8GRl6M-srf0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IM7JW5xYZQaKGeIF8GRl6M-srf0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/S0HmTshMyvI/AAAAAAAAB3o/SXjzOpIapig/s1600-h/Firewall.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 256px; height: 256px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/S0HmTshMyvI/AAAAAAAAB3o/SXjzOpIapig/s320/Firewall.png" alt="codingfreak linux" id="BLOGGER_PHOTO_ID_5422868652373101298" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Using "&lt;span style="font-weight: bold;"&gt;recent&lt;/span&gt;" match option in iptables we can match recent connections, and perform simple throttling operation on incoming connections i.e. we can create simple firewall rules which will deny access from remote clients who attempt to connect "too many" times.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;"recent" dynamically creates a list of IP addresses and then match against that list.&lt;/span&gt; The functionality of "&lt;span style="font-weight: bold;"&gt;recent&lt;/span&gt;" match option is simple, a list of IP addresses are created dynamically, which can be used in the future to test connection attempts against. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;rate limiting is conceptually different from bandwidth throttling/limiting; a bandwidth-throttled connection will queue packets and limit the rate at which they are transmitted/received. Rate limiting will not do this; when you use rate limiting on, for example, incoming TCP connection attempts to your identd, and connections exceeding the specified limit will be denied; there is no queueing of packets.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;NOTE:&lt;/span&gt; &lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;CONFIG_IP_NF_MATCH_RECENT&lt;/span&gt; flag should be enabled in Linux Kernel to use "&lt;span style="font-weight: bold;"&gt;recent&lt;/span&gt;" option.&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;br /&gt;Following 2 rules will limit incoming connections to destination port 22 not more than 3 in a minute and more than that will be dropped:&lt;pre name="code" class="cpp"&gt;iptables -I INPUT -p tcp --dport 22 -m state --state NEW \&lt;br /&gt;-m recent --set&lt;br /&gt;&lt;br /&gt;iptables -I INPUT -p tcp --dport 22 -m state --state NEW \&lt;br /&gt;-m recent --update --seconds 60 --hitcount 3 -j DROP&lt;/pre&gt;"&lt;span style="font-weight: bold;"&gt;--state NEW&lt;/span&gt;" – To make sure that only new connections are managed.&lt;br /&gt;&lt;br /&gt;"&lt;span style="font-weight: bold;"&gt;--set&lt;/span&gt;" flag will make sure that the IP address of the host which initiated the connection will be added to the "recent list", where it can be tested and used again in the future i.e. in our second rule.&lt;br /&gt;&lt;br /&gt;The second rule is where the actual magic happens.&lt;br /&gt;&lt;br /&gt;"&lt;span style="font-weight: bold;"&gt;—update&lt;/span&gt;" flag tests whether the source IP address is in the list of recent connections, in our case each new tcp connection to destination port 22 will be in the list because we used the "&lt;span style="font-weight: bold;"&gt;--set&lt;/span&gt;" flag to add it in the preceeding rule.&lt;br /&gt;&lt;br /&gt;"&lt;span style="font-weight: bold;"&gt;--seconds&lt;/span&gt;" flag is used to make sure that the source IP address is only going to match if the last connection was within the timeframe given.&lt;br /&gt;&lt;br /&gt;"&lt;span style="font-weight: bold;"&gt;--hitcount&lt;/span&gt;" flag matches only if the given count of connection attempts is greater than or equal to the number given.&lt;br /&gt;&lt;br /&gt;The second rule will DROP an incoming connection if:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The IP address which initiated the connection has previously been added to the list and&lt;/li&gt;&lt;li&gt;The IP address has sent a packet in the past 60 seconds and&lt;/li&gt;&lt;li&gt;The IP address has sent more than 3 packets in total.&lt;/li&gt;&lt;/ul&gt;Let use see another example where we try to limit ICMP echo requests to not more than 30 in a minute:&lt;pre name="code" class="cpp"&gt;iptables -I INPUT -p icmp --icmp-type echo-request \&lt;br /&gt;-m recent --set&lt;br /&gt;&lt;br /&gt;iptables -I INPUT -p icmp --icmp-type echo-request \&lt;br /&gt;-m recent --update --seconds 60 --hitcount 30 -j DROP&lt;/pre&gt;In above rules we dont use "&lt;span style="font-weight: bold;"&gt;--state NEW&lt;/span&gt;" since it is not needed in rate limiting number of ICMP echo requests.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;NOTE:&lt;/span&gt; In the above rules we are trying to automatically limit number of connections from each user. So it is something like 3 attempts from each user in 1 minute. &lt;br /&gt;&lt;br /&gt;What if we want to limit number of certain packets from all users ? Then "&lt;span style="font-weight:bold;"&gt;limit&lt;/span&gt;" match option comes to rescue.&lt;br /&gt;&lt;br /&gt;"&lt;span style="font-weight:bold;"&gt;limit&lt;/span&gt;" option specifies the maximum average number of matches to allow per second. We can specify time intervals in the format /second, /minute, /hour, or /day, or you can use abbreviations so that 3/second is the same as 3/s.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;NOTE:&lt;/span&gt; &lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;CONFIG_IP_NF_MATCH_LIMIT&lt;/span&gt; flag should be enabled in Linux Kernel to use "&lt;span style="font-weight: bold;"&gt;limit&lt;/span&gt;" option.&lt;br /&gt;&lt;br /&gt;Let us see how to limit number of ICMP echo requests not more than 3 per second and drop rest of them.&lt;pre name="code" class="cpp"&gt;iptables -I INPUT -p icmp --icmp-type echo-request \&lt;br /&gt;-m limit !--limit 3/s -j ACCEPT&lt;/pre&gt;When tuned correctly, this feature allows us to filter unusually high volumes of traffic that characterize denial of service (DOS) attacks and Internet worms.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;But take care: When tuned incorrectly, this feature does the opposite: Helping any attacker in denial of service attacks. Instead of having to initiate enough connections to bring the whole server down, it then may be sufficient to just start enough connections to activate the firewall rules.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;References:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;1. &lt;/span&gt;&lt;a href="http://www.debian-administration.org/articles/187" target="_blank"&gt;&lt;span style="font-weight: bold;"&gt;Debian Administration&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2. &lt;a href="http://linux.die.net/man/8/iptables" target="_blank"&gt;man iptables&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-2721042669156653879?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=TUZWaTg1CWM:Sb1cB2qspsY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=TUZWaTg1CWM:Sb1cB2qspsY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=TUZWaTg1CWM:Sb1cB2qspsY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=TUZWaTg1CWM:Sb1cB2qspsY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=TUZWaTg1CWM:Sb1cB2qspsY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=TUZWaTg1CWM:Sb1cB2qspsY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/TUZWaTg1CWM" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-03-30T09:07:34.125+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_MQa_rKnACRw/S0HmTshMyvI/AAAAAAAAB3o/SXjzOpIapig/s72-c/Firewall.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Creating and using static libraries in Linux</title><link>http://codingfreak.blogspot.com/2010/01/creating-and-using-static-libraries-in.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Fri, 01 Jan 2010 00:53:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-371159421297363693</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/sVJjhj9n4meN-sJVVtXTHn77W58/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sVJjhj9n4meN-sJVVtXTHn77W58/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/sVJjhj9n4meN-sJVVtXTHn77W58/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sVJjhj9n4meN-sJVVtXTHn77W58/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Static libraries &lt;span style="font-style:italic;"&gt;are simply a collection of ordinary object files&lt;/span&gt;. &lt;br /&gt;&lt;br /&gt;For more information on shared libraries checkout - &lt;a href="http://codingfreak.blogspot.com/2009/12/creating-and-using-shared-libraries-in.html" target="_blank"&gt;Creating and using shared libraries in Linux&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Static libraries conventionally end with the ".a" suffix. Static libraries aren't used as often as they once were, because of the advantages of shared libraries. Still, they're sometimes created, they existed first historically, and they're simpler to explain.&lt;br /&gt;&lt;br /&gt;Static libraries are often useful for developers if they wish to permit programmers to link to their library, but don't want to give the library source code (which is an advantage to the library vendor, but obviously not an advantage to the programmer trying to use the library). In theory, code in static ELF libraries that is linked into an executable should run slightly faster (by 1-5%) than a shared library or a dynamically loaded library, but in practice this rarely seems to be the case due to other confounding factors.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s1600-h/Obey_Linux_by_simpletoker.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 246px; height: 320px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s320/Obey_Linux_by_simpletoker.jpg" alt="codingfreak linux" id="BLOGGER_PHOTO_ID_5413196535000306226" border="0" /&gt;&lt;/a&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;We use following source code files for this post.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;calc_mean.c&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;&lt;br /&gt;double mean(double a, double b)&lt;br /&gt;{&lt;br /&gt;return (a+b) / 2;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;calc_mean.h&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;double mean(double, double);&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;main.c&lt;/span&gt; - We are including our shared library in this application.&lt;pre name="code" class="cpp"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include "calc_mean.h"&lt;br /&gt;&lt;br /&gt;int main(int argc, char* argv[]) {&lt;br /&gt;&lt;br /&gt;double v1, v2, m;&lt;br /&gt;v1 = 5.2;&lt;br /&gt;v2 = 7.9;&lt;br /&gt;&lt;br /&gt;m  = mean(v1, v2);&lt;br /&gt;&lt;br /&gt;printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);&lt;br /&gt;&lt;br /&gt;return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);font-size:100%;" &gt;Creating the static library&lt;/span&gt;&lt;br /&gt;First we have to create object file for calc_mean.c&lt;pre name="code" class="cpp"&gt;gcc -c calc_mean.c -o calc_mean.o&lt;/pre&gt;Then, using &lt;span style="font-weight:bold;"&gt;archiver (ar)&lt;/span&gt; we produce a static library (named libmean.a) out of the object file calc_mean.o.&lt;pre name="code" class="cpp"&gt;ar rcs libmean.a calc_mean.o&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;NOTE:&lt;/span&gt; A static library must start with the three letters '&lt;span style="font-weight:bold;"&gt;lib&lt;/span&gt;' and have the suffix '&lt;span style="font-weight:bold;"&gt;.a&lt;/span&gt;'.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);font-size:100%;"&gt;Compiling main program and linking with static library&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;gcc -static main.c -L. -lmean -o main&lt;/pre&gt;Now run the executable program 'main'&lt;pre name="code" class="cpp"&gt;$./main&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-371159421297363693?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Xx0Zl3zfpCU:C-2OXeahkXw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Xx0Zl3zfpCU:C-2OXeahkXw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Xx0Zl3zfpCU:C-2OXeahkXw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Xx0Zl3zfpCU:C-2OXeahkXw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Xx0Zl3zfpCU:C-2OXeahkXw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Xx0Zl3zfpCU:C-2OXeahkXw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/Xx0Zl3zfpCU" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-03-30T09:07:51.457+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s72-c/Obey_Linux_by_simpletoker.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>ctags - vim with steroids</title><link>http://codingfreak.blogspot.com/2009/12/ctags-vim-with-steroids.html</link><category>C</category><category>ctags</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 16 Dec 2009 21:50:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5389521138639751502</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0jA1BcilEEOohADy2JGUkZ5hTao/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0jA1BcilEEOohADy2JGUkZ5hTao/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0jA1BcilEEOohADy2JGUkZ5hTao/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0jA1BcilEEOohADy2JGUkZ5hTao/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Basically C/C++ projects have thousands of lines of code divided into hundreds in some cases thousands of files. Inorder to access various function definitions within the sourcecode repository effectively using a VIM editor there is a great need for using addons like &lt;span style="font-weight: bold;"&gt;ctags&lt;/span&gt; which provides easy code go through.&lt;br /&gt;&lt;br /&gt;Eventhough there are many effective GUI based code editors like eclipse e.t.c I prefer to use VIM editor as my primary code editor. I am not much into GUI funda so still prefer basic editor like VIM.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;font-size:120%;" &gt;1. Installing ctags package&lt;/span&gt;&lt;br /&gt;Almost all the linux flavours with 2.6.X kernel might have ctags installed by default. If not download the appropriate .deb or rpm file. Sorry I am not dealing with installing of ctags as I havent came across this stage as I am using FEDORA 9.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;font-size:120%;" &gt;2. Generating ctags on your source code.&lt;/span&gt;&lt;br /&gt;We have to generate a file named 'tags' for all the source code files, use the following command:&lt;br /&gt;&lt;pre class="cpp" name="code"&gt;ctags *.c *.h&lt;/pre&gt;When we have many files in many directories then we have to create a tags file in each of the directories. But VIM will only be able to jump to tags within the same directory. To find more tags files, we have to set the 'tags' option in VIM to include all the relevant tags files. Just set the following command in ~/.vimrc file. &lt;pre class="cpp" name="code"&gt;:set tags=./tags,./../tags,./*/tags&lt;/pre&gt;This finds a tags file in the same directory as the current file, one directory level higher and in all subdirectories.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;But VIM searching many places for tags files is not really robust enough as it may get a bit slow. It's better to generate one big tags file offcourse it takes more time to create a single tags files for whole project. Just give the following command to recursively add all files in your project. &lt;pre class="cpp" name="code"&gt;$ cd /proj &lt;br /&gt;$ ctags -R *&lt;/pre&gt;-R is to recursively go across all the directories, and a ‘tags’ file will be created including all the files in the sub-directories also.&lt;br /&gt;&lt;br /&gt;Now we can simply include this single tags files as shown below &lt;pre class="cpp" name="code"&gt;:set tags=~/proj/tags&lt;/pre&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;font-size:120%;" &gt;3. Start using ctags with VIM editor&lt;/span&gt;&lt;br /&gt;We have 3 different ways to use ctags in VIM editor.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;From Shell:&lt;/span&gt;&lt;br /&gt;We can invoke directly the file or the files containing the definition for a particular function from shell.&lt;pre class="cpp" name="code"&gt;vi -t function_name&lt;/pre&gt;This will find the correct file or list of files having function_name definition.&lt;br /&gt;&lt;br /&gt;OR&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;VIM command line:&lt;/span&gt;&lt;br /&gt;We can invoke from VIM commandline (in command mode) the definition of a function.&lt;pre class="cpp" name="code"&gt;:tag function_name&lt;br /&gt;or&lt;br /&gt;:ta function_name&lt;/pre&gt;This will jump automatically from the current file to the file containing the function_name definition.&lt;br /&gt;&lt;br /&gt;OR&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;By cursor position:&lt;/span&gt;&lt;br /&gt;This option is more user-friendly as we use a key combination instead of giving commands.&lt;pre class="cpp" name="code"&gt;ctrl + ]&lt;/pre&gt;Place the cursor on the first character of the function name and press ctrl-]. This will jump to the file containing the definition of function_name.&lt;pre class="cpp" name="code"&gt;ctrl + t&lt;/pre&gt;This command will jump back to previous location.&lt;pre class="cpp" name="code"&gt;ctrl + i&lt;/pre&gt;To travel forward through the tag history.&lt;pre class="cpp" name="code"&gt;ctrl + o&lt;/pre&gt;To travel backward through the tag history.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;History&lt;/span&gt;&lt;br /&gt;To display the list of tags that we have traversed in past give the following command.&lt;pre class="cpp" name="code"&gt;:tags&lt;/pre&gt;Shows tag stack (history). &lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;Divide and Conquer&lt;/span&gt;&lt;br /&gt;As we saw already '&lt;span style="font-weight: bold;"&gt;ctrl + ]&lt;/span&gt;' replaces the file in the current window with the one containing the new function. But suppose if we want to see not only the old function but also the new one?&lt;br /&gt;&lt;br /&gt;We can split the window using the "&lt;span style="font-weight: bold;"&gt;:split&lt;/span&gt;" command followed by the "&lt;span style="font-weight: bold;"&gt;:tag&lt;/span&gt;" command.&lt;pre class="cpp" name="code"&gt;:stag function_name&lt;/pre&gt;Cursor command for above feature is&lt;pre class="cpp" name="code"&gt;ctrl + w + ]&lt;/pre&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;Auto Complete&lt;/span&gt;&lt;br /&gt;VIM supports tag name completion. Start typing the tag name (i.e. function name) and then hit TAB key and name completion will be done automatically if there is a tag name.&lt;pre class="cpp" name="code"&gt;:tag start-of-tag-name_TAB&lt;/pre&gt;Jump to a tag name found by a search. &lt;pre class="cpp" name="code"&gt;:tag /search-string&lt;/pre&gt;When multiple entries exist in the tags file, such as a function declaration in a header file and a function definition (the function itself), the operator can choose by issuing this command. The user will be presented with all the references to the function and the user will be prompted to enter the number associated with the appropriate one.&lt;pre class="cpp" name="code"&gt;:tselect function-name&lt;/pre&gt;Jumps to next matching tag.&lt;pre class="cpp" name="code"&gt;:tnext&lt;br /&gt;or&lt;br /&gt;:tn&lt;/pre&gt;Jump to previous matching tag.&lt;pre class="cpp" name="code"&gt;:tprevious&lt;br /&gt;or&lt;br /&gt;:tp&lt;/pre&gt;Jump to first matching tag.&lt;pre class="cpp" name="code"&gt;:tfirst&lt;br /&gt;or&lt;br /&gt;:tf&lt;br /&gt;or&lt;br /&gt;:trewindor:tr&lt;/pre&gt;Jump to last matching tag.&lt;pre class="cpp" name="code"&gt;:tlast&lt;br /&gt;or&lt;br /&gt;:tl&lt;/pre&gt;&lt;span style="color: rgb(204, 0, 0); font-weight: bold;"&gt;Finding global identifiers&lt;/span&gt;&lt;br /&gt;You are editing a C program and wonder if a variable is declared as "int" or "unsigned". A quick way to find this is with the "[I" command. Suppose the cursor is on the word "column" then type &lt;pre class="cpp" name="code"&gt;[ + shiftkey + i&lt;/pre&gt;Vim will list the matching lines it can find.  Not only in the current file,&lt;br /&gt;but also in all included files (and files included in them, etc.).  The result&lt;br /&gt;looks like this:&lt;br /&gt;&lt;pre class="cpp" name="code"&gt;&lt;br /&gt; structs.h &lt;br /&gt;  1:   29     unsigned     column;    /* column number */ &lt;/pre&gt;The advantage over using tags or the preview window is that included files are&lt;br /&gt;searched.  In most cases this results in the right declaration to be found.&lt;br /&gt;Also when the tags file is out of date.  Also when you don't have tags for the&lt;br /&gt;included files.&lt;br /&gt;&lt;br /&gt;However, a few things must be right for "[I" to do its work.  First of all,&lt;br /&gt;the 'include' option must specify how a file is included.  The default value&lt;br /&gt;works for C and C++.  For other languages you will have to change it.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Reference:&lt;/span&gt;&lt;br /&gt;&lt;a href="http://vimdoc.sourceforge.net/htmldoc/usr_29.html" target="_blank"&gt;ctags documentation&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-5389521138639751502?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=0aXIlypMVdQ:Xxqu1teT92Q:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=0aXIlypMVdQ:Xxqu1teT92Q:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=0aXIlypMVdQ:Xxqu1teT92Q:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=0aXIlypMVdQ:Xxqu1teT92Q:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=0aXIlypMVdQ:Xxqu1teT92Q:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=0aXIlypMVdQ:Xxqu1teT92Q:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/0aXIlypMVdQ" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-03-29T09:41:02.367+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Creating Shared Libraries in Linux - Part 2</title><link>http://codingfreak.blogspot.com/2010/11/creating-shared-libraries-in-linux-part.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 16 Dec 2009 20:51:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6586624017391420226</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TGS_SSFH7Wg4DTvfwGt0ZhRWWF8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TGS_SSFH7Wg4DTvfwGt0ZhRWWF8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TGS_SSFH7Wg4DTvfwGt0ZhRWWF8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TGS_SSFH7Wg4DTvfwGt0ZhRWWF8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;4. Making the library available at runtime&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: small;"&gt;&lt;b&gt;Using LD_LIBRARY_PATH&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
We have to create or set the environment variable "&lt;span style="font-weight: bold;"&gt;LD_LIBRARY_PATH&lt;/span&gt;" to the directory containing the shared libraries. &lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;export LD_LIBRARY_PATH=/home/cf/lib&lt;/pre&gt;
If in current directory you can give the following command&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;export LD_LIBRARY_PATH=.&lt;/pre&gt;
If we have to append a new directory to the existing paths then add the directories separated by colons to environment variable "&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt;".&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH&lt;/pre&gt;
Now recompile the main program&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;gcc -o test main.c -lcalc_mean -L/home/cf/slib&lt;/pre&gt;
Now check the ldd command output&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;$ ldd test
 linux-gate.so.1 =&amp;gt;  (0x007ad000)
 libcalc_mean.so =&amp;gt; ./libcalc_mean.so (0x0081e000)
 libc.so.6 =&amp;gt; /lib/tls/i686/cmov/libc.so.6 (0x005ff000)
 /lib/ld-linux.so.2 (0x00e19000)
&lt;/pre&gt;
It seems now linker is able to locate our shared library as we can see in above output. Now run the program&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;$./test&lt;/pre&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt; is good for quick tests and for systems on which you don’t have admin privileges. As a downside, however, exporting the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt; environment variable&amp;nbsp; might screw up with other programs you run that also rely on &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt; if you don’t reset it to its previous state when you’re done.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Using rpath&lt;/b&gt;&lt;br /&gt;
Rpath, or the run path or -R, is a way of embedding the location of shared libraries in the executable itself, instead of relying on default locations or environment variables. We do this during the linking stage. Before we compile the main.c with &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;rpath&lt;/span&gt; option we will unset the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt;.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;gcc -g -o test main.c -lcalc_mean -L/home/cf/slib -Wl,-R/home/cf/slib
or
gcc -g -o test main.c -lcalc_mean -L/home/cf/slib -Wl,-rpath=/home/cf/slib
&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;-Wl portion sends comma-separated options to the linker, so we tell it to send the -rpath option to the linker with our working directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="cpp" name="code"&gt;$ ldd main
 linux-gate.so.1 =&amp;gt;  (0x00a1b000)
 libcalc_mean.so =&amp;gt; /home/cf/slib/libcalc_mean.so (0x00e84000)
 libc.so.6 =&amp;gt; /lib/tls/i686/cmov/libc.so.6 (0x00359000)
 /lib/ld-linux.so.2 (0x007c3000)&lt;/pre&gt;
The rpath method is great since each program gets to list its shared library locations independently, so there are no issues with different programs looking in the wrong paths like there were for &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_LIBRARY_PATH&lt;/span&gt;. 

But rpath has its downsides even.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;
First, it requires all shared libraries be installed in a fixed location so that all users of your program will have access to those libraries in those locations. That means less flexibility in system configuration.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Second, if that library refers to a NFS mount or other network drive, you may experience undesirable delays–or worse–on program startup.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;References&lt;/span&gt;&lt;br /&gt;
1. &lt;a href="http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html" target="_blank"&gt;Yolinux&lt;/a&gt;&lt;br /&gt;
2. &lt;a href="http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html" target="_blank"&gt;CProgramming&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-6586624017391420226?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6Ilw3wHKuXE:OkI8aItgsO0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6Ilw3wHKuXE:OkI8aItgsO0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6Ilw3wHKuXE:OkI8aItgsO0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6Ilw3wHKuXE:OkI8aItgsO0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6Ilw3wHKuXE:OkI8aItgsO0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6Ilw3wHKuXE:OkI8aItgsO0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/6Ilw3wHKuXE" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-01-19T10:24:02.452+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><georss:featurename xmlns:georss="http://www.georss.org/georss">Bengaluru, Karnataka, India</georss:featurename><georss:point xmlns:georss="http://www.georss.org/georss">12.9715987 77.5945627</georss:point><georss:box xmlns:georss="http://www.georss.org/georss">12.724026199999999 77.2787057 13.2191712 77.91041969999999</georss:box></item><item><title>Creating and using shared libraries in Linux</title><link>http://codingfreak.blogspot.com/2009/12/creating-and-using-shared-libraries-in.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 09 Dec 2009 02:23:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6620477416567895101</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/N8tLHbo9CEn3ke5c1reQpoBaxIA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/N8tLHbo9CEn3ke5c1reQpoBaxIA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/N8tLHbo9CEn3ke5c1reQpoBaxIA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/N8tLHbo9CEn3ke5c1reQpoBaxIA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;What is a Shared Library ??&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
A library that is loaded into physical memory only once and reused by multiple processes via virtual memory.&amp;nbsp;&lt;/blockquote&gt;
Generally Shared libraries are .so (or in windows .dll) files.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;Why shared libraries ??&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt; They reduce memory consumption if used by more than one process, and they reduce the size of the executable.&lt;/li&gt;
&lt;li&gt;They make developing applications easier: a small change in the implementation of a function in the library don't need the user to recompile and relink his application code every time. You need to only relink if you make incompatible changes, such as adding arguments to a call or changing the size of a struct.&lt;/li&gt;
&lt;/ul&gt;
&lt;span style="color: #cc0000; font-weight: bold;"&gt;NOTE:&lt;/span&gt; Debugging using a shared library is slightly more difficult when compared with static libraries, because the debugger usually used on Linux,  gdb, has some problems with shared libraries.&lt;br /&gt;
&lt;a href="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s1600-h/Obey_Linux_by_simpletoker.jpg"&gt;&lt;img alt="codingfreak linux" border="0" id="BLOGGER_PHOTO_ID_5413196535000306226" src="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s320/Obey_Linux_by_simpletoker.jpg" style="cursor: pointer; display: block; height: 320px; margin: 0px auto 10px; text-align: center; width: 246px;" /&gt;&lt;/a&gt;&lt;br /&gt;
Let us see how to create a shared library on Linux. We use following source code files for this post.&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;calc_mean.h&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;#ifndef calc_mean_h__
#define calc_mean_h__
double mean(double, double);
#endif  // calc_mean_h__&lt;/pre&gt;
&lt;span style="font-weight: bold;"&gt;calc_mean.c&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;double mean(double a, double b)
{
return (a+b) / 2;
}&lt;/pre&gt;
&lt;span style="font-weight: bold;"&gt;main.c&lt;/span&gt; - We are including our shared library in this application.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;#include &amp;lt;stdio.h&amp;gt;
#include "calc_mean.h"

int main(int argc, char* argv[]) {

double v1, v2, m;
v1 = 5.2;
v2 = 7.9;

m  = mean(v1, v2);

printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);

return 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;1. Creating Object File with Position Independent Code &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
All the code that goes into a shared library needs to be &lt;span style="font-weight: bold;"&gt;position independent&lt;/span&gt;. We can make gcc emit position-independent code by passing it one of the command-line switches &lt;span style="font-weight: bold;"&gt;-fpic&lt;/span&gt; or &lt;span style="font-weight: bold;"&gt;-fPIC&lt;/span&gt; (the former is preferred, unless the modules have grown so large that the relocatable code table is simply too small in which case the compiler will emit an error message, and you have to use -fPIC).&lt;br /&gt;
&lt;br /&gt;
First we will create object files for all .c files that goes into a shared library.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;gcc -c -fPIC calc_mean.c -o calc_mean.o&lt;/pre&gt;
Above we are compiling calc_mean.c with -fPIC option and generating calc_mean.o object file.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;2. Creating Shared library with the Object File&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;
Every shared library has a prefix "lib", the name of the library, the phrase ".so", followed by a period and a version number that is incremented whenever the interface changes (as a special exception, the lowest-level C libraries don't start with "lib"). 
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;gcc -shared -o libcalc_mean.so calc_mean.o&lt;/pre&gt;
Above command on successful produces a shared library named "&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-weight: bold;"&gt;libcalc_mean.so&lt;/span&gt;".&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-weight: bold;"&gt;-shared&lt;/span&gt;: Produces a shared object which can then be linked with other objects to form an executable.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;3. Using the Shared Library&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Now let us link the created shared library with our application. Compile &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;main.c&lt;/span&gt; as shown below&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;$ gcc -o test main.c -lcalc_mean
/usr/bin/ld: cannot find -lcalc_mean
collect2: ld returned 1 exit status&lt;/pre&gt;
The linker doesn’t know where to find &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;libcalc_mean&lt;/span&gt;. But why ?&lt;br /&gt;
GCC has a list of places to look by default for shared libraries, but our directory is not in that list. Bingo that's the reason compilation failed at linking level.
&lt;br /&gt;&lt;br /&gt;
Now we need to tell GCC where to find libcalc_mean.so. We will do that with the -L option.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;gcc -o test main.c -lcalc_mean -L/home/cf/slib&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;&lt;span style="font-weight: bold;"&gt;-l&lt;/span&gt; option tells the compiler to look for a file named &lt;span style="font-style: italic;"&gt;libsomething.so&lt;/span&gt; The &lt;span style="font-style: italic;"&gt;something&lt;/span&gt; is specified by the argument immediately following the “-l”. i.e. -lmean&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-weight: bold;"&gt;-L&lt;/span&gt; option tells the compiler where to find the library. The path to the directory containing the shared libraries is followed by "-L". If no “-L” is specified, the compiler will search the usual locations. "&lt;span style="font-weight: bold;"&gt;-L.&lt;/span&gt;" means looking for the shared libraries in the current directory and "&lt;span style="font-weight: bold;"&gt;-L/home/cf/lib&lt;/span&gt;" means looking for the shared libraries at "/opt/lib" path. You can specify as many “-l” and “-L” options as you like.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;span style="background-color: #f3f3f3; color: red;"&gt;NOTE&lt;/span&gt;:&lt;/b&gt; It would be a better idea to move all personal shared libraries in one directory rather in the current directory. For easy understanding I am moving "libcalc_mean.so" to "/home/cf/slib".&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;mv libcalc_mean.so /home/cf/slib&lt;/pre&gt;
Now compile &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;main.c&lt;/span&gt;. It would be successful and creates an executable named "&lt;b&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;test&lt;/span&gt;&lt;/b&gt;".&lt;br /&gt;
Let us check if the path to our shared library is included successfully into the executable by linker as shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;ldd executablename&lt;/i&gt;
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;$ ldd test 
 linux-gate.so.1 =&amp;gt;  (0x00332000)
 libcalc_mean.so =&amp;gt; not found
 libc.so.6 =&amp;gt; /lib/tls/i686/cmov/libc.so.6 (0x006aa000)
 /lib/ld-linux.so.2 (0x00db9000)&lt;/pre&gt;
You can see that linker cannot find our shared library &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;libcalc_mean.so&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;
Basically libraries are present in &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;/usr/lib&lt;/span&gt; amongst other places. Static libraries (.a suffix) are incorporated into the binary at link time, whereas dynamic ones (.so suffix) are referenced by location.&lt;br /&gt;
&lt;br /&gt;

Check the &lt;a href="http://codingfreak.blogspot.com/2010/11/creating-shared-libraries-in-linux-part.html" target="_blank"&gt;PART2&lt;/a&gt; of this article to understand further&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-6620477416567895101?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=inRkllD0q28:B6bL83465oc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=inRkllD0q28:B6bL83465oc:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=inRkllD0q28:B6bL83465oc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=inRkllD0q28:B6bL83465oc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=inRkllD0q28:B6bL83465oc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=inRkllD0q28:B6bL83465oc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/inRkllD0q28" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-01-19T10:24:51.248+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_MQa_rKnACRw/Sx-JkXgRkjI/AAAAAAAAB0s/8j8hp4yM7zk/s72-c/Obey_Linux_by_simpletoker.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">8</thr:total></item><item><title>Asterix &amp; Obelix collection - Part3</title><link>http://codingfreak.blogspot.com/2009/12/asterix-obelix-collection-part3.html</link><category>Comic</category><category>Asterix</category><author>noreply@blogger.com (Ajith)</author><pubDate>Thu, 19 Nov 2009 03:55:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-4114069932208389952</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4fwxmwKeNf-fTY2EUck_T8dFxf0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4fwxmwKeNf-fTY2EUck_T8dFxf0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4fwxmwKeNf-fTY2EUck_T8dFxf0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4fwxmwKeNf-fTY2EUck_T8dFxf0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and Caesar's Gift&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/Syy_pego7gI/AAAAAAAAB08/xPYTFIsUugM/s1600-h/Asterix+and+Caesar%27s+Gift.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/Syy_pego7gI/AAAAAAAAB08/xPYTFIsUugM/s320/Asterix+and+Caesar%27s+Gift.jpg" alt="" id="BLOGGER_PHOTO_ID_5416915171105369602" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25076039/845dc204/21-_Asterix_and_Caesars_Gift.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and the Great Crossing&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzAIvQlMxI/AAAAAAAAB1E/o47kqafKm74/s1600-h/Asterix+and+the+Great+Crossing.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzAIvQlMxI/AAAAAAAAB1E/o47kqafKm74/s320/Asterix+and+the+Great+Crossing.jpg" alt="" id="BLOGGER_PHOTO_ID_5416915708177363730" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25077168/37530b85/22-_Asterix_and_the_Great_Cros.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Obelix and Co.&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzAZ0ZEzRI/AAAAAAAAB1M/QjQ6UtHQ9OY/s1600-h/Obelix+and+Co..jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzAZ0ZEzRI/AAAAAAAAB1M/QjQ6UtHQ9OY/s320/Obelix+and+Co..jpg" alt="" id="BLOGGER_PHOTO_ID_5416916001612942610" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25077624/5fc6942f/23-_Obelix_and_Co.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix in Belgium&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_MQa_rKnACRw/SyzAq6LtYTI/AAAAAAAAB1U/DkNaJQA80HQ/s1600-h/Asterix+in+Belgium.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://3.bp.blogspot.com/_MQa_rKnACRw/SyzAq6LtYTI/AAAAAAAAB1U/DkNaJQA80HQ/s320/Asterix+in+Belgium.jpg" alt="" id="BLOGGER_PHOTO_ID_5416916295225270578" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25078117/b0ce9085/24-_Asterix_in_Belgium.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and the Great Divide&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/SyzA30WymYI/AAAAAAAAB1c/c04qeh0a_h0/s1600-h/Asterix+and+the+Great+Divide.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/SyzA30WymYI/AAAAAAAAB1c/c04qeh0a_h0/s320/Asterix+and+the+Great+Divide.jpg" alt="" id="BLOGGER_PHOTO_ID_5416916516999436674" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25078726/e8698f62/25-_Asterix_and_the_Great_Divi.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and the Black Gold&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzBGXlq-qI/AAAAAAAAB1k/YkqDc87Hhc0/s1600-h/Asterix+and+the+Black+Gold.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/SyzBGXlq-qI/AAAAAAAAB1k/YkqDc87Hhc0/s320/Asterix+and+the+Black+Gold.jpg" alt="" id="BLOGGER_PHOTO_ID_5416916766975261346" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25079290/5c895612/26-_Asterix_and_the_Black_Gold.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and Son&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/SyzBTzASDWI/AAAAAAAAB1s/LNHanR76rz0/s1600-h/Asterix+and+Son.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/SyzBTzASDWI/AAAAAAAAB1s/LNHanR76rz0/s320/Asterix+and+Son.jpg" alt="" id="BLOGGER_PHOTO_ID_5416916997672930658" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25079802/6ed209a1/27-_Asterix_and_Son.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and the Magic Carpet&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_MQa_rKnACRw/SyzBi8yZJ5I/AAAAAAAAB10/H1OgWOQXIyM/s1600-h/Asterix+and+the+Magic+Carpet.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://3.bp.blogspot.com/_MQa_rKnACRw/SyzBi8yZJ5I/AAAAAAAAB10/H1OgWOQXIyM/s320/Asterix+and+the+Magic+Carpet.jpg" alt="" id="BLOGGER_PHOTO_ID_5416917257997068178" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25500229/59443554/28-_Asterix_and_the_Magic_Carp.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and the Secret Weapon&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/SyzBu4Or7gI/AAAAAAAAB18/98LxJSvbBJE/s1600-h/Asterix+and+the+Secret+Weapon.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/SyzBu4Or7gI/AAAAAAAAB18/98LxJSvbBJE/s320/Asterix+and+the+Secret+Weapon.jpg" alt="" id="BLOGGER_PHOTO_ID_5416917462931992066" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25501428/92721e15/29-_Asterix_and_the_Secret_Wea.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 0, 0);"&gt;Asterix and Obelix All at Sea&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/SyzB-dadUTI/AAAAAAAAB2E/vxvltZoDn0U/s1600-h/Asterix+and+Obelix+All+at+Sea.jpg"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; cursor: pointer; width: 224px; height: 320px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/SyzB-dadUTI/AAAAAAAAB2E/vxvltZoDn0U/s320/Asterix+and+Obelix+All+at+Sea.jpg" alt="" id="BLOGGER_PHOTO_ID_5416917730611515698" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.4shared.com/file/25502118/ad2120d3/30-_Asterix_and_Obelix_all_at_.html" target="_blank"&gt;Download Link&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-4114069932208389952?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=c_4HLwD4hNs:IC_ietHGWN0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=c_4HLwD4hNs:IC_ietHGWN0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=c_4HLwD4hNs:IC_ietHGWN0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=c_4HLwD4hNs:IC_ietHGWN0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=c_4HLwD4hNs:IC_ietHGWN0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=c_4HLwD4hNs:IC_ietHGWN0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/c_4HLwD4hNs" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-01-01T14:46:53.372+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_MQa_rKnACRw/Syy_pego7gI/AAAAAAAAB08/xPYTFIsUugM/s72-c/Asterix+and+Caesar%27s+Gift.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Using 'find' command</title><link>http://codingfreak.blogspot.com/2009/11/using-find-command.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 03 Nov 2009 21:46:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-9013350577012341738</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zK1C-C3xCDe5ppcgRNZvXy4uNoE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zK1C-C3xCDe5ppcgRNZvXy4uNoE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zK1C-C3xCDe5ppcgRNZvXy4uNoE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zK1C-C3xCDe5ppcgRNZvXy4uNoE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;'&lt;span style="font-weight:bold;"&gt;find&lt;/span&gt;' is one of the useful commands available in Unix/Linux systems. In this article I am trying to use '&lt;span style="font-weight:bold;"&gt;find&lt;/span&gt;' in a effective way.&lt;br /&gt;&lt;br /&gt;Basic syntax of &lt;span style="font-weight:bold;"&gt;find&lt;/span&gt; command as per man page is - &lt;span style="font-weight: bold;"&gt;find [path...] [expression]&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/SvEhJACyLSI/AAAAAAAAByE/662byY5gkt8/s1600-h/magnify.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 240px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/SvEhJACyLSI/AAAAAAAAByE/662byY5gkt8/s320/magnify.jpg" alt="codingfreak find" id="BLOGGER_PHOTO_ID_5400133866708151586" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;NOTE:&lt;/span&gt; There are many options available for &lt;span style="font-weight: bold;"&gt;find&lt;/span&gt; command which are available in man page for &lt;span style="font-weight: bold;"&gt;find&lt;/span&gt;. Only some of those options are shown in this tutorial.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Finding a particular file in your system&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;$ find / -name 'filename' 2&gt;/dev/null&lt;br /&gt;$ find / -name 'filename' 2&gt;errors.txt&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;/&lt;/span&gt; - Start searching from the root directory (i.e / directory)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;-name&lt;/span&gt; - Given search text is the filename rather than any other attribute of a file 'filename'. Always enclose the filename in single quotes...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;NOTE:&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;2&gt;/dev/null&lt;/span&gt; is not related to find tool as such. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2&gt;/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Alternatively you could use &lt;span style="font-weight:bold;"&gt;2&gt;error.txt&lt;/span&gt; where after the search is completed you would have a file named error.txt in the current directory with all the error messages in it. &lt;pre name="code" class="cpp"&gt;$ find -name 'met*'&lt;/pre&gt;The above command would start searching for the files that begin with the letters 'met' within the current directory and the directories that are present within the current directory.&lt;br /&gt;&lt;br /&gt;If no paths are given, the current directory is used.  If no expression is given, the expression ‘-print’ is used.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Searching with respect to type of the file (-type)&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;$find . -name 'temp'&lt;br /&gt;./keepout/temp&lt;br /&gt;./temp&lt;br /&gt;&lt;br /&gt;$find . -name 'temp' -type d&lt;br /&gt;./temp&lt;/pre&gt;Where d - directory, p - named pipe (FIFO), f - regular file and so on&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Ignoring case-sensitivity (-iname)&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;$ find /home/temp -iname 'index*'&lt;/pre&gt;This command searchs for a file starting with string 'index' without considering the case of the filename. So all files starting with any combination of letters in upper and lower case such as INDEX or indEX or index would be returned.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Searching for a file based on size and time&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$ find /home/songs -name '*.mp3' -size -5000k&lt;br /&gt;&lt;br /&gt;$ find / -size +10000k&lt;/pre&gt;First command finds within a directory called /home/songs, only those mp3 files that have a size less than 5000 Kilobytes.&lt;pre name="code" class="cpp"&gt;$ find /home/temp -amin -10 -name '*.c'&lt;br /&gt;$ find /home/temp -atime -2 -name '*.c'&lt;br /&gt;$ find /home/temp -mmin -10 -name '*.c'&lt;br /&gt;$ find /home/temp -mtime -2 -name '*.c'&lt;/pre&gt;The 1st command searches for those files that are present in the directory /home/temp and its subdirectories which end in .c and which have been accessed in the last 10 minutes.&lt;br /&gt;&lt;br /&gt;The 2nd command does the same but searches for those files that have been accessed in the last 10 hours.&lt;br /&gt;&lt;br /&gt;The 3rd and the 4th commands do the same as the 1st and 2nd commands but they search for modified files rather than accessed files. Only if the contents of the files have been modified, would their names be returned in the search results.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$ find / -mount -name 'win*'&lt;/pre&gt;This command searches for files starting with the letters 'win' in their filenames. The only difference is that the mounted filesystems would not be searched for this time. This is useful when you have your Windows partitions mounted by default. And a search for 'win' might return many files on those partitions, which you may not be really interested in. This is only one use of -mount parameter.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$ find /home/songs -name 'Metallica*' -and -size +10000k&lt;br /&gt;$ find /home/songs -size +10000k ! -name "Metallica*"&lt;br /&gt;$ find /home/songs -name 'Metallica*' -or -size +10000k&lt;/pre&gt;Boolean operators such as AND, OR and NOT make find an extremely useful tool.&lt;br /&gt;&lt;br /&gt;The 1st command searches within the directory /songs for files that have their names beginning with 'Metallica' and whose size is greater than 10000 kilobytes (&gt; 10 MB).&lt;br /&gt;&lt;br /&gt;The 2nd command searches in the same directory as above case but only for files that are greater than 10MB, but they should not have 'Metallica' as the starting of their filenames.&lt;br /&gt;&lt;br /&gt;The 3rd command searches in the same directory for files that begin with 'Metallica' in their names or all the files that are greater than 10 MB in size.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;How to apply a unix command to a set of files (-exec) ?&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;$ find . -name '*.sh' -exec chmod o+r '{}' \; -print&lt;/pre&gt;This command will search in the current directory and all sub directories. All files ending with .sh extension will be processed by the chmod -o+r command. The argument &lt;span style="font-weight: bold;"&gt;'{}'&lt;/span&gt; inserts each found file into the chmod command line. The &lt;span style="font-weight: bold;"&gt;\;&lt;/span&gt; argument indicates the exec command line has ended.&lt;br /&gt;&lt;br /&gt;The end results of this command is all .sh files have the other permissions set to read access (if the operator is the owner of the file).&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Searching for a string in a selection of files (-exec grep ...).&lt;/span&gt;&lt;pre name="code" class="cpp"&gt;$ find . -exec grep "hello" '{}' \; -print&lt;/pre&gt;Prints all files that contain the string 'hello' will have their path printed to standard output.&lt;br /&gt;&lt;br /&gt;If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.&lt;pre name="code" class="cpp"&gt;find . -exec grep -q "hello" '{}' \; -print&lt;/pre&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Finding Empty files (-empty)&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;$find . -empty&lt;/pre&gt;To delete empty files in the current directory:&lt;pre name="code" class="cpp"&gt;$ find . -empty -maxdepth 1 -exec rm '{}' \;&lt;/pre&gt;For more examples try out&lt;br /&gt;&lt;br /&gt;1. &lt;a href="http://www.linux.ie/newusers/beginners-linux-guide/find.php" target="_blank"&gt;linux.ie&lt;/a&gt;&lt;br /&gt;2. &lt;a href="http://www.devdaily.com/unix/edu/examples/find.shtml" target="_blank"&gt;Devdaily&lt;/a&gt;&lt;br /&gt;3. &lt;a href="http://content.hccfl.edu/pollock/unix/findcmd.htm" target="_blank"&gt;hccfl.edu&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-9013350577012341738?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=W9WbKUH2UuY:Zq6X-GvNc-k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=W9WbKUH2UuY:Zq6X-GvNc-k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=W9WbKUH2UuY:Zq6X-GvNc-k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=W9WbKUH2UuY:Zq6X-GvNc-k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=W9WbKUH2UuY:Zq6X-GvNc-k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=W9WbKUH2UuY:Zq6X-GvNc-k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/W9WbKUH2UuY" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-03-30T09:08:41.259+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_MQa_rKnACRw/SvEhJACyLSI/AAAAAAAAByE/662byY5gkt8/s72-c/magnify.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Are you a Hacker ??</title><link>http://codingfreak.blogspot.com/2009/10/are-you-hacker.html</link><category>Hacking</category><category>Facts</category><author>noreply@blogger.com (Ajith)</author><pubDate>Fri, 30 Oct 2009 02:20:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5386961662624757191</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/y3iQ-4sygS5n_OLohrgi5hhe3Js/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y3iQ-4sygS5n_OLohrgi5hhe3Js/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/y3iQ-4sygS5n_OLohrgi5hhe3Js/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y3iQ-4sygS5n_OLohrgi5hhe3Js/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;pre  style="font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;from ReDragon on IRC, handed to newbies...&lt;br /&gt;&lt;br /&gt;Are You a Hacker?&lt;br /&gt;&lt;br /&gt;Take a little quiz for me today.  Tell me if you fit this description.  &lt;br /&gt;You got your net account several months ago.  You have been surfing the &lt;br /&gt;net, and you laugh at those media reports of the information superhighway.  &lt;br /&gt;You have a red box, you don't have to pay for phone calls. You have &lt;br /&gt;crackerjack, and you have run it on the password file at a unix you got &lt;br /&gt;an account on.  Everyone at your school is impressed by your computer &lt;br /&gt;knowledge, you are the one the teachers ask for help.  Does this sound &lt;br /&gt;like you? You are not a hacker.&lt;br /&gt;&lt;br /&gt;There are thousands of you out there.  You buy 2600 and you ask &lt;br /&gt;questions.  You read phrack and you ask questions.  You join #hack and &lt;br /&gt;you ask questions.  You ask all of these questions, and you ask what is &lt;br /&gt;wrong with that?  After all, to be a hacker is to question things, is &lt;br /&gt;it not?  But, you do not want knowledge.  You want answers. You do not &lt;br /&gt;want to learn how things work.  You want answers.  You do not want to &lt;br /&gt;explore.  All you want to know is the answer to your damn questions. &lt;br /&gt;You are not a hacker.&lt;br /&gt;&lt;br /&gt;Hacking is not about answers.  Hacking is about the path you take to &lt;br /&gt;find the answers.  If you want help, don't ask for answers, ask for &lt;br /&gt;a pointer to the path you need to take to find out those answers for &lt;br /&gt;yourself.  Because it is not the people with the answers that are the &lt;br /&gt;hackers, it is the people that are travelling along the path.&lt;br /&gt;&lt;br /&gt;                                          -ReDragon&lt;/span&gt;&lt;/pre&gt;&lt;span class="fullpost"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-5386961662624757191?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=-VYaUYWZV-Q:w646zUAvg0o:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=-VYaUYWZV-Q:w646zUAvg0o:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=-VYaUYWZV-Q:w646zUAvg0o:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=-VYaUYWZV-Q:w646zUAvg0o:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=-VYaUYWZV-Q:w646zUAvg0o:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=-VYaUYWZV-Q:w646zUAvg0o:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/-VYaUYWZV-Q" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-27T12:02:28.348+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Signals in Linux - Blocking Signals</title><link>http://codingfreak.blogspot.com/2009/10/signals-in-linux-blocking-signals.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Sun, 04 Oct 2009 23:18:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2933746973098326919</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iHrd3C5baOW6T0zsQ3Ihre4pGc4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iHrd3C5baOW6T0zsQ3Ihre4pGc4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iHrd3C5baOW6T0zsQ3Ihre4pGc4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iHrd3C5baOW6T0zsQ3Ihre4pGc4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Blocking a signal means telling the operating system to hold it and deliver it later when it is unblocked. Between the time when it is generated and when it is delivered a signal is said to be &lt;span style="font-weight: bold;"&gt;pending&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Generally, a program does not block signals indefinitely - it might as well ignore them by setting their actions to &lt;span style="font-weight: bold;"&gt;SIG_IGN&lt;/span&gt;. But it is useful to block signals briefly, to prevent them from interrupting sensitive operations.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;Is Blocking a signal similar to Ignoring a signal ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;No, blocking a signal is different from ignoring a signal. When a process blocks a signal, the operating system does not deliver the signal until the process unblocks the signal. A process blocks a signal by modifying its signal mask with &lt;span style="font-weight: bold;"&gt;sigprocmask&lt;/span&gt;. But when a process ignores a signal, the signal is delivered and the process handles it by throwing it away.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);font-size:130%;" &gt;How Blocking Signals is Useful ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Temporary blocking of signals with &lt;span style="font-weight: bold;"&gt;sigprocmask&lt;/span&gt; gives you a way to prevent interrupts during critical parts of your code. If signals arrive in that part of the program, they are delivered later, after you unblock them.&lt;br /&gt;&lt;br /&gt;One example where this is useful is for sharing data between a signal handler and the rest of the program. If the type of the data is not &lt;span style="font-weight: bold;"&gt;sig_atomic_t&lt;/span&gt;, then the signal handler could run when the rest of the program has only half finished reading or writing the data. This would lead to confusing consequences.&lt;br /&gt;&lt;br /&gt;To make the program reliable, you can prevent the signal handler from running while the rest of the program is examining or modifying that data - by blocking the appropriate signal around the parts of the program that touch the data. Blocking signals is also necessary when you want to perform a certain action only if a signal has not arrived. &lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;All signal blocking functions use a data structure called a &lt;span style="font-weight: bold;"&gt;signal set&lt;/span&gt; to specify what signals are affected. Thus, every activity involves two stages: creating the signal set, and then passing it as an argument to a library function. These facilities are declared in the header file signal.h.&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-weight: bold;"&gt;sigset_t&lt;/span&gt; data type is used to represent a signal set. Internally, it may be implemented as either an integer or structure type. For portability, use only the functions described below to initialize, change, and retrieve information from &lt;span style="font-weight: bold;"&gt;sigset_t&lt;/span&gt; objects - don't try to manipulate them directly.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-weight: bold;"&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int sigemptyset(sigset_t *set);&lt;br /&gt;&lt;br /&gt;int sigfillset(sigset_t *set);&lt;br /&gt;&lt;br /&gt;int sigaddset(sigset_t *set, int signum);&lt;br /&gt;&lt;br /&gt;int sigdelset(sigset_t *set, int signum);&lt;br /&gt;&lt;br /&gt;int sigismember(const sigset_t *set, int signum);&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;sigemptyset&lt;/span&gt; function initializes the signal set given by set to empty, with all signals excluded from the set.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;sigfillset&lt;/span&gt; function initializes set to full, including all signals.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;sigaddset&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;sigdelset&lt;/span&gt; functions add and delete respectively signal signum from set.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;sigismember&lt;/span&gt; function tests whether signum is a member of set.&lt;br /&gt;&lt;br /&gt;Objects of type &lt;span style="font-weight: bold;"&gt;sigset_t&lt;/span&gt; must be initialized by a call to either &lt;span style="font-weight: bold;"&gt;sigemptyset&lt;/span&gt; or &lt;span style="font-weight: bold;"&gt;sigfillset&lt;/span&gt; before being passed to the functions &lt;span style="font-weight: bold;"&gt;sigaddset, sigdelset and sigismember&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man3/sigsetops.3.html" target="_blank"&gt;&lt;span style="font-weight: bold;"&gt;man 3 sigsetops&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The collection of signals that are currently blocked is called the &lt;span style="font-weight: bold;"&gt;signal mask&lt;/span&gt;. Each process has its own &lt;span style="font-weight: bold;"&gt;signal mask&lt;/span&gt;. When you create a new process, it inherits its parent's mask. You can block or unblock signals with total flexibility by modifying the &lt;span style="font-weight: bold;"&gt;signal mask&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);&lt;/blockquote&gt;&lt;br /&gt;In a traditional single-threaded application, &lt;span style="font-weight: bold;"&gt;sigprocmask&lt;/span&gt; system call can be used to fetch and/or manipulate the signal mask of the calling thread.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;how&lt;/span&gt; determines what operation to be performed on the signal mask.&lt;br /&gt;If &lt;span style="font-weight: bold;"&gt;oldset&lt;/span&gt; is non-null, the previous value of the signal mask is stored in oldset.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;set&lt;/span&gt; determines list of signals to be set in blocking state.&lt;br /&gt;&lt;br /&gt;Signals, such as &lt;span style="font-weight: bold;"&gt;SIGSTOP&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;SIGKILL&lt;/span&gt;, cannot be blocked. If an attempt is made to block these signals, the system ignores the request without reporting an error.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;NOTE:&lt;/span&gt; Do not use &lt;span style="font-weight: bold;"&gt;sigprocmask&lt;/span&gt; in multi-threaded processes, because each thread has its own signal mask and there is no single process signal mask. According to POSIX, the behavior of &lt;span style="font-weight: bold;"&gt;sigprocmask&lt;/span&gt; in a multi-threaded process is "unspecified". Instead, use &lt;span style="font-weight: bold;"&gt;pthread_sigmask&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man2/sigprocmask.2.html" target="_blank"&gt;&lt;span style="font-weight: bold;"&gt;man 2 sigprocmask&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In the below example we try to block and unblock the &lt;span style="font-weight: bold;"&gt;SIGINT&lt;/span&gt; signal continually in a loop. If a user enters &lt;span style="font-weight: bold;"&gt;Ctrl-C&lt;/span&gt; while &lt;span style="font-weight: bold;"&gt;SIGINT&lt;/span&gt; is blocked, then the program terminates only after it is unblocked. If a user types &lt;span style="font-weight: bold;"&gt;Ctrl-C&lt;/span&gt; while &lt;span style="font-weight: bold;"&gt;SIGINT&lt;/span&gt; is unblocked, the program terminates immediately.&lt;pre name="code" class="cpp"&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main(int argc,  char *argv[]) {&lt;br /&gt;int i;&lt;br /&gt;sigset_t intmask;&lt;br /&gt;int repeatfactor;&lt;br /&gt;double y = 0.0;&lt;br /&gt;&lt;br /&gt;if ((sigemptyset(&amp;amp;intmask) == -1) || (sigaddset(&amp;amp;intmask, SIGINT) == -1)){&lt;br /&gt;   perror("Failed to initialize the signal mask");&lt;br /&gt;   return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;for ( ; ; ) {&lt;br /&gt;   printf("Entering BLOCK state\n");&lt;br /&gt;   if (sigprocmask(SIG_BLOCK, &amp;amp;intmask, NULL) == -1)&lt;br /&gt;       break;&lt;br /&gt;   fprintf(stderr, "SIGINT signal blocked\n");&lt;br /&gt;   sleep(2);&lt;br /&gt;&lt;br /&gt;   printf("Leaving Blocking State &amp;amp; Entering UNBLOCK state\n");&lt;br /&gt;   if (sigprocmask(SIG_UNBLOCK, &amp;amp;intmask, NULL) == -1)&lt;br /&gt;       break;&lt;br /&gt;   fprintf(stderr, "SIGINT signal unblocked\n");&lt;br /&gt;   sleep(2);&lt;br /&gt;}&lt;br /&gt;perror("Failed to change signal mask");&lt;br /&gt;return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;$ ./a.out&lt;br /&gt;Entering BLOCK state&lt;br /&gt;SIGINT signal blocked&lt;br /&gt;Leaving Blocking State &amp;amp; Entering UNBLOCK state&lt;br /&gt;SIGINT signal unblocked&lt;br /&gt;^C&lt;br /&gt;&lt;br /&gt;$ ./a.out&lt;br /&gt;Entering BLOCK state&lt;br /&gt;SIGINT signal blocked&lt;br /&gt;^CLeaving Blocking State &amp;amp; Entering UNBLOCK state&lt;br /&gt;&lt;br /&gt;$&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-2933746973098326919?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=hmRKt1yLlP4:hYGZ5fuQg8M:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=hmRKt1yLlP4:hYGZ5fuQg8M:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=hmRKt1yLlP4:hYGZ5fuQg8M:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=hmRKt1yLlP4:hYGZ5fuQg8M:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=hmRKt1yLlP4:hYGZ5fuQg8M:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=hmRKt1yLlP4:hYGZ5fuQg8M:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/hmRKt1yLlP4" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-27T14:16:55.920+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Signals in Linux - Catching and Ignoring Signals - sigaction</title><link>http://codingfreak.blogspot.com/2009/09/catching-and-ignoring-signals-sigaction.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 22 Sep 2009 02:00:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-876759143490144344</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0XdKHfxppgbTjPkoFJkFrOpdYZw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0XdKHfxppgbTjPkoFJkFrOpdYZw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0XdKHfxppgbTjPkoFJkFrOpdYZw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0XdKHfxppgbTjPkoFJkFrOpdYZw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;The &lt;span style="font-weight: bold;"&gt;sigaction&lt;/span&gt; system call has the same basic effect as &lt;span style="font-weight: bold;"&gt;signal&lt;/span&gt; system call: to specify how a signal should be handled by the process. &lt;br /&gt;&lt;br /&gt;But &lt;span style="font-weight: bold;"&gt;sigaction&lt;/span&gt; offers more control over the signal mechanism, at the expense of more complexity. In particular, &lt;span style="font-weight: bold;"&gt;sigaction&lt;/span&gt; allows you to specify additional flags to control when the signal is generated and how the handler is invoked.&lt;br /&gt;&lt;br /&gt;Hence for reference we can say that &lt;span style="font-weight:bold;"&gt;sigaction&lt;/span&gt; is more like opensource Linux OS flavours more options, more control and complex for normal users than the proprietary stuff.&lt;br /&gt;&lt;blockquote&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int sigaction(int signum, const struct sigaction *action,struct sigaction *oldaction);&lt;/blockquote&gt;&lt;br /&gt;signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP.&lt;br /&gt;&lt;br /&gt;The action argument is used to set up a new action for the signal signum, while the oldaction argument is used to return information about the action previously associated with this symbol. If action is non-null, the new action for signal signum is installed from action. If oldaction is non-null, the previous action is saved in oldaction.&lt;br /&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man2/sigaction.2.html" target="_blank"&gt;&lt;span style="font-weight: bold;"&gt;man 2 sigaction&lt;/span&gt;&lt;/a&gt;&lt;pre name="code" class="cpp"&gt;struct sigaction {&lt;br /&gt; void     (*sa_handler)(int);   /*SIG_DFL, SIG_IGN, or&lt;br /&gt;                                a function pointer*/&lt;br /&gt; void     (*sa_sigaction)(int, siginfo_t *, void *);&lt;br /&gt; sigset_t   sa_mask;&lt;br /&gt; int        sa_flags;&lt;br /&gt; void     (*sa_restorer)(void);&lt;br /&gt;};&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Portability Note:&lt;/span&gt; The basic signal function is a feature of ISO C, while sigaction is part of the POSIX.1 standard. If you are concerned about portability to non-POSIX systems, then you should use the signal function instead.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;static volatile sig_atomic_t doneflag = 0;&lt;br /&gt;&lt;br /&gt;int main (void) {&lt;br /&gt; struct sigaction act;&lt;br /&gt;&lt;br /&gt; act.sa_handler = setdoneflag;/* set up signal handler */&lt;br /&gt; act.sa_flags = 0;&lt;br /&gt; if ((sigemptyset(&amp;amp;act.sa_mask) == -1) ||&lt;br /&gt;       (sigaction(SIGINT, &amp;amp;act, NULL) == -1)) {&lt;br /&gt;     perror("Failed to set SIGINT handler");&lt;br /&gt;     return 1;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; while (!doneflag) {&lt;br /&gt;     printf("press CTRL+C to kill the loop\n");&lt;br /&gt;     sleep(1);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; printf("Program terminating ...\n");&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;$ ./a.out&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;^C&lt;br /&gt;&lt;br /&gt;In SignalHandler - setdoneflag&lt;br /&gt;Program terminating ...&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;sig_atomic_t&lt;/span&gt; is an integer type that is guaranteed by the standard to not be partially written or partially read in the presence of asynchronous interrupts.&lt;br /&gt;&lt;br /&gt;If we set act.sa_handler as &lt;span style="font-weight: bold;"&gt;SIG_DFL&lt;/span&gt; then the program simply terminates. If we set act.sa_handler as &lt;span style="font-weight: bold;"&gt;SIG_IGN&lt;/span&gt; then the program simply keeps on looping as we are ignoring the &lt;span style="font-weight: bold;"&gt;SIG_INT&lt;/span&gt; signal.&lt;br /&gt;&lt;br /&gt;In the POSIX base standard, a signal handler is an ordinary function that returns void and has one integer parameter. When the operating system delivers the signal, it sets this parameter to the number of the signal that was delivered. Most signal handlers ignore this value, but it is possible to have a single signal handler for many signals. The usefulness of signal handlers is limited by the inability to pass values to them. This capability has been added to the &lt;span style="font-weight: bold;"&gt;POSIX:RTS&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;POSIX:XSI&lt;/span&gt; Extensions, which can use the alternative sa_sigaction field of the struct sigaction structure to specify a handler.&lt;br /&gt;&lt;br /&gt;If &lt;span style="font-weight: bold;"&gt;SA_SIGINFO&lt;/span&gt; is specified in sa_flags, then sa_sigaction (instead of sa_handler) specifies the signal-handling function for signum. This function receives the signal number as its first argument, a pointer to a siginfo_t as its second argument and a pointer to ucontext_t (cast to void *) as its third argument. The siginfo_t argument to sa_sigaction is a struct with the following elements:&lt;pre name="code" class="cpp"&gt;siginfo_t {&lt;br /&gt;   int      si_signo;    /* Signal number */&lt;br /&gt;   int      si_errno;    /* An errno value */&lt;br /&gt;   int      si_code;     /* Signal code */&lt;br /&gt;   int      si_trapno;   /* Trap number that caused&lt;br /&gt;                       hardware-generated signal&lt;br /&gt;                       (unused on most architectures)*/&lt;br /&gt;   pid_t    si_pid;      /* Sending process ID */&lt;br /&gt;   uid_t    si_uid;      /* Real user ID of sending&lt;br /&gt;                         process */&lt;br /&gt;   int      si_status;   /* Exit value or signal */&lt;br /&gt;   clock_t  si_utime;    /* User time consumed */&lt;br /&gt;   clock_t  si_stime;    /* System time consumed */&lt;br /&gt;   sigval_t si_value;    /* Signal value */&lt;br /&gt;   int      si_int;      /* POSIX.1b signal */&lt;br /&gt;   void    *si_ptr;      /* POSIX.1b signal */&lt;br /&gt;   int      si_overrun;  /* Timer overrun count;&lt;br /&gt;                         POSIX.1b timers */&lt;br /&gt;   int      si_timerid;  /* Timer ID; POSIX.1b timers */&lt;br /&gt;   void    *si_addr;     /* Memory location which&lt;br /&gt;                         caused fault */&lt;br /&gt;   int      si_band;     /* Band event */&lt;br /&gt;   int      si_fd;       /* File descriptor */&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;NOTE: &lt;/span&gt;si_signo, si_errno and si_code are defined for all signals (si_errno is generally unused on Linux). The rest of the struct may be a union, so that one should only read the fields that are meaningful for the given signal.&lt;pre name="code" class="cpp"&gt;static volatile sig_atomic_t doneflag = 0;&lt;br /&gt;&lt;br /&gt;static void setdoneflag(int sig, siginfo_t *siginfo, void *context)&lt;br /&gt;{&lt;br /&gt;  printf ("Signo     -  %d\n",siginfo-&amp;gt;si_signo);&lt;br /&gt;  printf ("SigCode   -  %d\n",siginfo-&amp;gt;si_code);&lt;br /&gt;  doneflag = 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main (int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt;  struct sigaction act;&lt;br /&gt;&lt;br /&gt;  memset (&amp;amp;act, '\0', sizeof(act));&lt;br /&gt;&lt;br /&gt;  act.sa_sigaction = &amp;amp;setdoneflag;&lt;br /&gt;&lt;br /&gt;  act.sa_flags = SA_SIGINFO;&lt;br /&gt;&lt;br /&gt;  if (sigaction(SIGINT, &amp;amp;act, NULL) &amp;lt; 0) {&lt;br /&gt;      perror ("sigaction");&lt;br /&gt;      return 1;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  while (!doneflag) {&lt;br /&gt;      printf("press CTRL+C to kill the Loop\n");&lt;br /&gt;      sleep(1);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  printf("Program terminating ...\n");&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;$ ./a.out&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;^C&lt;br /&gt;Signo     -  2&lt;br /&gt;SigCode   -  128&lt;br /&gt;Program terminating ...&lt;br /&gt;$ &lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-876759143490144344?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=szvlLVJ1NJA:KNz8eKZ_M4k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=szvlLVJ1NJA:KNz8eKZ_M4k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=szvlLVJ1NJA:KNz8eKZ_M4k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=szvlLVJ1NJA:KNz8eKZ_M4k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=szvlLVJ1NJA:KNz8eKZ_M4k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=szvlLVJ1NJA:KNz8eKZ_M4k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/szvlLVJ1NJA" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-04T13:46:01.016+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Signals in Linux - Catching and Ignoring Signals - signal</title><link>http://codingfreak.blogspot.com/2009/09/catching-and-ignoring-signals-signal.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 15 Sep 2009 01:49:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5797415292308275708</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/NYJBjQfrk8JNDWQs0LIAgJTCcsI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NYJBjQfrk8JNDWQs0LIAgJTCcsI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/NYJBjQfrk8JNDWQs0LIAgJTCcsI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NYJBjQfrk8JNDWQs0LIAgJTCcsI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;The simplest way to change the action for a signal is to use the &lt;span style="font-weight:bold;"&gt;signal&lt;/span&gt; system call. You can specify a built-in action (such as to ignore the signal), or you can establish a handler.&lt;br /&gt;&lt;blockquote&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;typedef void (*sighandler_t)(int);&lt;br /&gt;&lt;br /&gt;sighandler_t signal(int signum, sighandler_t handler)&lt;/blockquote&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;signal&lt;/span&gt; sets the disposition of the signal signum to handler, which is either &lt;span style="font-weight:bold;"&gt;SIG_IGN, SIG_DFL&lt;/span&gt;, or the address of a programmer-defined function (a "signal handler").&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;NOTE:&lt;/span&gt; The signals &lt;span style="font-weight: bold;"&gt;SIGKILL&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;SIGSTOP&lt;/span&gt; cannot be caught or ignored. The effects of &lt;span style="font-weight:bold;"&gt;signal&lt;/span&gt; system call in a multithreaded process are unspecified.&lt;br /&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man2/signal.2.html" target="_blank"&gt;&lt;span style="font-weight: bold;"&gt;man 2 signal&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;NOTE:&lt;/span&gt; The behavior of signal system call varies across different UNIX versions, and has also varied historically across different versions of Linux.  &lt;span style="font-weight: bold;"&gt;Avoid its use: use sigaction system call&lt;/span&gt; instead. Check man 2 signal for detailed information about various portability issues.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;&lt;br /&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;static volatile sig_atomic_t doneflag = 0;&lt;br /&gt;&lt;br /&gt;static void setdoneflag(int signo) {&lt;br /&gt;    printf(&amp;quot;\nIn SignalHandler - setdoneflag\n&amp;quot;);&lt;br /&gt;    doneflag = 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main (void) {&lt;br /&gt;&lt;br /&gt;    signal(SIGINT, setdoneflag);&lt;br /&gt;&lt;br /&gt;    while (!doneflag) {&lt;br /&gt;        printf(&amp;quot;press CTRL+C to kill the Loop\n&amp;quot;);&lt;br /&gt;        sleep(1);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    printf(&amp;quot;Program terminating ...\n&amp;quot;);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;$ ./a.out&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;press CTRL+C to kill the Loop&lt;br /&gt;^C&lt;br /&gt;In SignalHandler - setdoneflag&lt;br /&gt;Program terminating ...&lt;br /&gt;$ &lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-5797415292308275708?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=X9quE6QVbkI:bUeXFHDqzg8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=X9quE6QVbkI:bUeXFHDqzg8:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=X9quE6QVbkI:bUeXFHDqzg8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=X9quE6QVbkI:bUeXFHDqzg8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=X9quE6QVbkI:bUeXFHDqzg8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=X9quE6QVbkI:bUeXFHDqzg8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/X9quE6QVbkI" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-04T13:44:13.851+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Reading a string of length 'n' from Standard Input [STDIN]</title><link>http://codingfreak.blogspot.com/2009/09/reading-string-of-length-n-from.html</link><category>C</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 08 Sep 2009 04:37:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6400665611236729746</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FknfrdLH3eETeuaBp5fuWBRLqyk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FknfrdLH3eETeuaBp5fuWBRLqyk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FknfrdLH3eETeuaBp5fuWBRLqyk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FknfrdLH3eETeuaBp5fuWBRLqyk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;We know how to read a string from STDIN in C by using library functions like &lt;span style="font-weight:bold;"&gt;scanf&lt;/span&gt;, &lt;span style="font-weight:bold;"&gt;fgets&lt;/span&gt; and so on. By using these functions there is a chance for memory corruption and strange behaviour. For example while using scanf if we try to save a string of length more than the variable size there is a chance of memory corruption.&lt;br /&gt;&lt;br /&gt;So here in this post I am just trying to implement a function capable to read a string of length 'n' from STDIN without memory corruption and other bugs.&lt;br /&gt;&lt;br /&gt;Do help me by checking the code if there is a chance for further improvements.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;string.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define BUF_SIZE 6&lt;br /&gt;#define STRING_SIZE 4&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * void getStringStdin(char *, int , int );&lt;br /&gt; *&lt;br /&gt; * 1: BUF        :Pointer to the array of characters where input string &lt;br /&gt;is to be stored.&lt;br /&gt; * 2: BUF_LEN    :Is the length of the array of characters where the &lt;br /&gt;string is stored.buffer where we save the string.&lt;br /&gt; * 3: STRING_LEN :Is the length of the string.&lt;br /&gt; *&lt;br /&gt; * NOTE: STRING_LEN &amp;lt; BUF_LEN&lt;br /&gt; *&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;getStringStdin(char *buf, int buf_len, int str_len)&lt;br /&gt;{&lt;br /&gt;  int ch, len;&lt;br /&gt;  char *s;&lt;br /&gt;&lt;br /&gt;  if(str_len&amp;gt;=buf_len)&lt;br /&gt;  len=buf_len-1;&lt;br /&gt;  else&lt;br /&gt;  len=str_len;&lt;br /&gt;&lt;br /&gt;  printf (&amp;quot;\nEnter string of length %d(Remaining is ignored): &amp;quot;,len);&lt;br /&gt;&lt;br /&gt;  if( (fgets(buf, len+1, stdin)) != NULL )&lt;br /&gt;  {&lt;br /&gt;    s=my_strchr(buf,'\n');&lt;br /&gt;&lt;br /&gt;    if(s!=NULL)&lt;br /&gt;    {&lt;br /&gt;      *s='\0';&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       while ((ch = getchar()) != '\n' &amp;amp;&amp;amp; ch != EOF);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;  int i=0;&lt;br /&gt;  char buf[BUF_SIZE];&lt;br /&gt;&lt;br /&gt;  do&lt;br /&gt;  {&lt;br /&gt;    getString(buf, BUF_SIZE, STRING_SIZE);&lt;br /&gt;    printf (&amp;quot;\nString : %s\n&amp;quot;, buf);&lt;br /&gt;    i++;&lt;br /&gt;  }while(i&amp;lt;2);&lt;br /&gt;&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-6400665611236729746?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kfTGv1VQbHI:g6BzPkIhDl0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kfTGv1VQbHI:g6BzPkIhDl0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kfTGv1VQbHI:g6BzPkIhDl0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kfTGv1VQbHI:g6BzPkIhDl0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kfTGv1VQbHI:g6BzPkIhDl0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kfTGv1VQbHI:g6BzPkIhDl0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/kfTGv1VQbHI" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-09-09T09:44:14.170+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>How to: Listing all users in a Linux machine</title><link>http://codingfreak.blogspot.com/2009/09/listing-all-users-in-linux-machine.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 08 Sep 2009 02:32:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1459690615204475837</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/NQFQ_RkLGb2183bjk6gRLs5rEXA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NQFQ_RkLGb2183bjk6gRLs5rEXA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/NQFQ_RkLGb2183bjk6gRLs5rEXA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NQFQ_RkLGb2183bjk6gRLs5rEXA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;TO list all the users who can access a Linux machine we have to access the &lt;span style="font-weight: bold;"&gt;/etc/passwd&lt;/span&gt; file, which stores information about all registered users of that machine. But it is not really so easy as told above since the file contains many other fields &amp;amp; machine trust accounts &amp;amp; inbuilt accounts.&lt;br /&gt;&lt;br /&gt;We'll start by &lt;pre name="code" class="cpp"&gt;cat /etc/passwd &lt;/pre&gt;&lt;br /&gt;As we all know that by default all the users created will have their home directories in &lt;span style="font-weight: bold;"&gt;/home&lt;/span&gt; share so we'll modify our command a bit by using &lt;span style="font-weight: bold;"&gt;grep&lt;/span&gt;. Now it'll be&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;cat /etc/passwd | grep "/home"&lt;/pre&gt;&lt;br /&gt;Now we'll get all the user accounts which have their home share in &lt;span style="font-weight: bold;"&gt;/home&lt;/span&gt;.But the only output we need is the list of users &amp;amp; nothing else. So we'll modify our command again&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre name="code" class="cpp"&gt;cat /etc/passwd | grep "/home" |cut -d: -f1&lt;/pre&gt;Now what we have done is that we have piped the output of previous command to another variable "cut"&lt;br /&gt;&lt;br /&gt;What we have done here is we have added cut -d: -f1&lt;br /&gt;-d: means delimiter :&lt;br /&gt;-f1 means display first field of line i.e. username.&lt;br /&gt;&lt;br /&gt;So final command is &lt;pre name="code" class="cpp"&gt;cat /etc/passwd | grep "/home" |cut -d: -f1&lt;/pre&gt;This works until all your users have their home share in &lt;span style="font-weight:bold;"&gt;/home&lt;/span&gt;. If you have defined their home share to some other destination. Modify the above command accordingly.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-1459690615204475837?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NFeJ-zwzkYI:4ZGX7C6jrD4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NFeJ-zwzkYI:4ZGX7C6jrD4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NFeJ-zwzkYI:4ZGX7C6jrD4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=NFeJ-zwzkYI:4ZGX7C6jrD4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NFeJ-zwzkYI:4ZGX7C6jrD4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=NFeJ-zwzkYI:4ZGX7C6jrD4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/NFeJ-zwzkYI" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-04T13:40:56.385+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Signals in Linux - Generating Signals</title><link>http://codingfreak.blogspot.com/2009/09/generating-signals.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 02 Sep 2009 00:45:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-522566313452808278</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iTZb_8r0g1OX744y2uJu1OJlgBo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iTZb_8r0g1OX744y2uJu1OJlgBo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iTZb_8r0g1OX744y2uJu1OJlgBo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iTZb_8r0g1OX744y2uJu1OJlgBo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Besides signals that are generated as a result of a hardware trap or interrupt, your program can explicitly send signals to itself or to another process.&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-weight:bold;"&gt;kill&lt;/span&gt; system call can be used to send any signal to any process group or process.&lt;br /&gt;&lt;blockquote&gt;#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int kill(pid_t pid, int sig);&lt;/blockquote&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man2/kill.2.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;man 2 kill&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;There are restrictions that prevent you from using kill to send signals to any random process. These are intended to prevent antisocial behavior such as arbitrarily killing off processes belonging to another user. In typical use, kill is used to pass signals between parent, child, and sibling processes, and in these situations you normally do have permission to send signals. The only common exception is when you run a setuid program in a child process; if the program changes its real UID as well as its effective UID, you may not have permission to send a signal. The su program does this.&lt;br /&gt;&lt;br /&gt;A process or thread can send a signal to itself with the &lt;span style="font-weight:bold;"&gt;raise&lt;/span&gt; function. The raise function takes just one parameter, a signal number.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;In a single-threaded program it is equivalent to &lt;span style="font-weight:bold;"&gt;kill(getpid(), sig)&lt;/span&gt;. In a multithreaded program it is equivalent to &lt;span style="font-weight:bold;"&gt;pthread_kill(pthread_self(), sig)&lt;/span&gt;. If the signal causes a handler to be called, raise will only return after the signal handler has returned. &lt;br /&gt;&lt;blockquote&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int raise(int sig);&lt;/blockquote&gt;&lt;br /&gt;For more information checkout: &lt;a href="http://www.kernel.org/doc/man-pages/online/pages/man3/raise.3.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;man 3 raise&lt;/span&gt;&lt;/a&gt; &lt;pre name="code" class="cpp"&gt;#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;static volatile sig_atomic_t doneflag = 10;&lt;br /&gt;&lt;br /&gt;static void setdoneflag(int signo) {&lt;br /&gt;    printf(&amp;quot;\nIn SignalHandler - setdoneflag\n&amp;quot;);&lt;br /&gt;    doneflag=0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main (void) {&lt;br /&gt;&lt;br /&gt;    signal(SIGINT, setdoneflag);&lt;br /&gt;&lt;br /&gt;    while(doneflag--)&lt;br /&gt;    {&lt;br /&gt;        printf(&amp;quot;In While loop - %d\n&amp;quot;,doneflag);&lt;br /&gt;        if(doneflag==5)&lt;br /&gt;        raise(2);&lt;br /&gt;        else&lt;br /&gt;        sleep(1);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    printf(&amp;quot;Program terminating ...\n&amp;quot;);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-522566313452808278?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Er5CrKWasIw:QPk8v9-LNAk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Er5CrKWasIw:QPk8v9-LNAk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Er5CrKWasIw:QPk8v9-LNAk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Er5CrKWasIw:QPk8v9-LNAk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Er5CrKWasIw:QPk8v9-LNAk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Er5CrKWasIw:QPk8v9-LNAk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/Er5CrKWasIw" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-04T13:48:08.268+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Implementation of Singly Linked List</title><link>http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html</link><category>C</category><category>DataStructures</category><author>noreply@blogger.com (Ajith)</author><pubDate>Wed, 26 Aug 2009 07:56:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2564135340772187317</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7SokDX7y1LaNz7GKkHHCfgyo9wU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7SokDX7y1LaNz7GKkHHCfgyo9wU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7SokDX7y1LaNz7GKkHHCfgyo9wU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7SokDX7y1LaNz7GKkHHCfgyo9wU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;A Linked list is a chain of structs or records called &lt;span style="font-weight:bold;"&gt;Nodes&lt;/span&gt;. Each node has at least two members, one of which points to the next Node in the list and the other holds the data. These are defined as Single Linked Lists because they can only point to the next Node in the list but not to the previous.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt; struct Node&lt;br /&gt; {&lt;br /&gt;   int Data;&lt;br /&gt;   struct Node *Next;&lt;br /&gt; }*Head; &lt;/pre&gt;We use above structure for a Node in our example. Variable &lt;span style="font-weight:bold;"&gt;Data&lt;/span&gt; holds the data in the Node while pointer of type struct Node &lt;span style="font-weight:bold;"&gt;Next&lt;/span&gt; holds the address to the next Node in the list. &lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt; is a pointer of type struct Node which acts as the Head to the list. &lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Initially we set '&lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt;' as &lt;span style="font-weight:bold;"&gt;NULL&lt;/span&gt; which means list is empty.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt;//Set HEAD as NULL&lt;br /&gt;Head=NULL;&lt;/pre&gt;Let us see Insertion Functions .. &lt;span style="font-weight:bold;"&gt;addBeg&lt;/span&gt;, &lt;span style="font-weight:bold;"&gt;addEnd&lt;/span&gt;, &lt;span style="font-weight:bold;"&gt;addAt&lt;/span&gt;.&lt;br /&gt;&lt;pre name="code" class="cpp"&gt; // Adding a Node at the Beginning of the List&lt;br /&gt;&lt;br /&gt; void addBeg(int num)&lt;br /&gt; {&lt;br /&gt;    struct Node *temp;&lt;br /&gt;&lt;br /&gt;    temp=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;    temp-&gt;Data = num;&lt;br /&gt;&lt;br /&gt;    if (Head == NULL)&lt;br /&gt;    {&lt;br /&gt;       //List is Empty&lt;br /&gt;       Head=temp;&lt;br /&gt;       Head-&gt;Next=NULL;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       temp-&gt;Next=Head;&lt;br /&gt;       Head=temp;&lt;br /&gt;    }&lt;br /&gt; }&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_MQa_rKnACRw/S9NRS5daK_I/AAAAAAAAB44/F2Vp5GR0Tho/s1600/01.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 216px;" src="http://3.bp.blogspot.com/_MQa_rKnACRw/S9NRS5daK_I/AAAAAAAAB44/F2Vp5GR0Tho/s320/01.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5463800158034209778" /&gt;&lt;/a&gt;&lt;br /&gt;We create '&lt;span style="font-weight:bold;"&gt;temp&lt;/span&gt;' node and save the &lt;span style="font-weight:bold;"&gt;Data&lt;/span&gt; part. &lt;br /&gt;&lt;br /&gt;If &lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt; is &lt;span style="font-weight:bold;"&gt;NULL&lt;/span&gt; it means list is empty. So we set &lt;span style="font-weight:bold;"&gt;temp&lt;/span&gt; node as the &lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt; of the list and set the &lt;span style="font-weight:bold;"&gt;Next&lt;/span&gt; as &lt;span style="font-weight:bold;"&gt;NULL&lt;/span&gt;. Else we set the &lt;span style="font-weight:bold;"&gt;Next&lt;/span&gt; in &lt;span style="font-weight:bold;"&gt;temp&lt;/span&gt; node as &lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt; and reassign &lt;span style="font-weight:bold;"&gt;Head&lt;/span&gt; with &lt;span style="font-weight:bold;"&gt;temp&lt;/span&gt;.&lt;pre name="code" class="cpp"&gt;//Adding a Node at the end of the list&lt;br /&gt;&lt;br /&gt; void addEnd(int num)&lt;br /&gt; {&lt;br /&gt;    struct Node *temp1, *temp2;&lt;br /&gt;&lt;br /&gt;    temp1=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;    temp1-&gt;Data=num;&lt;br /&gt;&lt;br /&gt;    // Copying the Head location into another node.&lt;br /&gt;    temp2=Head;&lt;br /&gt;&lt;br /&gt;    if(Head == NULL)&lt;br /&gt;    {&lt;br /&gt;       // If List is empty we create First Node.&lt;br /&gt;       Head=temp1;&lt;br /&gt;       Head-&gt;Next=NULL;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;       // Traverse down to end of the list.&lt;br /&gt;       while(temp2-&gt;Next != NULL)&lt;br /&gt;       temp2=temp2-&gt;Next;&lt;br /&gt;&lt;br /&gt;       // Append at the end of the list.&lt;br /&gt;       temp1-&gt;Next=NULL;&lt;br /&gt;       temp2-&gt;Next=temp1;&lt;br /&gt;    }&lt;br /&gt; }&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_MQa_rKnACRw/S9NYLGRW6jI/AAAAAAAAB5A/hySIJz0v1ZQ/s1600/02.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 250px;" src="http://4.bp.blogspot.com/_MQa_rKnACRw/S9NYLGRW6jI/AAAAAAAAB5A/hySIJz0v1ZQ/s320/02.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5463807720615766578" /&gt;&lt;/a&gt;&lt;pre name="code" class="cpp"&gt; // Adding a new Node at specified position&lt;br /&gt;&lt;br /&gt; void addAt(int num, int loc)&lt;br /&gt; {&lt;br /&gt;    int i;&lt;br /&gt;    struct Node *temp, *prev_ptr, *cur_ptr;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;    if(loc &gt; (length()+1) || loc &amp;lt;= 0)&lt;br /&gt;    {&lt;br /&gt;       printf("\nInsertion at given location is not possible\n ");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        // If the location is starting of the list&lt;br /&gt;        if (loc == 1)&lt;br /&gt;        {&lt;br /&gt;            addBeg(num);&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            for(i=1;i&amp;lt;loc;i++)&lt;br /&gt;            {&lt;br /&gt;                prev_ptr=cur_ptr;   &lt;br /&gt;                cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            temp=(struct Node *)malloc(sizeof(struct Node));&lt;br /&gt;            temp-&gt;Data=num;&lt;br /&gt;&lt;br /&gt;            prev_ptr-&gt;Next=temp;&lt;br /&gt;            temp-&gt;Next=cur_ptr;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; // Counting number of elements in the List&lt;br /&gt;&lt;br /&gt; int length()&lt;br /&gt; {&lt;br /&gt;    struct Node *cur_ptr;&lt;br /&gt;    int count=0;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;    while(cur_ptr != NULL)&lt;br /&gt;    {&lt;br /&gt;       cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;       count++;&lt;br /&gt;    }&lt;br /&gt;    return(count);&lt;br /&gt; }&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NcDunFmFI/AAAAAAAAB5I/NM5Czfo1ZbE/s1600/03.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 260px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NcDunFmFI/AAAAAAAAB5I/NM5Czfo1ZbE/s320/03.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5463811992051882066" /&gt;&lt;/a&gt;&lt;br /&gt;Now we will see how to delete a node from the list depending upon the data in the Node.&lt;pre name="code" class="cpp"&gt; // Deleting a node from List depending upon the data in the node.&lt;br /&gt;&lt;br /&gt; int delNodeData(int num)&lt;br /&gt; {&lt;br /&gt;    struct Node *prev_ptr, *cur_ptr;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;    while(cur_ptr != NULL)&lt;br /&gt;    {&lt;br /&gt;       if(cur_ptr-&gt;Data == num)&lt;br /&gt;       {&lt;br /&gt;          if(cur_ptr==Head)&lt;br /&gt;          {&lt;br /&gt;             Head=cur_ptr-&gt;Next;&lt;br /&gt;             free(cur_ptr);&lt;br /&gt;             return 0;&lt;br /&gt;          }&lt;br /&gt;          else&lt;br /&gt;          {&lt;br /&gt;             prev_ptr-&gt;Next=cur_ptr-&gt;Next;&lt;br /&gt;             free(cur_ptr);&lt;br /&gt;             return 0;&lt;br /&gt;          }&lt;br /&gt;       }&lt;br /&gt;       else&lt;br /&gt;       {&lt;br /&gt;          prev_ptr=cur_ptr;&lt;br /&gt;          cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    printf("\nElement %d is not found in the List", num);&lt;br /&gt;    return 1;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt; If the value to be deleted is the head of the list.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NmUtjJFEI/AAAAAAAAB5Y/GniHOl2qGns/s1600/04.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 122px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NmUtjJFEI/AAAAAAAAB5Y/GniHOl2qGns/s320/04.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5463823278940951618" /&gt;&lt;/a&gt;&lt;br /&gt;If the value to be deleted is not the head of the list.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NmEnTNBFI/AAAAAAAAB5Q/U2LJ384KpzE/s1600/05.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 226px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/S9NmEnTNBFI/AAAAAAAAB5Q/U2LJ384KpzE/s320/05.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5463823002385581138" /&gt;&lt;/a&gt;&lt;br /&gt;Similarly if we want to delete a node based on its location in the list.&lt;pre name="code" class="cpp"&gt; // Deleting a node from List depending upon the location in the list.&lt;br /&gt;&lt;br /&gt; int delNodeLoc(int loc)&lt;br /&gt; {&lt;br /&gt;    struct Node *prev_ptr, *cur_ptr;&lt;br /&gt;    int i;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;    if(loc &gt; (length()) || loc &lt;= 0)&lt;br /&gt;    {&lt;br /&gt;        printf("\nDeletion of Node at given location is not possible\n ");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        // If the location is starting of the list&lt;br /&gt;        if (loc == 1)&lt;br /&gt;        {&lt;br /&gt;            Head=cur_ptr-&gt;Next;&lt;br /&gt;            free(cur_ptr);&lt;br /&gt;            return 0;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            for(i=1;i&amp;lt;loc;i++)&lt;br /&gt;            {&lt;br /&gt;                prev_ptr=cur_ptr;&lt;br /&gt;                cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            prev_ptr-&gt;Next=cur_ptr-&gt;Next;&lt;br /&gt;            free(cur_ptr);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return 1;&lt;br /&gt; }&lt;/pre&gt;Displaying a list.&lt;pre name="code" class="cpp"&gt; // Displaying list contents&lt;br /&gt;&lt;br /&gt; void display()&lt;br /&gt; {&lt;br /&gt;    struct Node *cur_ptr;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;&lt;br /&gt;    if(cur_ptr==NULL)&lt;br /&gt;    {&lt;br /&gt;       printf("\nList is Empty");&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        printf("Elements in the List: ");&lt;br /&gt;        //traverse the entire linked list&lt;br /&gt;        while(cur_ptr!=NULL)&lt;br /&gt;        {&lt;br /&gt;            printf(" -&gt; %d ",cur_ptr-&gt;Data);&lt;br /&gt;            cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;        }&lt;br /&gt;        printf("\n");&lt;br /&gt;    }&lt;br /&gt; }&lt;/pre&gt;Reversing a list.&lt;pre name="code" class="cpp"&gt; //Reversesing a Linked List&lt;br /&gt;&lt;br /&gt; void reverse()&lt;br /&gt; {&lt;br /&gt;    struct Node *prev_ptr, *cur_ptr, *temp;&lt;br /&gt;&lt;br /&gt;    cur_ptr=Head;&lt;br /&gt;    prev_ptr=NULL;&lt;br /&gt;&lt;br /&gt;    while(cur_ptr != NULL)&lt;br /&gt;    {&lt;br /&gt;       temp=prev_ptr;&lt;br /&gt;       prev_ptr=cur_ptr;&lt;br /&gt;&lt;br /&gt;       cur_ptr=cur_ptr-&gt;Next;&lt;br /&gt;       prev_ptr-&gt;Next=temp;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    Head=prev_ptr;&lt;br /&gt; }&lt;/pre&gt;For complete source code - Checkout &lt;a href="http://codingfreak.blogspot.com/2010/04/singly-linked-list-in-c.html" target="_blank"&gt;Singly Linked List in C&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-2564135340772187317?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=ciMSW49Fvyo:5THfXi22b4c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=ciMSW49Fvyo:5THfXi22b4c:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=ciMSW49Fvyo:5THfXi22b4c:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=ciMSW49Fvyo:5THfXi22b4c:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=ciMSW49Fvyo:5THfXi22b4c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=ciMSW49Fvyo:5THfXi22b4c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/ciMSW49Fvyo" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2010-07-03T08:02:23.889+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_MQa_rKnACRw/S9NRS5daK_I/AAAAAAAAB44/F2Vp5GR0Tho/s72-c/01.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">68</thr:total></item><item><title>Signals in Linux - Standard Signals</title><link>http://codingfreak.blogspot.com/2009/08/standard-signals-in-linux.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Tue, 25 Aug 2009 03:54:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2480889089069929924</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/D3RGVyWCE5hCi0rj7GN2EA3XTwM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/D3RGVyWCE5hCi0rj7GN2EA3XTwM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/D3RGVyWCE5hCi0rj7GN2EA3XTwM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/D3RGVyWCE5hCi0rj7GN2EA3XTwM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Every signal has a unique signal name, an abbreviation that begins with &lt;span style="font-weight:bold;"&gt;SIG&lt;/span&gt; (SIGINT for interrupt signal, for example). Each signal name is a macro which stands for a positive integer - the signal number for that kind of signal. Your programs should never make assumptions about the numeric code for a particular kind of signal, but rather refer to them always by the names defined. This is because the number for a given kind of signal can vary from system to system, but the meanings of the names are standardized and fairly uniform. &lt;br /&gt;&lt;br /&gt;The signal names are defined in &lt;span style="font-weight:bold;"&gt;signal.h&lt;/span&gt; (&lt;span style="font-weight:bold;"&gt;/usr/include/bits/signum.h&lt;/span&gt;), which must be included by any C program that uses signals.&lt;br /&gt;&lt;br /&gt;Several signal numbers are architecture-dependent, as indicated in the "Value" column.  (Where three values are given, the first one is usually valid for alpha and sparc, the middle one for ix86, ia64, ppc, s390, arm and sh, and the last one for mips. A &lt;span style="font-weight: bold;"&gt;-&lt;/span&gt; denotes that a signal is absent on the corresponding architecture.)&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_MQa_rKnACRw/Srn_RqHMjZI/AAAAAAAABxI/WKtUA-yWEKA/s1600-h/01.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 271px;" src="http://2.bp.blogspot.com/_MQa_rKnACRw/Srn_RqHMjZI/AAAAAAAABxI/WKtUA-yWEKA/s400/01.JPG" alt="" id="BLOGGER_PHOTO_ID_5384615508325469586" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;The signals &lt;span style="font-weight:bold;"&gt;SIGKILL&lt;/span&gt; and &lt;span style="font-weight:bold;"&gt;SIGSTOP&lt;/span&gt; cannot be caught, blocked, or ignored.&lt;br /&gt;&lt;br /&gt;Next the signals not in the POSIX.1-1990 standard but described in SUSv2 and POSIX.1-2001.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/Srn_Lf-0LpI/AAAAAAAABxA/mztyZh1lLIY/s1600-h/02.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 143px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/Srn_Lf-0LpI/AAAAAAAABxA/mztyZh1lLIY/s400/02.JPG" alt="" id="BLOGGER_PHOTO_ID_5384615402526748306" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Up to and including Linux 2.2, the default behavior for SIGSYS, SIGXCPU, SIGXFSZ, and (on architectures other than SPARC and MIPS) SIGBUS was to terminate the process (without a core dump). Linux 2.4 conforms to the POSIX.1-2001 requirements for these signals, terminating the process with a core dump.&lt;br /&gt;&lt;br /&gt;Next various other signals.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_MQa_rKnACRw/Srn_H69WY9I/AAAAAAAABw4/DnpvtU6qTto/s1600-h/03.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 144px;" src="http://1.bp.blogspot.com/_MQa_rKnACRw/Srn_H69WY9I/AAAAAAAABw4/DnpvtU6qTto/s400/03.JPG" alt="" id="BLOGGER_PHOTO_ID_5384615341048882130" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;For detailed information about the side-effects and reasons causing these signals checout &lt;a href="http://www.gnu.org/software/libc/manual/html_node/Standard-Signals.html#Standard-Signals" target="_blank"&gt;libc manual&lt;/a&gt;.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-2480889089069929924?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=coUpD-rx5BQ:yEaq90-exeY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=coUpD-rx5BQ:yEaq90-exeY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=coUpD-rx5BQ:yEaq90-exeY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=coUpD-rx5BQ:yEaq90-exeY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=coUpD-rx5BQ:yEaq90-exeY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=coUpD-rx5BQ:yEaq90-exeY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/coUpD-rx5BQ" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-11-04T13:49:35.511+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_MQa_rKnACRw/Srn_RqHMjZI/AAAAAAAABxI/WKtUA-yWEKA/s72-c/01.JPG" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Signals in Linux - Basics</title><link>http://codingfreak.blogspot.com/2009/08/all-about-signals-in-linux-basics.html</link><category>Signals</category><category>Linux</category><author>noreply@blogger.com (Ajith)</author><pubDate>Sun, 23 Aug 2009 02:50:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6646400580554344638</guid><description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/bb1IW-JaIxxrmVOilLibSECo1X8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bb1IW-JaIxxrmVOilLibSECo1X8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/bb1IW-JaIxxrmVOilLibSECo1X8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bb1IW-JaIxxrmVOilLibSECo1X8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;What is a Signal ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A signal is a software interrupt delivered to notify a process or thread of a particular event. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asynchronous events, such as disconnection of a phone line.&lt;br /&gt;&lt;br /&gt;The lifetime of a signal is the interval between its generation and its delivery. A signal that has been generated but not yet delivered is said to be pending. There may be considerable time between signal generation and signal delivery. The process must be running on a processor at the time of signal delivery.&lt;br /&gt;&lt;blockquote&gt;Many computer science researchers compare signals with hardware interrupts, which occur when a hardware subsystem, such as a disk I/O (input/output) interface, generates an interrupt to a processor when the I/O completes. This event in turn causes the processor to enter an interrupt handler, so subsequent processing can be done in the operating system based on the source and cause of the interrupt. When a signal is sent to a process or thread, a signal handler may be entered (depending on the current disposition of the signal), which is similar to the system entering an interrupt handler as the result of receiving an interrupt.&lt;/blockquote&gt; &lt;span class="fullpost"&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;What causes a Signal ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let us see some of the events that can cause (or generate, or raise) a signal:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;A program error&lt;/span&gt; such as dividing by zero or issuing an address outside the valid range.&lt;/li&gt;&lt;li&gt;A user request to interrupt or terminate the program. Most environments are set up to let a user suspend the program by typing &lt;span style="font-weight: bold;"&gt;Ctrl-z&lt;/span&gt;, or terminate it with &lt;span style="font-weight: bold;"&gt;Ctrl-c&lt;/span&gt;. Whatever key sequence is used, the operating system sends the proper signal to interrupt the process.&lt;/li&gt;&lt;li&gt;The termination of a child process.&lt;/li&gt;&lt;li&gt;Expiration of a timer or alarm.&lt;/li&gt;&lt;li&gt;A call to &lt;span style="font-weight: bold;"&gt;kill&lt;/span&gt; or &lt;span style="font-weight: bold;"&gt;raise&lt;/span&gt; system calls by the same process.&lt;/li&gt;&lt;li&gt;A call to &lt;span style="font-weight: bold;"&gt;kill&lt;/span&gt; from another process. Signals are a limited but useful form of interprocess communication.&lt;/li&gt;&lt;li&gt;An attempt to perform an I/O operation that cannot be done. Examples are reading from a pipe that has no writer, and reading or writing to a terminal in certain situations.&lt;/li&gt;&lt;/ul&gt; Each of these kinds of events (excepting explicit calls to &lt;span style="font-weight: bold;"&gt;kill&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;raise&lt;/span&gt;) generates its own particular kind of signal.&lt;br /&gt;&lt;br /&gt;Signals may be generated synchronously or asynchronously. A synchronous signal pertains to a specific action in the program, and is delivered (unless blocked) during that action. Most errors generate signals synchronously, and so do explicit requests by a process to generate a signal for that same process. On some machines, certain kinds of hardware errors (usually floating-point exceptions) are not reported completely synchronously, but may arrive a few instructions later.&lt;br /&gt;&lt;br /&gt;Asynchronous signals are generated by events outside the control of the process that receives them. These signals arrive at unpredictable times during execution. External events generate signals asynchronously, and so do explicit requests that apply to some other process. One obvious example would be the sending of a signal to a process from another process or thread via a kill system call. Asynchronous signals are also aptly referred to as interrupts.&lt;br /&gt;&lt;br /&gt;A given type of signal is either typically synchronous or typically asynchronous. For example, signals for errors are typically synchronous because errors generate signals synchronously. But any type of signal can be generated synchronously or asynchronously with an explicit request.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How Signals Are Delivered ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When a signal is generated, it becomes pending. Normally it remains pending for just a short period of time and then is delivered to the process that was signaled. However, if that kind of signal is currently blocked, it may remain pending indefinitely—until signals of that kind are unblocked. Once unblocked, it will be delivered immediately.&lt;br /&gt;&lt;br /&gt;When the signal is delivered, whether right away or after a long delay, the specified action for that signal is taken. For certain signals, such as SIGKILL and SIGSTOP, the action is fixed, but for most signals, the program has a choice. Possible default dispositions are&lt;br /&gt;&lt;pre&gt;&lt;span style="font-weight: bold;"&gt;Term &lt;/span&gt;  Default action is to terminate the process.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Ign&lt;/span&gt;    Default action is to ignore the signal.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Core&lt;/span&gt;   Default action is to terminate the process&lt;br /&gt;  and dump core.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Stop&lt;/span&gt;   Default action is to stop the process.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Cont&lt;/span&gt;   Default action is to continue the process&lt;br /&gt;  if it is currently stopped.&lt;/pre&gt;The program can also specify its own way of handling the signals (signal handlers). We sometimes say that a handler catches the signal. While the handler is running, that particular signal is normally blocked.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:110;" &gt;References:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1. &lt;a href="http://www.gnu.org/software/libc/manual/html_node/index.html"&gt;Libc Manual&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4070434216759110691-6646400580554344638?l=codingfreak.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=z3I-wBYUX3Y:qxYQZ1Ms3tA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=z3I-wBYUX3Y:qxYQZ1Ms3tA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=z3I-wBYUX3Y:qxYQZ1Ms3tA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=z3I-wBYUX3Y:qxYQZ1Ms3tA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=z3I-wBYUX3Y:qxYQZ1Ms3tA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=z3I-wBYUX3Y:qxYQZ1Ms3tA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/z3I-wBYUX3Y" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2009-10-05T10:51:10.247+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item></channel></rss>

