Question: +++++++++++++++++++++++++++++++FORK_EXEC.C+++++++++++++++++++++++++++++ #include #include #include #include #include /* should add this header */ /* pid_t fork(void) is declared in unistd.h */ /* pid_t is a special

+++++++++++++++++++++++++++++++FORK_EXEC.C+++++++++++++++++++++++++++++

#include

#include

#include

#include

#include /* should add this header */

/* pid_t fork(void) is declared in unistd.h */

/* pid_t is a special type for process ids. it's equivalent to int. */

int main(void)

{

pid_t child_pid;

int status;

pid_t wait_result;

child_pid = fork();

if (child_pid == 0)

{

/* this code is only executed in the child process */

printf ("I am a child and my pid = %d ", getpid());

execl("/bin/ls", "ls", "-l", NULL);

/* if execl succeeds, this code is never used */

printf ("Could not execl file /bin/ls ");

/* this exit stops only the child process */

exit(1);

}

else if (child_pid > 0)

{

/* this code is only executed in the parent process */

printf ("I am the parent and my pid = %d ", getpid());

printf ("My child has pid = %d ", child_pid);

printf ("I am a parent and I am going to wait for my child ");

do

{

/* parent waits for the SIGCHLD signal sent

to the parent of a (child) process when

the child process terminates */

wait_result = wait(&status);

} while (wait_result != child_pid);

printf ("I am a parent and I am quitting. ");

}

else

{

printf ("The fork system call failed to create a new process "

);

exit(1);

}

return 0;

}

Suppose we want the new process to do something quite different from the parent process, namely run a different program. The execl system call loads a new executable into memory and associates it with the current process. In other words, it changes things so that this process starts executing executable code from a different file.

Look at the program fork_exec.c. Compile and execute it. Answer the following question: include a screenshot of each of your answer

Does the child processs id change as a result of the execl() system call? How can you tell whether it has been changed or not?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!