/********************************************************************************* * Copyright: (C) 2025 LingYun IoT System Studio * All rights reserved. * * Filename: fifo.c * Description: This file is FIFO chat example program * * Version: 1.0.0(11/07/2025) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "11/07/2025 02:34:43 PM" * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #define FIFO_FILE1 "/tmp/fifo_chat1" #define FIFO_FILE2 "/tmp/fifo_chat2" enum { MODE_WRITER = 1, MODE_READER, }; int g_stop = 0; void sig_handler(int signum) { switch(signum) { case SIGTERM: printf("SIGTERM signal detected(kill/killall)\n"); break; case SIGPIPE: printf("SIGPIPE signal detected(socket error)\n"); break; case SIGINT: printf("SIGINT signal detected(Ctrl+C)\n"); break; default: break; } g_stop = 1; } void print_usage(char *progname) { printf("%s usage: \n", progname); printf(" -w(--writer) writer mode\n"); printf(" -r(--reader) reader mode\n"); printf(" -h(--Help): print this help information\n"); return ; } int main(int argc, char **argv) { int fdr_fifo; int fdw_fifo; int rv; char buf[1024]; int mode = 0; /* 1: Writer 2: Reader */ fd_set rdset; int ch; struct option opts[] = { {"writer", no_argument, NULL, 'w'}, {"reader", no_argument, NULL, 'r'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; /* parser command line input arguments */ while( (ch=getopt_long(argc, argv, "wrh", opts, NULL)) != -1 ) { switch(ch) { case 'w': mode = MODE_WRITER; break; case 'r': mode = MODE_READER; break; case 'h': print_usage(argv[0]); return 0; } } if( !mode ) { print_usage(argv[0]); return 1; } /* 管道是一种半双工的通信方式,如果要实现两个进程间的双向通信则需要两个管道,即两个管道分别作为两个进程的读端和写端 */ if( access(FIFO_FILE1 , F_OK) ) { printf("FIFO file \"%s\" not exist and create it now\n", FIFO_FILE1); mkfifo(FIFO_FILE1, 0666); } if( access(FIFO_FILE2 , F_OK) ) { printf("FIFO file \"%s\" not exist and create it now\n", FIFO_FILE2); mkfifo(FIFO_FILE2, 0666); } signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGPIPE, sig_handler); if( MODE_READER == mode ) { /* 这里以只读模式打开命名管道FIFO_FILE1的读端,默认是阻塞模式;如果另外一端(写端)没被打开则open()将会一直阻塞, 所以另外一个进程必须先以写模式打开该文件FIFO_FILE1,否则会出现死锁 */ printf("start open '%s' for read and it will blocked untill write endpoint opened...\n", FIFO_FILE1); if( (fdr_fifo=open(FIFO_FILE1, O_RDONLY)) < 0 ) { printf("Open fifo[%s] for chat read endpoint failure: %s\n", FIFO_FILE1, strerror(errno)); return 2; } printf("start open '%s' for write...\n", FIFO_FILE2); if( (fdw_fifo=open(FIFO_FILE2, O_WRONLY)) < 0 ) { printf("Open fifo[%s] for chat write endpoint failure: %s\n", FIFO_FILE2, strerror(errno)); return 2; } } else if( MODE_WRITER == mode ) { /* 这里以只写模式打开命名管道FIFO_FILE1的写端,默认是阻塞模式;如果另外一端(读端)没被打开则open()将会一直阻塞, 因为前一个进程是以读模式打开该管道文件的读端,所以这里必须以写模式打开该文件的写端,否则会出现死锁 */ printf("start open '%s' for write and it will blocked untill read endpoint opened...\n", FIFO_FILE1); if( (fdw_fifo=open(FIFO_FILE1, O_WRONLY)) < 0 ) { printf("Open fifo[%s] for chat write endpoint failure: %s\n", FIFO_FILE1, strerror(errno)); return 2; } printf("start open '%s' for read...\n", FIFO_FILE2); if( (fdr_fifo=open(FIFO_FILE2, O_RDONLY)) < 0 ) { printf("Open fifo[%s] for chat read endpoint failure: %s\n", FIFO_FILE2, strerror(errno)); return 2; } } printf("start chating with another program now, please input message now: \n"); while( !g_stop ) { FD_ZERO(&rdset); FD_SET(STDIN_FILENO, &rdset); FD_SET(fdr_fifo, &rdset); /* select多路复用监听标准输入和作为输入的命名管道读端 */ rv = select(fdr_fifo+1, &rdset, NULL, NULL, NULL); if( rv <= 0 ) { printf("Select get timeout or error: %s\n", strerror(errno)); continue; } /* 如果是作为输入的命名管道上有数据到来则从管道上读入数据并打印到标注输出上 */ if( FD_ISSET(fdr_fifo, &rdset) ) { memset(buf, 0, sizeof(buf)); rv=read(fdr_fifo, buf, sizeof(buf)); if( rv < 0) { printf("read data from FIFO get errorr: %s\n", strerror(errno)); break; } else if( 0==rv ) /* 如果从管道上读到字节数为0,说明管道的写端已关闭 */ { printf("Another side of FIFO get closed and program will exit now\n"); break; } printf("<-- %s", buf); } /* 如果标准输入上有数据到来,则从标准输入上读入数据后,将数据写入到作为输出的命名管道上给另外一个进程 */ if( FD_ISSET(STDIN_FILENO, &rdset) ) { memset(buf, 0, sizeof(buf)); fgets(buf, sizeof(buf), stdin); write(fdw_fifo, buf, strlen(buf)); } } /* 删除管道文件 */ unlink(FIFO_FILE1); unlink(FIFO_FILE2); }