Files
2026-06-25 00:09:09 +08:00

37 lines
845 B
C++

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
unlink("fa");
unlink("fb");
mkfifo("fa", 0666);
mkfifo("fb", 0666);
printf("I am Peter...\n");
if (fork() == 0) {
int fd2 = open("fb", O_RDONLY);
char buf[100];
while (1) {
memset(buf, 0, 100);
if (read(fd2, buf, 100) == 0) {
kill(getppid(), 9);
return 0;
}
printf("\rJack says:%s", buf);
fflush(stdout);
}
} else {
int fd1 = open("fa", O_WRONLY);
char buf[100];
while (1) {
fgets(buf, sizeof(buf), stdin);
write(fd1, buf, sizeof(buf));
}
}
return 0;
}