Write a program using the fork() system call that prints the given number of Fib
ID: 3838234 • Letter: W
Question
Write a program using the fork() system call that prints the given number of Fibonacci sequence (https://en.wikipedia.org/wiki/Fibonacci_number). This input number has to be at least 2. For example, if the input number is 6, it prints 1 1 2 3 5 8. The input integer will be provided in the command line. Because the parent and child processes have their own copies of the data it will be necessary for the child to output the Fibonacci 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 the input integer is 2 or higher is passed in the command line.Explanation / Answer
Answer:
programe for fibonacci number using fork() system call:
if user enter 6, then it displays:
1 1 2 3 5 8 child ends.
The number of the sequence will be provided in the command line. if 6 is provided, then first six numbers in the Fibonacci sequence will be the 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.
int main()
{
int a=0,b=1,n=a+b,i,j;
pid_t pid;
printf("Enter the number of a Fibonacci Secquence: ");
scanf("%d", &j);
if ( j<0 )
printf("Please enter a integer is 2 or higher is passed ! ");
else
{
pid = fork();
if ( pid==0 )
{
printf("Child is producing the Fibonacci Sequence.... ");
printf("%d %d", a,b);
for(i=0;i<j;i++)
{
n=a+b;
println("%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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.