Professional Documents
Culture Documents
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int status;
pid1 = fork();
if (pid1 == 0) {
exit(20);
pid2 = fork();
if (pid2 == 0) {
exit(25);
pid3 = fork();
if (pid3 == 0) {
exit(30);
}
Question#2
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
sleep(10);
exit(0);
printf("Parent process with PID %d created child process with PID %d\n", getpid(), pid);
sleep(5);
printf("Parent process with PID %d is executing the ps command to display child process information:\
n", getpid());
char command[100];
system(command);
waitpid(pid, &status, 0);
printf("Parent process with PID %d waited for child process with PID %d and received exit status %d\
n", getpid(), pid, WEXITSTATUS(status));
return 0;
Question#3
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
if (pid == 0) {
// Child process
perror("execl");
exit(1);
// Parent process
wait(NULL);
} else {
// Error
perror("fork");
exit(1);
return 0;