Thread: Please help...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    Please help...

    The Fibonacci sequence problem: The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as:

    fib0 = 0
    fib1 = 1
    fib2 = fibn-1 + fibn-2

    Write a C program using the fork() system call 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, so can you say solve this without using fork() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    I want to use fork().

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So you said.

    But do we have to explain the basic problem of displaying the Fibonacci series first, or can we get to the point by you showing us what you've managed to far and then we can help you with the next step of making use of fork().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Thumbs up hi

    that shouldn't be too hard

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
      int n;
      printf("please enter the number n\n");
      scanf("%d",&n);
      if (n<=0) 
        {
          printf("moron!\n");
          return 1;
        }
      pid_t pid = fork();
      if (pid == 0)
        {
          /*child process*/
          /*here we print out the fibonacci numbers, which i would do like this*/
          printf("fibonacci-numbers up to %d:\n",n);
          int *fib_array = (int*)malloc(sizeof(int)*n);
          int i;
          for (i=0;i<n;i++)
    	{
    	  if (i<=1)
    	    fib_array[i] = 1;
    	  else
    	    fib_array[i] = fib_array[i-1]+fib_array[i-2];
    	  printf("%d\n",fib_array[i]);
    	}
          free(fib_array); /*not really neccessary, but anyway*/
          exit(0);
        }
      if (pid < 0)
        {
          printf("error forking child\n");
          return 1;
        }
      if (pid > 0)
        {
          /*parent process*/
          int status;
          wait(&status);
          printf("now the child process has exited with the status %d\n",status);
          return 0;
        }
    }
    cheers
    maklolm

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's not a question of being easy, it's a matter of the OP learning to do the homework for themselves and not being spoon-fed a complete answer right off the bat.

    Not that it matters, it seems the OP has already gotten lucky elsewhere as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Thank you maklolm!!!

Popular pages Recent additions subscribe to a feed