blob: 4d9966d03c77567c8087d805c85d3229005b9c11 (
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
|
#include <unistd.h> // fork, write, close, sleep
#include <fcntl.h> // open
#include <stdio.h> // perror
int main(int argc, char* argv[]) {
pid_t f = fork();
int file = open("test.txt", O_WRONLY);
if (file < 0) {
perror("open");
return 1;
}
if (f == 0) { // in child
char buf = 'c';
for (int i = 0; i < 5; i++) {
sleep(2);
if(write(file, &buf, 1) < 0) {
perror("write");
return 2;
}
}
}
else {
char buf = 'p';
for (int i = 0; i < 5; i++) {
if(write(file, &buf, 1) < 0) {
perror("write");
return 3;
}
sleep(1);
}
}
close(file);
}
|