-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.c
42 lines (37 loc) · 858 Bytes
/
3.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
// filename: 3.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// read number from argv[1] using atoi.
// If no or an invalid number is provided,
// use the default value 5
int n = ...;
// multiply by 2 and convert to string
char str[16];
snprintf(str, sizeof(str), "%d", 2*n);
// fork
...;
if (...) {
// child
char *child_argv[] = {
..., // prepare arguments
NULL
};
...; // execute program
perror("Cannot execute child.");
} else if (pid > 0) {
// parent
int status, exitcode;
if (...) { // wait for child
exitcode = ...; // extract exit code
printf("Child terminated with exit code %d.\n", exitcode);
} else {
perror("Cannot wait for child.");
}
}
return 0;
}