#include // fork, write, close, sleep #include // open #include // 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); }