question given like this

The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as:
f ib0 = 0
f ib1 = 1
f ibn = f ibn−1 + f ibn−2

1) Write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.


2) Repeat the preceding exercise, this time using the CreateProcess () in the Win32 API. In this instance, you will need to specify a separate program to be invoked from CreateProcess(). It is this separate program that will run as a child process outputting the Fibonacci sequence. Perform necessary error checking to ensure that a non-negative number is passed on the command line.
i have done with Fibonacci sequence .but i dont know how to include tht fork() function and win32 api .any one can help to finish ?
this is my c Fibonacci programme

#include<stdio.h>

int main()
{
    unsigned int i=0,j=0,sum=1,num;
    printf("nEnter the limit for the series ");
    scanf("%d",&num);
    while(sum<num)
    {
       printf("%d ",sum);
        i=j;
        j=sum;
        sum=i+j;
                      
    }
   
  
  getch();  
}

Recommended Answers

All 2 Replies

First, something like that
Parent process

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    int child_process = 0;
    child_process = fork();
	if(child_process < 0)
		exit(1);	
	if(child_process == 0)
	{
		int res = execl("child", "child 5", "", 0); //place name of your Fibonacci programm instead of first "child", and the second child - it's a command line
	}
//type wait() by yourself

   return 0;
}

clild process

#include<stdio.h>

int main(int argc, char **lpCmdLine)
{
   if(argc < 2)
   {
       perror("Needs 2 arguments in the command line");
       exit(1);
   }
   unsigned int i=0,j=0,sum=1,num;
   num = lpCmdLine[argc-1][0]-48; //make integer from symbol
   while(sum < num)
   {
       printf("%d ",sum);
       i=j;
       j=sum;
       sum=i+j;
   }
   getch(); 
   return 0;
}

CreateProcess
Main Process

#include <Windows.h>
#include <iostream>
#include <stdio>

int main()
{
	STARTUPINFO lpSI,
	PROCESS_INFORMATION lpPI;
	
	GetStartupInfo(&lpSI);

	char *cmd = "5"; //number of iterations

	if(!(CreateProcess("Child.exe", cmd, NULL, NULL, FALSE, 0, NULL, NULL, &lpSI, &lpPI)))
	{
		std::cout << "Error creating process\n";
		exit(1);
	}
	
	WaitForSingleObject(lpPI.hProcess, INFINITE);
	return 0;
}

Child process type by yourself.

Hello - I was wondering if your prgram is working related to "generates the Fibonacci sequence in the child process " Part 1 -could you please send me the code. I need only first part.

I need it very much.

I need code in java.

Anyone - please help.

Thanks and appreciate it very much.

Namaste

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.