3. Using the code in Section 2, write a C/C++ program that creates three processes, each executing the program that you wrote in Section 1. Make sure that the parent process waits until all three children processes print out their PIDs and terminate.
1. Examine the code below, which prints the process's PID. Save this code in a file named 'proc_id.c', then compile it to produce an executable in the current directory.
#include <stdio.h>
#include <unistd.h>
int main(){
int p_id;
p_id = getpid(); /* process id */
printf("Process ID: %d
", p_id);
return 0;
}
2. Examine the code below and review how to create a process
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 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");
return 0;
}
}