利用FIFO通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
  
#define MAXLINE 4096
#define BUFFSIZE 4096
 
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
  
void client(int, int), server(int, int);
  
int main(int argc, char *argv[]) {
    int readfd, writefd;
    pid_t childpid;
 
    if((mkfifo(FIFO1, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)<0)&&(errno!=EEXIST)) {
        fprintf(stderr, "can't create %s\n", FIFO1);
        exit(-1);
    }
 
    if((mkfifo(FIFO2, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)<0)&&(errno!=EEXIST)) {
        unlink(FIFO1);
        fprintf(stderr, "cant't create %s\n", FIFO2);
    }
 
    if((childpid=fork()) < 0) {
        perror("fork error");
        exit(-1);
    } else if(childpid == 0) {
        if((readfd=open(FIFO1, O_RDONLY, 0))<0) {
            fprintf(stderr, "open %s error.\n", FIFO1);
            exit(-1);
        }
        if((writefd=open(FIFO2, O_WRONLY, 0))<0) {
            fprintf(stderr, "open %s error.\n", FIFO2);
            exit(-1);
        }
        server(readfd, writefd);
        exit(0);
    }
 
    if((writefd=open(FIFO1, O_WRONLY, 0))<0) {
        fprintf(stderr, "open %s error.\n", FIFO1);
        exit(-1);
    }
    if((readfd=open(FIFO2, O_RDONLY, 0))<0) {
        fprintf(stderr, "open %s error.\n", FIFO2);
        exit(-1);
    }
  
    client(readfd, writefd);
  
    if((waitpid(childpid, NULL, 0)) == -1) {
        perror("waitpid error");
        exit(-1);
    }
 
    if(close(readfd)==-1) {
        fprintf(stderr, "close readfd error.\n");
        exit(-1);
    }
    if(close(writefd)==-1) {
        fprintf(stderr, "close writefd error.\n");
        exit(-1);
    }
 
    if(unlink(FIFO1)<0) {
        fprintf(stderr, "delete %s error.\n", FIFO1);
        exit(-1);
    }
    if(unlink(FIFO2)<0) {
        fprintf(stderr, "delete %s error.\n", FIFO2);
        exit(-1);
    }
 
    exit(0);
}
  
void client(int readfd, int writefd) {
    size_t len;
    ssize_t n;
    char buff[MAXLINE];
  
    printf("-------hello-----starting client------------\n");
  
    if((fgets(buff, MAXLINE, stdin) == NULL) && ferror(stdin)) {
        perror("fgets error");
        exit(-1);
    }
  
    printf("client ipc pathname: %s\n", buff);
  
    len = strlen(buff);
    if(buff[len-1] == '\n')
        len--;
  
    if(write(writefd, buff, len) != len) {
        perror("write error");
        exit(-1);
    }
  
    while((n=read(readfd, buff, MAXLINE)) > 0) {
        printf("-----------------print to film----------------\n");
        if (write(STDOUT_FILENO, buff, n) != n ) {
            perror("write error");
            exit(-1);
        }
    }
}
  
void server(int readfd, int writefd) {
    int fd;
    ssize_t n;
    char buff[MAXLINE+1];
  
    printf("----------------start server-------------------\n");
  
    if((n=read(readfd, buff, MAXLINE)) == -1) {
        perror("read error");
        exit(-1);
    }
    if(n == 0) {
        perror("end-of-file while reading pathname");
        exit(-1);
    }
    buff[n]='\0';
  
    printf("server IPC pathname: %s\n",buff);
  
    if((fd=open(buff, O_RDONLY)) < 0) {
        snprintf(buff+n, sizeof(buff)-n, ": can't open, %s\n", strerror(errno));
        n=strlen(buff);
        if(write(writefd, buff, n) != n) {
            perror("write error");
            exit(-1);
        }
    }
    else {
        printf("------server open file success-------\n");
        while((n=read(fd, buff, MAXLINE)) > 0) {
            if(write(writefd, buff, n) != n) {
                perror("write error");
                exit(-1);
            }
        }
        close(fd);
    }
}

 

posted @   东宫得臣  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-10-04 Principal Component Analysis ---- PRML读书笔记
点击右上角即可分享
微信分享提示