40 lines
914 B
C
40 lines
914 B
C
// This program creates a fork and uses execl to run the ls command
|
|
// in the child process. The parent process waits for the child to
|
|
// finish and then prints a message containing the child's exit status.
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
|
|
#define LS "/run/current-system/sw/bin/ls"
|
|
#define PATH "/home/dm"
|
|
|
|
int main() {
|
|
pid_t pid;
|
|
int status;
|
|
|
|
pid = fork();
|
|
if (pid < 0) {
|
|
perror("fork");
|
|
exit(1);
|
|
}
|
|
|
|
if (pid == 0) {
|
|
// Child process
|
|
execl(LS, "ls", "-lasi", PATH, NULL);
|
|
// this line will only be reached if execl fails, meaning that it
|
|
// will not influence the status
|
|
perror("execl");
|
|
exit(3);
|
|
}
|
|
|
|
// Parent process
|
|
wait(&status);
|
|
printf("exit status: 0x%04x\n", status);
|
|
printf("Child exited with status %d\n", WEXITSTATUS(status));
|
|
|
|
return 0;
|
|
}
|
|
|