Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

Print fabionci with fork in C

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   int a=0, b=1, n=a+b,i,ii;
   pid_t pid;

   printf("Enter the number of a Fibonacci Sequence:
");
   scanf("%d", &ii);

   if (ii < 0)
      printf("Please enter a non-negative integer!
");
   else
   {
      pid = fork();
      if (pid == 0)
      {
         printf("Child is producing the Fibonacci Sequence...
");
         printf("%d %d",a,b);
         for (i=0;i<ii;i++)
         {
            n=a+b;
            printf("%d ", n);
            a=b;
            b=n;
         }
         printf("Child ends
"); 
      }
      else 
      {
         printf("Parent is waiting for child to complete...
");
         wait(NULL);
         printf("Parent ends
");
      }
   }
   return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Print #fabionci #fork #C
ADD COMMENT
Topic
Name
7+4 =