23,217
社区成员




//PIPE 的情况:
int main(int argc, char *argv[]){
int pfd[2];
pipe(pfd);
if(fork() == 0){
char string1[30] = "abcdefg";
close(pfd[0]);
write(pfd[1], string1, sizeof(string1));
int fp = fdopen(pfd[0]); // 这行有必要吗
fflush(fp); // 这行有必要吗?
exit(0);
}
close(pfd[1]);
char string2[30];
read(pfd[0], string2, sizeof(string2));
exit(0);
}
//FIFO 的情况:
int main(int argc, char *argv[]){
mkfifo("./myfifo", 0777);
if(fork() == 0){
char string1[30] = "abcdefg";
int fd1 = open("./myfifo", O_WRONLY);
write(pfd1, string1, sizeof(string1));
int fp = fdopen(pfd1); // 这行有必要吗?
fflush(fp); // 这行有必要吗?
exit(0);
}
int fd2 = open("./myfifo", O_RDONLY);
char string2[30];
read(fd2, string2, sizeof(string2));
exit(0);
}