-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_process_8.c
45 lines (40 loc) · 1.08 KB
/
unix_process_8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char* argv[]) {
int pid = fork();
if (pid == -1) {
return 1;
}
if (pid == 0) {
int file = open("pingResults.txt", O_WRONLY | O_CREAT, 0777);
if (file == -1) {
return 2;
}
printf("The fd to pingResults: %d\n", file);
dup2(file, STDOUT_FILENO);
close(file);
int err = execlp("ping", "ping", "-c", "3", "google.com", NULL);
if (err == -1) {
printf("Could not find program to execute or other error ocurred\n");
return 2;
}
} else {
int wstatus;
wait(&wstatus);
if (WIFEXITED(wstatus)) {
int statusCode = WEXITSTATUS(wstatus);
if (statusCode == 0) {
printf("Success\n");
} else {
printf("Failure with status code %d\n", statusCode);
}
}
}
return 0;
}