Linux--进程间通信(二)-命名管道(pipe)通信

76 篇文章 5 订阅
  • server.c
  • client.c
  • pipelib.h
  • 编译命令:gcc -o server server.c pipelib.h
  • 编译命令:gcc -o client client.c pipelib.h
  • 运行命令:./server
  • 运行命令:./client.c

server.c

#include "pipelib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {

        int server_fifo_fd;
        int client_fifo_fd;
        int res;
        char client_fifo_name[NAME_SIZE];
        message msg;
        char *p;
        if ( mkfifo(SERVER_FIFO_NAME, 0777) == -1) {//创建服务器从客户端接收消息的管道文件
                fprintf(stderr, "Sorry,create server fifo failure!\n" );
                return -1;
        }

        server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);//以读的方式打开服务器管道
        if (server_fifo_fd == -1) {
                fprintf(stderr, "Sorry server fifo open failure!\n" );
                return -1;
        }
        sleep(5);
        while ( res = read(server_fifo_fd, &msg, sizeof(msg)) > 0) {

                p = msg.data;
                while (*p){

                        *p = toupper(*p);/*转换为大写字母*/
                        ++p;
                }
                sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid);

                client_fifo_fd = open(CLIENT_FIFO_NAME, O_WRONLY);/*以写的方式打开客户端通道*/
                if (client_fifo_fd == -1){
                        fprintf(stderr, "Sorry,client fifo open failure!\n" );
                        return -1;
                }
                write(client_fifo_fd, &msg, sizeof(msg));/*将消息通过client的管道发送给客户端*/
                close(client_fifo_fd);
        }
        close(server_fifo_fd);
        unlink(SERVER_FIFO_NAME);
        exit(EXIT_SUCCESS);
}

client.c

#include "pipelib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){

        int server_fifo_fd;
        int client_fifo_fd;
        int res;
        char client_fifo_name[NAME_SIZE];
        message msg;
        msg.client_pid = getpid();
        sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid);/*将客户进程的pid写入结构体中*/

        if ( mkfifo(client_fifo_name, 0777) == -1) {/*创建向服务器发送消息的管道文件*/
                fprintf(stderr, "Sorry,create client fifo failure!\n" );
                return -1;
        }

        server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);/*以写的方式打开服务器通道*/
        if (server_fifo_fd == -1) {
                fprintf(stderr, "Sorry ,open server fifo failure!\n" );
                return -1;
        }
        sprintf(msg.data, "Hello from: %d",msg.client_pid);/*向服务器进程发送Hello from PID信息*/
        printf("%d sent %s\n",msg.client_pid, msg.data );
        write(server_fifo_fd, &msg, sizeof(msg));

        client_fifo_fd = open(client_fifo_name, O_RDONLY);/*以读的方式打开客户端通道*/
        if (client_fifo_fd == -1) {
                fprintf(stderr, "Sorry ,client fifo open failure!\n");
                return -1;
        }
        res = read(client_fifo_fd, &msg, sizeof(msg));/*从服务器接收数据*/
        if (res > 0) {
                printf("received:%s\n", msg.data);
        }
        close(client_fifo_fd);
        close(server_fifo_fd);
        unlink(client_fifo_name);
        return 0;

}

pipelib.h

#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>

#define SERVER_FIFO_NAME "/web/liefyuan/study/android/server_fifo"
#define CLIENT_FIFO_NAME "/web/liefyuan/study/android/client_fifo"
#define BUFFER_SIZE PIPE_BUF
#define MESSAGE_SIZE 256
#define NAME_SIZE 256

typedef struct message
{
        pid_t client_pid;
        char data[MESSAGE_SIZE + 1];
}message;

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值