Categories > Coding > C++ >

Fibonacci series

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 2

Posted

This function accepts the number of terms in the Fibonacci sequence in the child process, creates an array, and redirects the output to the parent via pipe. Parent must wait till the child develops the Fibonacci series. The received text always displays -1, although the transmitted text displays the number of entered numbers *4, which is acceptable.

 

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

int* fibo(int n)
{
    int* a=(int*)malloc(n*sizeof(int));
    *(a+0)=0;
    *(a+1)=1;
    int i;
    for(i=0;i<n-2;i++)
    {
        *(a+i+2)=*(a+i)+(*(a+i+1));
    }
    return a;
    }


    int main()
    {
        int* fib;
        int fd[2];
        pid_t childpid;
        int n,nb;

        int k=pipe(fd);
        if(k==-1)
        {
        printf("Pipe failed");
        return 0;
    }

    childpid=fork();
    if(childpid == 0) 
    {
        printf("Enter no. of fibonacci numbers");
        scanf("%d",&n);
        fib=fibo(n);
        close(fd[0]);
        nb=(fd[1],fib,n*sizeof(int));
        printf("Sent string: %d \n",nb);
        exit(0);
    }
    else
    {
        wait();
        close(fd[1]);
        nb= read(fd[0],fib,n*sizeof(int));
        printf("Received string: %d ",nb);
    }
    return 0;
}
  • 0

efewss

ecstxsy

Posts: 116

Threads: 14

Joined: Apr, 2021

Reputation: 3

Replied

move to c++ pls 

  • 0

my dude entity 🤜🤛

 

https://cdn.discordapp.com/attachments/775762417618780193/978180264037593129/1653286667685.png

Cuby

Student loans

vip

Posts: 205

Threads: 5

Joined: Mar, 2019

Reputation: 57

Replied

Almost believed the man with a stock photo to be a senior developer

  • 0

Join the Front-Page Club!

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )