有名管道
有名管道
进程A
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define MESSAGE "can you hear me?" int main() { int fd; fd = open("myfifo", O_RDWR); char buf[1024]; memset(buf, 0, sizeof(buf)); int len = read(fd, buf, sizeof(buf)); printf("process recv message:%s\n", buf); return 0; }
进程B
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define MESSAGE "can you hear me?" int main() { int fd; fd = open("myfifo", O_WRONLY); printf("process send message:%s\n", MESSAGE); write(fd, MESSAGE, strlen(MESSAGE)); return 0; }