blob: 682376d5f1479a76ea1289da9cdf7e546aa48a62 (
plain) (
blame)
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
|
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main() {
int f1 = open("read.txt", O_RDONLY);
if (f1 < 0) {
perror("open read.txt");
return 1;
}
int f2 = open("write.txt", O_CREAT | O_WRONLY | O_APPEND, 0755);
if (f2 < 0) {
perror("open write.txt");
return 1;
}
pid_t fpid = fork();
if (fpid < 0) {
perror("fork");
return 2;
}
if (fpid == 0) { // child
char buff[3];
for (int i = 0; i < 6; i++) {
read(f1, buff, 3);
write(f2, buff, 3);
sleep(1);
}
}
else {
int status = -2;
while (status == -2) {
sleep(2);
write(f2, "abc", 3);
waitpid(fpid, &status, WNOHANG);
}
}
}
|