Question
Type the following C program, this is a sample program for getting process ID and forking a separate process: #include #include int main(void) { int
Type the following C program, this is a sample program for getting process ID and forking a separate process:
#include
#include
int main(void)
{
int i;
printf("FORK TEST PROGRAM ");
printf(" My process id %d and My Parent's process id %d ",getpid(),getppid());
i = fork(); /* A child process is created and the codes are the same */
if (i == -1)
{
printf("How many times do you SEE this line? ");
printf(" Process cannot be created ");
exit(0);
}
if (i !=0) {
/* parent process executes */
printf(" PARENT: I am the parent - My process id %d and My Parent's process id %d ",getpid(),getppid());
printf(" PARENT: My child process id is %d ",i);
}
else
/* Child process continues here. the value that is returned is 0 */
{
printf(" CHILD: I am the child - My process id %d and My Parent's process id %d ",getpid(),getppid());
}
exit(0);
}
Call the above program p2.c, and compile it as follows: gcc -o p2 p2.c
Then execute p2 and report the output.
Questions:
2.1 How many times fork() system call returns in this program? Explain your answer?
2.2 What is the return value of fork() system call?
2.3 What is the parent process ID?
2.4 What is the child process ID?
2.5 What is the parents parent process ID?
2.6 How many times do you see the "How many times do you see this line?" message? Why?
3. The only way in which a program is executed by UNIX Is for an existing process to issue the exec() system call. The exec() system call replaces the current process with the new program. The process ID does not change. Some UNIX manuals incorrectly refer to new program as the new process, but realize that it is really a new program executing in the context of the calling process. A new process is not created by exec(). There are six versions of exec system call: execlp, execl, execle, execvp, execv, execve.
Here, we are using the execvp(file, argv). This is argv style with automatic searching, containing the pointers to the argument strings. This argv array must contain a NULL pointer to specify its end, since a count is not specified.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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