Description
In this practice class, we will study the use of the fork() and execlp() functions in combination. See the skeleton code given here.
– Skeleton C Program: fork + execlp
#include
#include
int main (int argo, char *argv[]) {
int pid; /* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf (stderr, “Fork Failed”);
exit (-1);
} else if (pid == 0) { /* child process */
execlp (“/bin/ls”, “ls”, NULL);
} else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf (“Child Complete”);
exit (0);
}
}
Assignment:
Q1. Write a C code for implementing fork + execlp combination where execlp is used in child to do the following:
(A) starts a simple program printing “Hello World” on the screen, and then
(B) uses the system() function to execute the “ps” command.
The parent process uses the system() function to execute the “ls” command and then waits for the child to finish. Measure the total time to execute the program using the system clock.
Pablo Mendez –
Follows the required notes. Right on time. Amazing solution.
Kathy Henley –
Second time coming to this website for a paper and it worked out great.