Question
Use unix or linux terminal to solve This inlab assignment is to help you understand system call fork(). Part I: Please type(you can use any
- Use unix or linux terminal to solve
This inlab assignment is to help you understand system call fork().
Part I:
Please type(you can use any editor such as vi), compile and run the following Unix program. Please submit the program and running result.
/*
* This program forks a separate process using the fork()/exec() system calls.
*/
#include
#include
#include
int main()
{
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
printf("I am the child %d",pid);
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
printf("I am the parent %d",pid);
wait(NULL);
printf("Child Complete");
exit(0);
}
}
Part II:
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, if 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.
Step by Step Solution
3.45 Rating (171 Votes )
There are 3 Steps involved in it
Step: 1
1 running your code and giving screenshot of the output I am giving you the ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started