forked from xxyzz/ostep-hw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.c
40 lines (36 loc) · 1.07 KB
/
2.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
#include <fcntl.h> // open
#include <stdio.h> // fgetc, fopen, fclose
#include <stdlib.h> // exit
#include <string.h> // strlen
#include <sys/stat.h> // S_IRWXU
#include <sys/wait.h>
#include <unistd.h> // fork, write, close
int main() {
int fd = open("./2.txt", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
int rc = fork();
write(fd, "First line.\n", strlen("First line.\n"));
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(EXIT_FAILURE);
} else if (rc == 0) {
write(fd, "child writes a line.\n", strlen("child writes a line.\n"));
printf("file descriptor in child process: %d\n", fd);
} else {
write(fd, "parent writes a line.\n", strlen("parent writes a line.\n"));
printf("file descriptor in parent prosess: %d\n", fd);
if (wait(NULL) == -1) {
fprintf(stderr, "wait failed\n");
exit(EXIT_FAILURE);
}
FILE *fp;
fp = fopen("./2.txt", "r");
int ch;
printf("\nfile contents: \n");
while ((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
}
close(fd);
return 0;
}