Linux-实现没有血缘关系的进程之间的通信

目录

一.makefile的编写        

二.comm.hpp头文件的编写

三.serve.cc文件的编写

四.client.cc文件的编写


一.makefile的编写        

.PHONY:all
all:serve client

serve : serve.cc
	g++ -o $@ $^ -g -std=c++11
client : client.cc
	g++ -o $@ $^ -g -std=c++11

.PHONY:clean
clean:
	rm -rf serve client

二.comm.hpp头文件的编写

#pragma once

#include <cstdio>
#include <cerrno>
#include <string>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

using namespace std;

#define SIZE      1024
#define MODE      0664
#define FIFO_FILE "./myfifo"


enum
{
    FIFO_CREATE_ERR = 1,
    FIFO_DELETE_ERR,
    FIFO_OPEN_ERR
};

三.serve.cc文件的编写

#include "comm.hpp"

int main(void)
{
    //创建命名管道文件
    int n = 0;
    n = mkfifo(FIFO_FILE, MODE);
    if(-1 == n)
    {
        perror("mkfifo");
        exit(FIFO_CREATE_ERR);
    }

    //以读的方式打开管道文件
    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        exit(FIFO_OPEN_ERR);
    }
    cout << "serve open file done" << endl;

    //进行没有血缘关系的进程之间的通信
    while(true)
    {
        char buffer[SIZE];

        int read_number = 0;
        read_number = read(fd, buffer, sizeof(buffer));
        if (read_number > 0)
        {
            buffer[read_number] = 0;
            cout << "client say# " << buffer << endl;
        }
        else if (read_number == 0)
        {
            printf("client quit, me too !");
            break;
        }
        else
            break;

    }

    //关闭命名管道文件
    close(fd);

    //删除管道文件
    n = unlink(FIFO_FILE);
    if(-1 == n)
    {
        perror("unlink");
        exit(FIFO_DELETE_ERR);
    }
    return  0;
}

四.client.cc文件的编写

#include "comm.hpp"

int main(void)
{
    //以写的方式打开命名管道文件
    int fd = open(FIFO_FILE, O_WRONLY);
    if(fd < 0)
    {
        perror("open");
        exit(FIFO_OPEN_ERR);
    }
    cout << "client open file done" << endl;

    //进行没有血缘关系的进程之间的通信
    string client_str;
    while(true)
    {
        cout << "Please Enter@ ";
        getline(cin, client_str);

        write(fd, client_str.c_str(), client_str.size());
    }

    //关闭命名管道文件
    close(fd);

    return  0;
}

相关推荐

  1. Linux-实现没有血缘关系进程之间通信

    2023-12-11 01:58:03       61 阅读
  2. DPDK多进程之间通信

    2023-12-11 01:58:03       66 阅读
  3. 0711,0712,0713 进程进程之间通信

    2023-12-11 01:58:03       18 阅读
  4. 探索利用 LineageLogger 获取hive字段级血缘关系

    2023-12-11 01:58:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 01:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 01:58:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 01:58:03       82 阅读
  4. Python语言-面向对象

    2023-12-11 01:58:03       91 阅读

热门阅读

  1. 【力扣】160.相交链表

    2023-12-11 01:58:03       54 阅读
  2. 关于 UbuntuServer 的一些配置

    2023-12-11 01:58:03       54 阅读
  3. SpringBootAdmin设置邮件通知

    2023-12-11 01:58:03       52 阅读
  4. 顺序表的应用

    2023-12-11 01:58:03       56 阅读
  5. 力扣119双周赛

    2023-12-11 01:58:03       63 阅读
  6. 力扣面试150题 | 轮转数组

    2023-12-11 01:58:03       63 阅读
  7. 智能化缺陷检测系统的发展趋势

    2023-12-11 01:58:03       70 阅读
  8. Android 13 - Media框架(22)- ACodecBufferChannel

    2023-12-11 01:58:03       46 阅读
  9. LeetCode 2048. 下一个更大的数值平衡数

    2023-12-11 01:58:03       62 阅读
  10. Linux结束程序运行的命令

    2023-12-11 01:58:03       54 阅读