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

  • 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;

这里写图片描述

这里写图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页