使用两个管道的客户----服务器模型

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
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
 
#define MAXLINE 4096
#define BUFFSIZE 4096
 
void client(int, int), server(int, int);
 
int main(int argc, char *argv[]) {
    int pipe1[2],pipe2[2];
    pid_t childpid;
 
    if(pipe(pipe1) < 0) {
        perror("pipe1 error");
        exit(-1);
    }
 
    if(pipe(pipe2) < 0) {
        perror("pipe2 error");
        exit(-1);
    }
 
    if((childpid=fork()) < 0) {
        perror("fork error");
        exit(-1);
    }
    else if(childpid == 0) {
        if(close(pipe1[1]) == -1) {
            perror("close pipe1 error");
            exit(-1);
        }
        if(close(pipe2[0]) == -1) {
            perror("close pipe2 error");
            exit(-1);
        }
 
        server(pipe1[0], pipe2[1]);
        exit(0);
    }
 
    if(close(pipe1[0]) == -1) {
        perror("parent close pipe1 error");
        exit(-1);
    }
 
    if(close(pipe2[1]) == -1) {
        perror("parent close pipe2 error");
        exit(-1);
    }
 
    client(pipe2[0], pipe1[1]);
 
    if((waitpid(childpid, NULL, 0)) == -1) {
        perror("waitpid error");
        exit(-1);
    }
 
    exit(0);
}
 
void client(int readfd, int writefd) {
    size_t len;
    ssize_t n;
    char buff[MAXLINE];
 
    printf("------------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);
    }
}

pipe函数返回两个文件描述符:fd[0]fd[1]。前者打开来读,后者打开来写。

当需要一个双向数据流时,必须创建两个管道,每个方向一个。

1)创建管道1(pipe1[0]pipe1[1])和管道2(pipe2[0]和pipe2[1]);

2)fork

3)父进程关闭管道1的读出端(pipe1[0]);

4)父进程关闭管道2的写入端(pipe2[1]);

5)子进程关闭管道1的写入端(pipe1[1]);

6)子进程关闭管道2的读出端(pipe2[0]);

管道没有名字,因此它们最大的劣势是只能用于有一个共同祖先进程的各个进程之间。

posted @   东宫得臣  阅读(191)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示