<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Programming in Linux</title>
	<atom:link href="http://linuxprograms.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://linuxprograms.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 03 Oct 2011 11:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='linuxprograms.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Programming in Linux</title>
		<link>http://linuxprograms.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://linuxprograms.wordpress.com/osd.xml" title="Programming in Linux" />
	<atom:link rel='hub' href='http://linuxprograms.wordpress.com/?pushpress=hub'/>
		<item>
		<title>C: Variable Argument List Access</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 15:06:35 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Variable Arguments]]></category>
		<category><![CDATA[va_arg]]></category>
		<category><![CDATA[va_list]]></category>
		<category><![CDATA[va_start]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/</guid>
		<description><![CDATA[Here, I explained how we can utilize the operator ellipsis (&#8230;) to pass variable number of arguments to a function. I have utilised there the concept of pointers to access the variable arguments. The standard C Library provides support to access these arguments. Use for this support All you need is to know the last [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=10&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://linuxprograms.wordpress.com/2008/03/07/c-ellipsis-operator-printf/">Here</a>, I explained how we can utilize the operator ellipsis (&#8230;) to pass variable number of arguments to a function. I have utilised there the concept of pointers to access the variable  arguments. The standard C Library provides support to access these arguments. Use  for this support<br />
All you need is to know the last argument before the ellipsis operator(At least one argument is must to use variable arguments), let&#8217;s call it larg</p>
<p>suppose</p>
<pre>    fun(type var1,type var2,...)</pre>
<p>is a function, then larg corresponds to var2</p>
<p>Now we need to declare the list using va_list<br />
i.e.,</p>
<pre>    va_list al</pre>
<p>now initialize it using va_start</p>
<pre>    va_start(al,larg);</pre>
<p>Now  to access each argument, we must know the expected type of the argument</p>
<pre>     type var_name = va_arg(al,type);</pre>
<p>When we have accessed all the variable arguments, we need to clean up</p>
<pre>     va_end(al);</pre>
<p>Using standard library, we can easily access the variable arguments</p>
<p>Take a simple example</p>
<pre>#include &lt;stdarg.h&gt;
int print(int num_count,...)
/*num_count contains the number of integers passed*/
{
  int i;
  va_list al;
  va_start(al,num_count);
  for(i=1;i&lt;=num_count;i++){
     int val=va_arg(al,int);
     printf("%d ",val);
  }

}

int print_str(const char *str,...)
/*str has the number of strings passed*/
{
  int i;
  va_list al;
  int num_count=atoi(str);
  va_start(al,str);
  int *num=&amp;num_count;
  for(i=1;i&lt;=num_count;i++){
     char *str=va_arg(al,char *);
     printf("%s ",str);
  }

}

int main()
{
   print(3,2,3,4);
   print_str("3","Hi","All","!!!");
}</pre>
<p><strong>OUTPUT</strong></p>
<p>2 3 4 Hi All !!!</p>
<p>For more updates, check <a href="http://programming.johnsamuel.in/">Programming Insights</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=10&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>C: Printing data types using printf : short, wchar_t, long double</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/c-printing-data-types-using-printf-short-wchar_t-long-double/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/c-printing-data-types-using-printf-short-wchar_t-long-double/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 14:54:29 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[printf()]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/03/07/c-printing-data-types-using-printf-short-wchar_t-long-double/</guid>
		<description><![CDATA[For more updates, check Programming Insights. printf is the most commonly used function while programming (for big developers at least in the designing stage). But we mostly come across the problem: &#8220;Which conversion character for char, short, pointers&#8230; and their unsigned counterparts?&#8221; Here is a program which deals with printing of all the different data [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=7&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.<br />
printf is the most commonly used function while programming (for big developers at least in the designing stage). But we mostly come across the problem: &#8220;Which conversion character for char, short, pointers&#8230; and their unsigned counterparts?&#8221; Here is a program which deals with printing of all the different data types.</p>
<pre>#include &lt;stdio.h&gt;

/*A program to see how diffent data types can be printed using printf
*See the conversion characters with % for the various data types
*/
int main()
{
    //Character strings
   char *message="Printing different data types";
   wchar_t *wmessage=L"Wide Character string";

   //Characters
   char ca='A';
   wchar_t wca=L'A';

   //Integer Data types
   unsigned short usa=65535; short ssb=-32768;
   unsigned int usi=4294967295; int ssi=-2147483648;
   unsigned long long int ulli=4294967295L;long long int lli=-2147483647L;

   //Floating point Data types
   float fa=1e+37,fb=1e-37;
   double da=1e+37,db=1e-37;
   long double lda=1e+37L,ldb=1e-37L;

   //Pointers
   int *p=&amp;ssi;

   //Character strings
   printf("%s\n",message);
   printf("%ls\n\n",wmessage);

   /<em>/Character strings</em>
   printf("%c\n",ca);
   printf("%lc\n\n",wca);

   printf("%hu %hi\n",usa,ssb); //short
   printf("%hx %hx\n\n",usa,ssb);

   printf("%u %i\n",usi,ssi); //integer/long integers
   printf("%x %x\n\n",usi,ssi);

   printf("%llu %lli\n",ulli,lli); //long long integers
   printf("%llx %llx\n\n",ulli,lli);

   printf("%f %f\n",fa,fb); //float

   printf("%f %f\n",da,db); //double
   printf("%e %e\n\n",da,db);

   printf("%llf %llf\n",lda,ldb); //long double
   printf("%lle %lle\n\n",lda,ldb);

   printf("%p\n\n",p); //pointer of any data type

}</pre>
<p>Here&#8217;s the output</p>
<div>
<pre><a title="printf" rel="attachment wp-att-9" href="http://linuxprograms.wordpress.com/2008/03/07/c-printing-data-types-using-printf-short-wchar_t-long-double/printf/"><img src="http://linuxprograms.files.wordpress.com/2008/03/printf1.jpg?w=468" alt="printf" /></a>

For more updates, check <a href="http://programming.johnsamuel.in/">Programming Insights</a>.</pre>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=7&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/c-printing-data-types-using-printf-short-wchar_t-long-double/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>

		<media:content url="http://linuxprograms.files.wordpress.com/2008/03/printf1.jpg" medium="image">
			<media:title type="html">printf</media:title>
		</media:content>
	</item>
		<item>
		<title>C:  #define, #ifdef, #ifndef, #else</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/c-define-ifdef-ifndef-else/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/c-define-ifdef-ifndef-else/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 14:47:37 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[#define]]></category>
		<category><![CDATA[#else]]></category>
		<category><![CDATA[#ifdef]]></category>
		<category><![CDATA[#ifndef]]></category>
		<category><![CDATA[C Preprocessor]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/?p=6</guid>
		<description><![CDATA[#define is a C preprocessor. There are other ways of making the best usage of #define for writing efficient programs , for the purpose of testing and debugging, for deciding the course of action of the programs When we first learn about #define, the commonly said example is define some commonly used values like PI, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=6&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>#define is a C preprocessor. There are other ways of making the best usage of  #define for writing efficient programs , for the purpose of testing and debugging, for deciding the course of action of the programs<br />
When we first learn about #define, the commonly said example is define some commonly used values like PI, NAME_LEN ,&#8230; But there are other uses of #define.</p>
<p>Let me show some aspects of #define<br />
The general syntax of #define</p>
<pre>    #define identifier(identifier,..., indentifier) token_sequence</pre>
<p>1. It is not necessary that all the identifiers used in token sequence is already defined</p>
<pre>#define A B+20
#define B 10</pre>
<p>As you can see that B was not defined when A is being defined. So the only condition is that we need to define it somewhere.</p>
<p>2. Using gcc, you can define macros and decide the course of your programming. For example, if I wish to perform debugging. I will define  __DEBUG__ through gcc.To define macros using gcc, compile using the D option of gcc</p>
<pre>gcc -Dmacro[=defn]...]</pre>
<p>The following code snippet depicts this</p>
<pre>#include &lt;stdio.h&gt;

/*This program shows how to make the use of #define to define the course

*of programming

*/

#define A B+20 /<em>B is not defined here, but will be defined later</em>/

#define B 10

int main()

{

#ifdef __DEBUG__
    printf("__DEBUG__ DEFINED\n");

    printf("%d\n",A);

#else

   printf("__DEBUG__ not defined\n");

   printf("%d\n",B);

#endif

return 0;
}</pre>
<p>If I compile</p>
<pre>$gcc test.c</pre>
<p><span style="font-weight:bold;">Output:</span></p>
<p>__DEBUG__ not defined</p>
<p>10</p>
<p>If I compile</p>
<pre>$gcc -D __DEBUG__ test.c</pre>
<p><span style="font-weight:bold;">Output:</span></p>
<pre>__DEBUG__ defined

30</pre>
<p>Thus we can see how the C Preprocessors also help in deciding the course of action of a program. The above is also called <span style="font-weight:bold;">Conditional Compilation</span></p>
<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=6&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/c-define-ifdef-ifndef-else/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>C: Ellipsis operator (&#8230;) : printf</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/c-ellipsis-operator-printf/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/c-ellipsis-operator-printf/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 12:44:44 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C Operator]]></category>
		<category><![CDATA[printf()]]></category>
		<category><![CDATA[Variable Arguments]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/03/07/c-ellipsis-operator-printf/</guid>
		<description><![CDATA[For more updates, check Programming Insights. Ever imagined how printf works, even though we are able to pass a number of arguments to it. If we design a function which takes two arguments and pass three parameters, we are bound to get this error &#8220;too many arguments to function&#8221;i.e., suppose we have a function int [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=5&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;">
<pre>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</pre>
</div>
<div style="text-align:justify;">Ever imagined how printf works, even though we are able to pass a number of arguments to it. If we design a function which takes two arguments and pass  three parameters, we are bound to get this error &#8220;too many arguments to function&#8221;i.e., suppose we have a function</div>
<div style="text-align:justify;">
<pre>    int fun2(int a, int b)</pre>
<p>and we call the function</p>
<pre>    fun2(2,3,4)</pre>
<p>we are sure to get the above error. So the question is how printf / scanf works with variable number of arguments? This is because C has a  feature called ellipsis (&#8230;) by which you are able to pass variable number of arguments?</p>
<p>So the prototype of printf is</p>
<pre>    int printf(const char *str,...)</pre>
<p>But the next question is how then can we access the arguments in the function. This can be done by the power of pointers. Let&#8217;s take a pointer which points to the last argument before &#8230;<br />
and depending on the next arguments of what we expect, we increment the pointer and increment it accordingly</p>
<p>Below is a simple code which shows how this can be done</p>
<pre>int print(const char *str,...)

/*str has the number of integers passed*/

{

        int i;

        int num_count=atoi(str);

        int *num=(int *)&amp;str;

        for(i=1;i&lt;=num_count;i++)

                printf("%d ",*(num+i));}

int print_num(int num_count,...)

/*num_count contains the number of integers passed*/

{

        int i;

        int *num=&amp;num_count;

        for(i=1;i&lt;=num_count;i++)

                printf("%d ",*(num+i));

}

int main()

{

        print_num(3,2,3,4);

        print("3",2,3,4);

}

For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</pre>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=5&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/c-ellipsis-operator-printf/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>C: Structure Initialization (Advanced)</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 12:42:37 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Structure]]></category>
		<category><![CDATA[Structure Initialization]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/</guid>
		<description><![CDATA[Previous C: Structure Initilaization There is one more way by which we can initialize structures. These way utilizes the benefits of both the ways of structure initialization described here. That is we are able to initialize the elements of the structure using the set notation and also we need not remember the order of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=4&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;">
<div style="text-align:left;"><a href="http://linuxprograms.wordpress.com/2008/03/07/3/"><span style="font-family:arial;">Previous</span><br />
<span style="font-family:arial;">C: Structure Initilaization</span></a></div>
<p><span style="font-family:arial;"><br />
There is one more way by which we can initialize structures. These way utilizes the benefits of both the ways of structure initialization described <a href="http://linuxprograms.wordpress.com/2008/03/07/3/">here</a>. That is we are able to initialize the elements of the structure using the set notation and also we need not remember the order of the elements of the structure.</span><br />
<span style="font-family:arial;">Suppose consider the  structure &#8216;</span><span style="font-style:italic;font-family:arial;">student</span><span style="font-family:arial;">&#8216; we already used </span></p>
<pre>struct student {

     int roll;

     int class;

     char name[50];

};</pre>
<p>Now let&#8217;s declare a variable st3 and initialize using the third method</p>
<pre>student st3={

           .name = "Mark", //Notice the equal to and the comma

           .class  = 10,

           .roll  = 1038

};</pre>
<p><span style="font-family:arial;">As you can see the way by which the variable st3 has been initialized. It has used the set notation and also see the order is not the same as in the case of the structure &#8216;<span style="font-style:italic;">student</span>&#8216;</span></p>
</div>
<p><span style="font-family:arial;"><br />
The following program demonstrates this. Note the different ways by which the array of structures has been initialized<br />
</span></p>
<pre>/*

*Different Ways of structure initialization

*/#include &lt;<span class="blsp-spelling-error"> </span>stdio.h&gt;

#include &lt;<span class="blsp-spelling-error"> </span>string.h&gt;

#define NAME_LEN 25

<span class="blsp-spelling-error">typedef</span> unsigned short age_t;

<span class="blsp-spelling-error">typedef</span> unsigned int roll_t;

<span class="blsp-spelling-error">typedef</span> <span class="blsp-spelling-error">struct</span> student{

    char name[NAME_LEN];

    roll_t <span class="blsp-spelling-error">rno</span>;

    age_t age;

}student;

int main()

{

/* Method 3a: Just like the Method 2a, but here you do not

* need to know order of the elements in the declaration

*/

student st3={

.name = "Mark",//Notice the equal to and comma

.age  = 23,
.rno  = 1038
};

printf("%s %hi %u\n\n",st3.name,st3.age,st3.rno);

/*

* Method 3b: For Initializing an array of structures

*/

student st4[]={
{

.name = "Neil",

.age  = 23,

.rno  = 1039      },

{

.name = "Peter",

.age  = 23,

.rno  = 1040

}
};

<span class="blsp-spelling-error">printf</span>("%s %hi %u\n%s %hi %u\n\n",st4[0].name,st4[0].age,

st4[0].<span class="blsp-spelling-error">rno</span>, st4[1].name,st4[1].age,st4[1].rno);

/*

* Method 3c : Change the order of initialization of the

* elements of the array. Normally as seen in Method 2b,

* the 0<span class="blsp-spelling-error">th</span> array element is initialized  then 1st, then

* 2<span class="blsp-spelling-error">nd</span> and so on. So by using a variation  of Method 3b,

* we  can initialize the array elements in any order

*/

student st5[5]={

  [3]={

              .name = "Titus",

              .age  = 22,

              .<span class="blsp-spelling-error">rno</span>  = 1041      },

[2]={

              .name = "Stephen",

              .age  = 23,

              .<span class="blsp-spelling-error">rno</span>  = 1042

      }

      /*As you can see only 2<span class="blsp-spelling-error">nd</span> and 3rd array elements has

       * been initialized and that too not in order. Such

       * an initialization is useful, if we wish to allocate

       * some fixed size array but only initialize some element

       */
};

<span class="blsp-spelling-error">printf</span>("%s %hi %u\n%s %hi %u\n\n",st5[2].name,st5[2].age,

st5[2].<span class="blsp-spelling-error">rno</span>, st5[3].name,st5[3].age,st5[3].rno);

}</pre>
<div style="text-align:justify;"><span style="font-family:arial;">The Methods 3a, 3b and 3c are the ways of structure initialization that I have seen in the kernel code. If you find any other way to do the structure initialization, kindly comment. </span></p>
<div style="text-align:left;"><a href="http://linuxprograms.wordpress.com/2008/03/07/3/"><span style="font-family:arial;">Previous</span><br />
<span style="font-family:arial;">C: Structure Initilaization</span></a></div>
<div style="text-align:left;">For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</div>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=4&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<georss:point>12.971606 77.594376</georss:point>
		<geo:lat>12.971606</geo:lat>
		<geo:long>77.594376</geo:long>
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>C: Structure Initialization</title>
		<link>http://linuxprograms.wordpress.com/2008/03/07/3/</link>
		<comments>http://linuxprograms.wordpress.com/2008/03/07/3/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 12:21:03 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Structure]]></category>
		<category><![CDATA[Structure Initialization]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/03/07/3/</guid>
		<description><![CDATA[Next: C: Structure Initialization (Advanced) There are several ways by which a structure can be initialized. In the first way, we first declare the structure variable and initialize the fields of the structure with its name along with the variable name. i.e., Suppose consider a structure &#8216;student&#8216; structure student { int roll; int class; char [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=3&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;">
<div style="text-align:right;"><a href="http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/">Next:<br />
C: Structure Initialization (Advanced)</a></div>
<p>There are several ways by which a structure can be initialized. In the first way, we first declare the structure variable and initialize the fields of the structure with its name along with the <span class="blsp-spelling-corrected">variable</span> name.<br />
i.e., Suppose consider a structure &#8216;<span style="font-style:italic;">student</span>&#8216;</p>
<pre>structure student {

   int roll;

   int class;

   char name[25];

};</pre>
<p>Now we <span class="blsp-spelling-corrected">declare</span> variable for the structure student st1<br />
Now to initialize this variable, we access the elements of the structure using the variable<br />
i.e.,</p>
<pre>      st1.roll  = 21;

      strncpy (st1.name, "Albert", 10);</pre>
<p>There can be case in which we do not initialize some elements of the structure, like in the above case, we did not initialize the element class for the variable st1. This can be considered as a big disadvantage. Because we may forget <span class="blsp-spelling-corrected">initializing</span> some fields.</p>
<p>In the second way, we initialize the structure elements using the set notation.<br />
like the variable st2 can be initialized as</p>
<pre>     struct student st2 = {22, 10, "Alan"};</pre>
<p>Unlike the first way, where we can initialize the elements of the structure in any order, here we must remember the order of the elements in the structure. But chances of not initializing a particular element is very less.</p>
<p><span style="font-family:verdana;">The following program demonstrates these two ways of</span><span style="font-family:arial;"> structure initialization. Also check the way an array of structures is initialized. </span></div>
<pre>/*Different Ways of structure initialization*/#include &amp;<span class="blsp-spelling-error">lt</span>;stdio.h&gt;

#include &amp;<span class="blsp-spelling-error">lt</span>;string.h&gt;

#define NAME_LEN 25

<span class="blsp-spelling-error">typedef</span> unsigned short age_t;

<span class="blsp-spelling-error">typedef</span> unsigned int roll_t;

<span class="blsp-spelling-error">typedef</span> <span class="blsp-spelling-error">struct</span> student{

char name[NAME_LEN];

roll_t <span class="blsp-spelling-error">rno</span>;

age_t age;

}student;

int main()

{

/*

* Method 1a:Commonly seen initialization of a structure
<ul>*/student st;<span class="blsp-spelling-error">strcpy</span>(st.name,"Albert");

st.age=23;

st.<span class="blsp-spelling-error">rno</span>=1034;</ul>

<span class="blsp-spelling-error">printf</span>("%s %hi %u\n\n",st.name,st.age,st.rno);

/*

* Method 1b: For initialing an array of structures

*Commonly seen initialization of a structure

*/
<ul>   student st1 [2];<span class="blsp-spelling-error">strcpy</span>(st1[0].name,"Albert");st1[0].age=23;

st1[0].<span class="blsp-spelling-error">rno</span>=1034;</ul>

<span class="blsp-spelling-error">strcpy</span>(st1[1].name,"Alvin");

st1[1].age=24;

st1[1].<span class="blsp-spelling-error">rno</span>=1035;

<span class="blsp-spelling-error">printf</span>("%s %hi %u\n%s %hi %u\n\n",st1[0].name,st1[0].age,

st1[0].rno,st1[1].name,st1[1].age,st1[1].rno);

/*

* Method 2a:Initializing like a set, the only requirement is

* that the order by which these elements are entered

* should be as in the declaration

*/
<ul> student st2={"Alvin",1035,22};<span class="blsp-spelling-error">printf</span>("%s %hi %u\n\n",st2.name,st2.age,st2.rno);</ul>

/*

* Method 2b: For Initializing an array of structures

*/
<ul>   student st2b[2]={                 {"Alan",1036,23},         {"Eric",1037,22}};</ul>

<span class="blsp-spelling-error">printf</span>("%s %hi %u\n%s %hi %u\n\n",st2b[0].name,st2b[0].age,

st2b[0].rno, st2b[1].name,st2b[1].age,st2b[1].rno);

}</pre>
<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</p>
<div style="text-align:right;"><a href="http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/">Next:<br />
C: Structure Initialization (Advanced)</a></div>
<div style="text-align:right;"></div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=3&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/03/07/3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming with FIFO: mkfifo(), mknod()</title>
		<link>http://linuxprograms.wordpress.com/2008/02/15/programming-with-fifo-mkfifo-mknod/</link>
		<comments>http://linuxprograms.wordpress.com/2008/02/15/programming-with-fifo-mkfifo-mknod/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 13:35:00 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[FIFO]]></category>
		<category><![CDATA[mkfifo()]]></category>
		<category><![CDATA[mknod()]]></category>
		<category><![CDATA[pipe()]]></category>
		<category><![CDATA[Pipes]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/02/15/programming-with-fifo-mkfifo-mknod/</guid>
		<description><![CDATA[FIFOs can be created from the shell. But they can also be created using programs. There are two ways by which FIFOs can be created from the program Creating a FIFO 1. mknod Syntax int mknod(const char *pathname, mode_t mode, dev_t dev); where pathname corresponds to the fifo name, mode corresponds to the file permissions. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=119&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>FIFOs can be created from the shell. But they can also be created using programs. There are two ways by which FIFOs can be created from the program</p>
<p>Creating a FIFO</p>
<p><strong>1. </strong><strong>mknod</strong></p>
<p>Syntax</p>
<pre>int mknod(const char *pathname, mode_t mode, dev_t dev);</pre>
<p>where pathname corresponds to the fifo name, mode corresponds to the file permissions. Since mknod can be used to create a regular file, block or charcter special files and fifo. We have to specify the file type. Corresponding to FIFO: the file type is S_IFIFO.</p>
<pre>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;

int main(int argc, char *argv[]){
     int result;
     if (argc != 2) {
             fprintf(stderr, "Usage: ./a.out fifoname\n");
             exit (1);
     }

     result = mknod (argv[1], S_IRUSR| S_IWUSR|S_IFIFO, 0);
     if (result &lt; 0) {
          perror ("mknod");
          exit (2);
       }
  }</pre>
<p>Here we wish to give both read and write permissions to the user. So we specified S_IRUSR | S_IWUSR| S_IFIFO in the mode. If  the  file  type is S_IFCHR or S_IFBLK then dev is checked; otherwise it is ignored. So we passed 0 as the argument.</p>
<p><span style="font-weight:bold;">2. mkfifo</span></p>
<p><span>Syntax</span></p>
<pre>int mkfifo(const char *pathname, mode_t mode);</pre>
<p><span style="font-weight:bold;"><span style="font-weight:normal;">where pathname correspnds to fifo name and mode corresponds to file mode.</span></span></p>
<pre> # include &lt;stdio.h&gt;
 # include &lt;stdlib.h&gt;
 # include &lt;sys/types.h&gt;
 # include &lt;sys/stat.h&gt;

int main(int argc, char *argv[]){
     int result;

     if (argc != 2) {
          fprintf(stderr, "Usage: ./a.out fifoname\n");
          exit (1);
     }

    result = mkfifo (argv[1], S_IRUSR| S_IWUSR);
     if (result &lt; 0) {
          perror ("mkfifo");
          exit (2);
      }
  }</pre>
<p><span>Here we need not explicitly specify the file type S_IFIFO. You can (if you wish) specify S_IFIFO in the mod</span><span>e.</span></p>
<p>Also refer the <a href="http://programming.johnsamuel.in/main/index.php/use-of-constants-in-c-programming/">Use of Constants in C Programming</a></p>
<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/119/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/119/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=119&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/02/15/programming-with-fifo-mkfifo-mknod/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Pipes in Linux Processes</title>
		<link>http://linuxprograms.wordpress.com/2008/02/15/using-pipes-in-linux-processes/</link>
		<comments>http://linuxprograms.wordpress.com/2008/02/15/using-pipes-in-linux-processes/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 11:43:00 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[Pipes]]></category>
		<category><![CDATA[Process]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/02/15/using-pipes-in-linux-processes/</guid>
		<description><![CDATA[Pipes can be used in threads and processes. The program below demonstrates how pipes can be used in processes. A new process can be created using the system call fork(). It returns two differnt values to the child and parent. The value 0 is returned to the child (new) process and the PID (Process ID) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=118&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Pipes can be used in threads and processes. The program below demonstrates how pipes can be used in processes. A new process can be created using the system call fork(). It returns two differnt values to the child and parent. The value 0 is returned to the child (new) process and the PID (Process ID) of the child is returned to the parent process. This is used to distinguish between the two processes. In the program given below, the child process waits for the user input and once an input is entered, it writes into the pipe. And the parent process reads from the pipe.</p>
<p><span style="font-weight:bold;">A sample program to demonstrate how pipes are used in Linux Processes</span></p>
<pre>#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define MSGLEN  64

int main(){
    int     fd[2];
    pid_t pid;
    int     result;

    //Creating a pipe
    result = pipe (fd);
    if (result &lt; 0) {
        //failure in creating a pipe
        perror("pipe");
        exit (1);
    }

    //Creating a child process
    pid = fork();
    if (pid &lt; 0) {
         //failure in creating a child
         perror ("fork");
         exit(2);
    }

    if (pid == 0) {
        //Child process
         char message[MSGLEN];

          while(1) {
                    //Clearing the message
                    memset (message, 0, sizeof(message));
                    printf ("Enter a message: ");
                    scanf ("%s",message);

                    //Writing message to the pipe
                    write(fd[1], message, strlen(message));
            }
            exit (0);
    }
    else {
        //Parent Process
         char message[MSGLEN];

         while (1) {
                    //Clearing the message buffer
                    memset (message, 0, sizeof(message));

                    //Reading message from the pipe
                   read (fd[0], message, sizeof(message));
                    printf("Message entered %s\n",message);
            }

            exit(0);
     }
}</pre>
<p>Note here that the pipe() system call was called before the system call fork(). The buffer allocated to the pipe is accessible to both the processes.</p>
<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/118/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/118/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=118&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/02/15/using-pipes-in-linux-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>FIFO &#8211; Named pipes: mkfifo, mknod</title>
		<link>http://linuxprograms.wordpress.com/2008/02/14/fifo-named-pipes-mkfifo-mknod/</link>
		<comments>http://linuxprograms.wordpress.com/2008/02/14/fifo-named-pipes-mkfifo-mknod/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 06:21:00 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[Pipes]]></category>
		<category><![CDATA[FIFO]]></category>
		<category><![CDATA[mkfifo()]]></category>
		<category><![CDATA[mknod()]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/02/14/fifo-named-pipes-mkfifo-mknod/</guid>
		<description><![CDATA[Pipes are commonly used for interprocess communication. But the major disadvantage of pipes is that they can be used only by one process (there are readers and writers within the same process) or the processes which share the same file descriptor table (normally the processes and the child processes or threads created by them). Thus [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=117&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;">Pipes are commonly used for interprocess communication. But the major disadvantage of pipes is that they can be used only by one process (there are readers and writers within the same process) or the processes which share the same file descriptor table (normally the processes and the child processes or threads created by them). Thus pipes have this big limitation: they cannot pass information between unrelated processes. This is because they do not share the same file descriptor table. But if names are given to the pipes, then one would be able to read or write data to them just like a normal file. The processes need not even share anything with each otherFIFO (First In First Out) are also called named pipes. The main features of FIFO are<br />
1. It implements FIFO feature of the pipes<br />
2. They can be opened just like normal files using their names<br />
3. Data can be read from or written to the fifo<br />
<span style="font-weight:bold;font-size:130%;"><br />
Working with FIFO in a Shell</span><br />
<span style="font-weight:bold;">Creating a FIFO<br />
</span></p>
<pre>mkfifo</pre>
<p>creates fifo- the named pipes</p>
<p>Syntax</p>
<pre>mkfifo [options] fifo_name</pre>
<p>Example</p>
<pre>$ mkfifo fifo</pre>
<p>There is one more way by which we can FIFO using mknod. mknod is used to create block or character special files.</p>
<pre>$ mknod [OPTION]... NAME TYPE</pre>
<p>To create a FIFO fifo1</p>
<pre>$ mknod fifo1 p</pre>
<p>where p coressponds to file type : pipe (remember FIFO is a  named pipe).</p>
<p><span style="font-size:130%;"><span style="font-weight:bold;">Reading/ Writing data from/to a FIFO</span></span><br />
Let&#8217;s open two terminals<br />
In the first terminal</p>
<pre>$ cat &gt; fifo</pre>
<p>we are experimenting with the FIFOThis is second line. After opening the fifo in the second terminal for readingusing cat, you will notice the above two lines displayed there.<br />
Now open the second terminal and go to the directory containing the FIFO &#8216;fifo&#8217;</p>
<pre>$ cat fifo</pre>
<p>we are experimenting with the FIFOThis is second line. After opening the fifo in the second terminal for reading<br />
Now keep on writing to the first terminal. You will notice that every time you press enter, the coressponding line appears in the second terminal.</p>
<p>Pressing CTRL+D in the first terminal terminates writing to the fifo. This also terminates the second process because reading from the fifo now generates a &#8220;BROKEN PIPE&#8221; signal. The default action for this is to terminate the process.</p>
<p>Let us now see the details of the file &#8216;fifo&#8217;</p>
<pre>$ ls -l fifo
prw-r--r-- 1 user user 0 Feb 14 10:05 fifo</pre>
<p>The p in the beginning denotes that it is a pipe.</p>
<p>Let&#8217;s see more details about the pipe using stat</p>
<pre>$ stat fifo
File: `fifo'Size: 0               Blocks: 0          IO Block: 4096   fifo
Device: fd00h/64768d    Inode: 1145493     Links: 1
Access: (0644/prw-r--r--)  Uid: (    0/    user)   Gid: (    0/    user)
Access: 2008-02-14 10:05:49.000000000 +0530
Modify: 2008-02-14 10:05:49.000000000 +0530
Change: 2008-02-14 10:05:49.000000000 +0530</pre>
<p>If you notice carefully, FIFOs just like a normal file possess all the details like inode number, the number of links to it, the access, modification times, size and the access permissions.</p>
<p>As in the case of pipes, there can be multiple readers and writers to a pipe. Try opening multiple terminals to read from and write to a pipe.</p>
<p>For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/117/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/117/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=117&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/02/14/fifo-named-pipes-mkfifo-mknod/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
		<item>
		<title>Operations on Pipe (Advanced Pipe Usage)</title>
		<link>http://linuxprograms.wordpress.com/2008/01/24/operations-on-pipe-advanced-pipe-usage/</link>
		<comments>http://linuxprograms.wordpress.com/2008/01/24/operations-on-pipe-advanced-pipe-usage/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 12:05:00 +0000</pubDate>
		<dc:creator>John Samuel</dc:creator>
				<category><![CDATA[Pipes]]></category>
		<category><![CDATA[pipe()]]></category>
		<category><![CDATA[pthread]]></category>
		<category><![CDATA[Reader-Writer]]></category>

		<guid isPermaLink="false">http://linuxprograms.wordpress.com/2008/01/24/operations-on-pipe-advanced-pipe-usage/</guid>
		<description><![CDATA[we discussed how we can work with pipe() system call, where it is used(Linux shell), how to use pipes in threads(processes). Here we can discuss on the topic &#8216;Number of readers and writers accessing a pipe&#8217; A pipe can have more than one reader and more than one writer. Only one thing that must be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=115&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>we discussed how we can work with pipe() system call, where it is used(Linux shell), how to use pipes in threads(processes).</p>
<p>Here we can discuss on the topic &#8216;Number of readers and writers accessing a pipe&#8217;<br />
A pipe can have more than one reader and more than one writer. Only one thing that must be kept in mind is that writer writes into fd[1] and reader reads from fd[0], where fd is the filedescriptor passed for the pipe() call.</p>
<pre>int pipe(int fd[2]);</pre>
<p>The program below demonstrates this with two writers and one reader. The writer Writer_ABC writes the capital Alphabet and the Writer_abc writes the small alphabet into the pipe. The reader reads in the order of the letters written by the two writers</p>
<pre>#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;
#include &lt;pthread.h&gt;

int fd[2];//File descriptor for creating a pipe

//This function continously reads fd[0] for any input data byte
//If available, prints

void *reader()
{
    while(1){
       char    ch;
       int     result;

       result = read (fd[0],&amp;ch,1);
       if (result != 1) {
            perror("read");
            exit(3);
    }    printf ("Reader: %c\n", ch);  }
}

//This function continously writes Capital Alphabet into fd[1]
//Waits if no more space is available

void *writer_ABC()
{
     int     result;
     char    ch='A';

     while(1){
           result = write (fd[1], &amp;ch,1);
           if (result != 1){
               perror ("write");
               exit (2);
           }

           printf ("Writer_ABC: %c\n", ch);
           if(ch == 'Z')
              ch = 'A'-1;

           ch++;
      }
}

//This function continously writes small Alphabet into fd[1]
//Waits if no more space is available

void *writer_abc()
{
  int     result;  char    ch='a';

  while(1){
      result = write (fd[1], &amp;ch,1);
      if (result != 1){
            perror ("write");
            exit (2);
      }

      printf ("Writer_abc: %c\n", ch);
      if(ch == 'z')
            ch = 'a'-1;

     ch++;
  }
}

int main()
{
   pthread_t       tid1,tid2,tid3;
   int             result;

   result = pipe (fd);
   if (result &lt; 0){
       perror("pipe ");
       exit(1);
   }

 pthread_create(&amp;tid1,NULL,reader,NULL);
 pthread_create(&amp;tid2,NULL,writer_ABC,NULL);
 pthread_create(&amp;tid3,NULL,writer_abc,NULL);

 pthread_join(tid1,NULL);
 pthread_join(tid2,NULL);
 pthread_join(tid3,NULL);
}</pre>
<p><span style="font-weight:bold;">Output</span></p>
<pre>Writer_abc: o
Writer_abc: p
Writer_abc: q
Writer_abc: r
Writer_abc: s
Writer_abc: t
Writer_abc: u
Reader: A
Reader: B
Reader: C
Reader: D
Reader: E
Reader: F
Reader: G
Reader: H
Reader: I
Reader: J
Reader: K
Reader: L
Reader: M
Reader: N
...
For more updates, check <strong><a href="http://programming.johnsamuel.in/">Programming Insights</a></strong>.</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/linuxprograms.wordpress.com/115/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/linuxprograms.wordpress.com/115/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/linuxprograms.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/linuxprograms.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/linuxprograms.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=linuxprograms.wordpress.com&amp;blog=3088535&amp;post=115&amp;subd=linuxprograms&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://linuxprograms.wordpress.com/2008/01/24/operations-on-pipe-advanced-pipe-usage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b133ea3256067549158dd0bddfaf42e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnsamuel</media:title>
		</media:content>
	</item>
	</channel>
</rss>
