Linux--命名函数实现

Makefile:

cpp 复制代码
.PHONY:all
all:client mutiServer

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

.PHONY:clean
clean:
	rm -f client mutiServer

server.cc

cpp 复制代码
#include "comm.hpp"
#include <sys/wait.h>

static void getMessage(int fd)
{
    char buffer[SIZE];
    while(true)
    {
        memset(buffer,'\0',sizeof (buffer));
        ssize_t s=read(fd,buffer,sizeof(buffer)-1);
        if(s>0)
        {
            cout<<"["<<getpid()<<"] "<<"client say> "<<buffer<<endl;
        }
        else if(s==0)
        {
            //end of file
            cerr<<"["<<getpid()<<"] "<<"read end of file, client quit, server quit too!"<<endl;
            break;
        }
        else
        {
            //read error
            perror("read");
            break;
        }
    }
}

int main()
{
    //1.创建管道文件
    if(mkfifo(ipcPath.c_str(),MODE)<0)
    {
        perror("mkfifo");
        exit(1);
    }

    Log("创建管道文件成功",Debug)<<" step 1 "<<endl;

    //2.打开文件
    int fd=open(ipcPath.c_str(),O_RDONLY);
    if(fd<0)
    {
        perror("open");
        exit(2);
    }

    Log("打开管道文件成功",Debug)<<" step 2 "<<endl;

    int nums=3;
    for(int i=0;i<nums;i++)
    {
        pid_t id=fork();

        if(id==0)
        {
            //3.编写正常的通信代码
            getMessage(fd);
            exit(1);
        }
    }

    for(int i=0;i<nums;i++)
    {
        waitpid(-1,nullptr,0);
    }
   
    //4.关闭文件
    close(fd);

    Log("关闭管道文件成功",Debug)<<" step 3 "<<endl;

    unlink(ipcPath.c_str());//通信完毕

    Log("删除管道文件成功",Debug)<<" step 4 "<<endl;

    return 0;
}

client.cc

cpp 复制代码
#include "comm.hpp"

int main()
{
    //1.获取管道
    int fd=open(ipcPath.c_str(),O_WRONLY);
    if(fd<0)
    {
        perror("open");
        exit(1);
    }
    
    //2.ipc过程
    string buffer;
    while(true)
    {
        cout<<"please enter your message line:>";
        getline(cin,buffer);
        write(fd,buffer.c_str(),buffer.size());
    }

    //3.关闭管道文件
    close(fd);
    return 0;
}

comm.hpp

cpp 复制代码
#ifndef _COMM_H_
#define _COMM_H_

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include "Log.hpp"

using namespace std;

#define MODE 0666
#define SIZE 128
string ipcPath = "./fifo.ipc";

#endif

Log.hpp

cpp 复制代码
#ifndef _LOG_H
#define _LOG_H

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

#define Debug 0
#define Notice 1
#define Warning 2
#define Error 3

const string msg[]={
    "Debug",
    "Notice",
    "Warning",
    "Error"
};



ostream &Log(string message,int level)
{
    cout<<" | "<<(unsigned)time(NULL)<<" | "<<msg[level]<<" | "<<message;
    return cout;
}

#endif
相关推荐
海阔天空任鸟飞~2 小时前
Linux 权限 777
linux·运维·服务器
IT瑞先生3 小时前
docker-compose下快速部署实操——持续更新...
运维·docker·容器
无锡银洲自动化4 小时前
工业模拟量采集痛点分析:IDCB-4E/DR/Y测量前端解决方案
运维·产品运营
Kina_C5 小时前
Linux iptables 防火墙原理与实操——从四表五链到 NAT 配置
linux·运维·服务器·iptables
A洛5 小时前
Codex 实战:一句话完成 Temu 商品图采集与印花抠图自动化
运维·人工智能·自动化·codex
wdfk_prog5 小时前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构
linux·开发语言·面试·架构
Urbano5 小时前
服装工厂卫衣量产提质增效方案:工序自动化替代、产能优化、多品牌设备选型及落地案例
运维·自动化
GlobalSign数字证书6 小时前
自动化时代,如何高效部署SSL证书?
运维·自动化·ssl
大侠锅锅8 小时前
第 19 篇:远程运维体系——指标、SSH、日志与 OTA 升级
运维·ssh
灵机一物9 小时前
企业选型参考:2026 CXL 内存扩展方案六强测评与落地指南
服务器·网络·数据库·人工智能