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

<channel>
	<title>Samad's Blog &#187; Coding</title>
	<atom:link href="http://blog.samad64.com/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.samad64.com</link>
	<description>Yes.. I blog. Get over it.</description>
	<lastBuildDate>Thu, 26 Aug 2010 19:49:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unix Lucas Fork Program</title>
		<link>http://blog.samad64.com/2008/04/05/unix-lucas-fork-program/</link>
		<comments>http://blog.samad64.com/2008/04/05/unix-lucas-fork-program/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 18:05:43 +0000</pubDate>
		<dc:creator>Samad</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://blog.samad64.com/2008/04/05/unix-lucas-fork-program/</guid>
		<description><![CDATA[I wrote this program for a class a few semesters ago. It is a Lucas Number calculator that uses forks/pipes (a requirement of the project). Works great, hopefully it will help someone out there! It is also available for download below. /* Lucas Fork &#038; Pipe*/ #include &#60;stdio.h&#62; #include &#60;time.h&#62; // to print the time [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote this program for a class a few semesters ago. It is a Lucas Number calculator that uses forks/pipes (a requirement of the project). Works great, hopefully it will help someone out there! It is also available for download below.</p>
<p><code><br />
/*  Lucas Fork &#038; Pipe*/<br />
#include &lt;stdio.h&gt;<br />
#include &lt;time.h&gt; // to print the time</p>
<p>float    fib(int); // fibonacci function, recursive<br />
int    fork(void);<br />
void   sleep(unsigned);</p>
<p>int main(void)<br />
{<br />
   int n; // input variable<br />
   int i; // for the loop<br />
   int start = time(NULL); // get the time the process started<br />
        char buf[100]; /* store the characters read */</p>
<p>   // pipe needs an integer array of size 2<br />
		int fd[2];<br />
		printf("Create the pipe\n\n");<br />
		pipe(fd); /* fd[0] read; fd[1] write */</p>
<p>	printf("Enter a number: ");<br />
	if (scanf("%d", &#038;n) != 1) { // check to make sure there is input<br />
		printf("Input error.\n");<br />
		return 1;<br />
	}<br />
		printf("Create the child process using fork\n");<br />
   if (fork() == 0) {    // child process<br />
		close(fd[1]); /* close write end */<br />
		printf("child is running...\n");<br />
        printf("about to read from the pipe\n");<br />
		n = read( fd[0], buf, 100); /* child reads from pipe */<br />
		printf("after reading from the pipe\n");<br />
        printf("the value of n is: %d\n",n);<br />
        printf("the value in the buffer is: %s\n\n", buf);<br />
      for (i = 0; i <= n; ++i) {<br />
         printf("fib(%2d) = %d\n", i, fib(i));<br />
		 sleep(1);<br />
		}<br />
	}<br />
   else    {            // parent process<br />
		close(fd[0]); /* close read end - not needed for this example */<br />
		printf("parent is running...\n");<br />
        printf("about to write to the pipe\n");<br />
        write( fd[1], "Writing to pipe\n", n); /* parent writes to pipe */<br />
        printf("after writing to the pipe\n\n");<br />
	 for (i = 0; i < n; ++i) {<br />
         sleep(1);<br />
         printf("Calculating...elapsed time = %d\n", time(NULL) - start);<br />
      }<br />
	}<br />
   return 0; // exits program<br />
}</p>
<p>float fib(int n) // fibonacci function, recursive<br />
{<br />
   if (n <= 1)<br />
      return n;<br />
   else<br />
      return (fib(n - 1) + fib(n - 2));<br />
}<br />
</code></p>
<p><a title="Lucas Fork Source File (zipped)" href="http://blog.samad64.com/wp-content/uploads/2008/04/lucas-fork.zip">Lucas Fork Source File (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.samad64.com/2008/04/05/unix-lucas-fork-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
