步骤1:同上
步骤2:建立目录
cd workdir/linux/application/10-comm/
mkdir fifo
cd fifo
步骤3:复制
cp /mnt/hgfs/share/2.\ Linux 系统部分/13.\ Linux系统有名管道通信实验/实验代码/write_fifo.c ./ -a
cp /mnt/hgfs/share/2.\ Linux 系统部分/13.\ Linux系统有名管道通信实验/实验代码/read_fifo.c ./ -a
cp /mnt/hgfs/share/2.\ Linux 系统部分/13.\ Linux系统有名管道通信实验/实验代码/create_fifo.c ./ -a
步骤4:执行
gcc create_fifo.c -o create_fifo
./create_fifo test
gcc read_fifo.c -o read_fifo

gcc write_fifo.c -o write_fifo
./read_fifo test
步骤5:重新打开一个新终端
进入该目录
./write_fifo test
输入任意字符

在旧的终端可以接受到信息

附:程序源码
create_fifo.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/stat.h> 4 #include <string.h> 5 #include <errno.h> 6 7 int main(int argc, char **argv) 8 { 9 if (argc < 2) 10 { 11 fprintf(stdout, "Usage: %s <filename>\n", argv[0]); 12 exit(1); 13 } 14 15 //int mkfifo(const char *path, mode_t mode); 16 if (mkfifo(argv[1], 0644) < 0) 17 { 18 fprintf(stderr, "mkfifo() failed: %s\n", strerror(errno)); 19 exit(1); 20 } 21 22 return 0; 23 }
read_fifo.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 #include <string.h> 7 #include <errno.h> 8 9 #define BUFFER_SIZE 1024 10 11 int main(int argc, char **argv) 12 { 13 int fd; 14 15 if (argc < 2) 16 { 17 fprintf(stdout, "Usage: %s <filename>\n", argv[0]); 18 exit(1); 19 } 20 21 //int open(const char *path, int oflag, ...); 22 if ((fd = open(argv[1], O_RDONLY)) < 0) 23 { 24 fprintf(stderr, "open fifo %s for reading failed: %s\n", argv[1], strerror(errno)); 25 exit(1); 26 } 27 28 fprintf(stdout, "open fifo %s for reading successed.\n", argv[0]); 29 char buffer[BUFFER_SIZE]; 30 ssize_t n; 31 32 while (1) 33 { 34 again: 35 //ssize_t read(int fd, void *buf, size_t count); 36 if ((n = read(fd, buffer, BUFFER_SIZE)) < 0) 37 { 38 if (errno == EINTR) 39 { 40 goto again; 41 } 42 else 43 { 44 fprintf(stderr, "read failed on %s: %s\n", argv[1], strerror(errno)); 45 exit(1); 46 } 47 } 48 else if (n == 0) 49 { 50 fprintf(stderr, "peer closed fifo.\n"); 51 break; 52 } 53 else 54 { 55 buffer[n] = '\0'; 56 fprintf(stdout, "read %d bytes from fifo: %s\n", n, buffer); 57 } 58 } 59 return 0; 60 }
write_fifo.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 #include <signal.h> 7 #include <string.h> 8 #include <errno.h> 9 10 #define BUFFER_SIZE 1024 11 12 void signal_handler(int s); 13 14 int main(int argc, char **argv) 15 { 16 int fd; 17 18 if (argc < 2) 19 { 20 fprintf(stdout, "Usage: %s <filename>\n", argv[0]); 21 exit(1); 22 } 23 24 signal(SIGPIPE, signal_handler); 25 26 //int open(const char *path, int oflag, ...); 27 if ((fd = open(argv[1], O_WRONLY)) < 0) 28 { 29 fprintf(stderr, "open fifo %s for writting failed: %s\n", argv[1], strerror(errno)); 30 exit(1); 31 } 32 33 fprintf(stdout, "open fifo %s for writting successed.\n", argv[0]); 34 35 char buffer[BUFFER_SIZE]; 36 ssize_t n; 37 38 //char *fgets(char *s, int size, FILE * stream); 39 while (fgets(buffer, BUFFER_SIZE, stdin)) 40 { 41 again: 42 //ssize_t write(int fd, const void *buf, size_t count); 43 if ((n = write(fd, buffer, strlen(buffer))) < 0) 44 { 45 if (errno == EINTR) 46 { 47 goto again; 48 } 49 else 50 { 51 fprintf(stderr, "write() failed on fifo: %s\n", strerror(errno)); 52 break; 53 } 54 } // end if 55 } // end while 56 57 return 0; 58 } 59 60 void signal_handler(int signo) 61 { 62 fprintf(stdout, "Caught signal %d\n", signo); 63 }
浙公网安备 33010602011771号