Use unix or linux terminal to solve This inlab assignment is to help you understand system call
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 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.
Applied Statistics for Public and Nonprofit Administration
ISBN: 978-1285737232
9th edition
Authors: Kenneth J. Meier, Jeffrey L. Brudney, John Bohte