前言一、思维导图二、例程
1.代码2.日志文件案例
前言
本文记录的是进程通信间的有名管道
一、思维导图 二、例程 1.代码
一个文件是写内容到有名管道,另外一个文件是读内容到有名管道
代码如下(示例):
mkfifo_write.c
#include#include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" int main(int argc, char const *argv[]) { const char buf[20] = "hello linux!"; //创建有名管道 if( access(FIFO_PATH, F_OK) ) { if( mkfifo(FIFO_PATH, 0666) < 0) { perror("mkfifo error"); return -1; } else { printf("创建有名管道成功!n"); } } //打开有名管道 int fd_fifo = open(FIFO_PATH, O_WRONLY); if(fd_fifo < 0) { perror("open fifo error"); return -1; } //写数据到有名管道 ssize_t ret = write (fd_fifo, buf, strlen(buf)); if(ret > 0) { printf("成功写入%ld个字节n", ret); } close(fd_fifo); return 0; }
mkfifo_read.c
代码如下(示例):
#include#include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" int main(int argc, char const *argv[]) { char buf[20] = {0}; //创建有名管道 if( access(FIFO_PATH, F_OK) ) { if( mkfifo(FIFO_PATH, 0666) < 0) { perror("mkfifo error"); return -1; } } //打开有名管道 int fd_fifo = open(FIFO_PATH, O_RDONLY); if(fd_fifo < 0) { perror("open fifo error"); return -1; } //从有名管道读数据 ssize_t ret = read(fd_fifo, buf, sizeof(buf)); if(ret > 0) { printf("成功读取%ld个字节n", ret); printf("内容是:%sn", buf); } close(fd_fifo); return 0; }
结果如图所示:
思路:
服务器通过读取有名管道文件的内容,然后写入到日志文件;
客户端通过创建父子进程,然后不断往有名管道更新内容
代码如下(示例):
mkfifo_server.c
#include#include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" #define JOURNAL_PATH "./journal.txt" int main(int argc, char const *argv[]) { char msg_buf[150] = {0}; //创建有名管道 if( access(FIFO_PATH, F_OK) ) { if( mkfifo(FIFO_PATH, 0666) < 0) { perror("mkfifo error"); return -1; } else { printf("创建有名管道成功!n"); } } //打开有名管道 int fd_fifo = open(FIFO_PATH, O_RDONLY); if(fd_fifo < 0) { perror("open fifo error"); return -1; } //打开日志文件 int fd_file = open(JOURNAL_PATH, O_RDWR | O_CREAT, 0666); if(fd_file < 0) { perror("open file error"); return -1; } //把有名管道的内容写入到日志文件 while(1) { read(fd_fifo, msg_buf, sizeof(msg_buf)); write (fd_file, msg_buf, strlen(msg_buf)); memset(msg_buf, 0, sizeof(msg_buf)); } //关闭文件 close(fd_fifo); close(fd_file); return 0; }
mkfifo_client.c
代码如下(示例):
#include#include #include #include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" #define FATHER_INFO "father" #define SON_INFO "son" int main(int argc, char const *argv[]) { char msg_buf[150] = {0}; int status; //创建有名管道 if( access(FIFO_PATH, F_OK) ) { if( mkfifo(FIFO_PATH, 0666) < 0) { perror("mkfifo error"); return -1; } else { printf("创建有名管道成功!n"); } } //打开有名管道 int fd_fifo = open(FIFO_PATH, O_RDWR); if(fd_fifo < 0) { perror("open fifo error"); return -1; } //创建进程(父子进程分别往有名管道写数据) pid_t pid = fork(); if(pid > 0) { //父进程 printf("我是父进程,ID:%dn", getpid()); time_t tloc; while(1) { time(&tloc); memset(msg_buf, 0, sizeof(msg_buf)); sprintf(msg_buf, "pid:%dt info:%st time:%sn", getpid(), FATHER_INFO, ctime(&tloc)); write (fd_fifo, msg_buf, strlen(msg_buf)); sleep(5); } wait(&status); } else if(0 == pid) { //子进程 printf("我是子进程,ID:%dn", getpid()); time_t tloc; while(1) { time(&tloc); memset(msg_buf, 0, sizeof(msg_buf)); sprintf(msg_buf, "pid:%dt info:%st time:%sn", getpid(), SON_INFO, ctime(&tloc)); write (fd_fifo, msg_buf, strlen(msg_buf)); sleep(5); } } //关闭文件 close(fd_fifo); return 0; }
结果如图所示:



