test_pipe 分析 无名管道

1.类图

写的比较简单,没有涉及到eventpoller

 2.代码摘要

/**
 * 子进程通过pipe发送数据,父进程通过pipe读数据,一共写了10次数据
 * 父进程创建pipe,在执行完fork后,子进程可以共用父进程创建的pipe
 * pipe读:目前是父进程,接收(读)pipe数据的回调函数onRead在创建pipe时设置;
 * pipe写:目前是子进程发送,给pipe发送数据通过pipe.send函数
 * 子进程-写-fd[1]=====内核(管道)===父进程-读-fd[0]
 * @return
 */
int main() {
    //设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
#if defined(_WIN32)
    ErrorL << "该测试程序不能再windows下运行,因为我不会windows下的多进程编程,但是管道模块是可以在windows下正常工作的。" << endl;
#else
    //获取父进程的PID
    auto parentPid = getpid();
    InfoL << "parent pid:" << parentPid << endl;

    //定义一个管道,lambada类型的参数是管道收到数据的回调
    Pipe pipe([](int size,const char *buf) {
        //该管道有数据可读了
        InfoL << getpid() << " recv:" << buf;
    });

    //创建子进程
    auto pid = fork();

    if (pid == 0) {
        //子进程
        int i = 10;
        while (i--) {
            //在子进程每隔一秒把数据写入管道,共计发送10次
            sleep(1);
            string msg = StrPrinter << "message " << i << " form subprocess:" << getpid();
            DebugL << "子进程发送:" << msg << endl;
            pipe.send(msg.data(), msg.size());
        }
        DebugL << "子进程退出" << endl;
    } else {
        //父进程设置退出信号处理函数
        static semaphore sem;
        signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
        sem.wait();

        InfoL << "父进程退出" << endl;
    }
#endif // defined(_WIN32)

    return 0;
}

/**
 * console output:
 * /work/__code/__202311141609_202312281011_1_bak/__202311141609_bak/__202311141609/ZLToolKit/cmake-build-debug/bin/test_pipe
2024-01-17 10:18:43.182 I [test_pipe] [7458-test_pipe] test_pipe.cpp:28 main | parent pid:7458
2024-01-17 10:18:43.183 D [test_pipe] [7458-stamp thread] util.cpp:367 operator() | Stamp thread started
2024-01-17 10:18:43.185 I [test_pipe] [7458-test_pipe] EventPoller.cpp:485 EventPollerPool | EventPoller created size: 6
2024-01-17 10:18:44.188 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 9 form subprocess:7468
2024-01-17 10:18:44.188 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 9 form subprocess:7468
2024-01-17 10:18:45.189 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 8 form subprocess:7468
2024-01-17 10:18:45.190 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 8 form subprocess:7468
2024-01-17 10:18:46.191 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 7 form subprocess:7468
2024-01-17 10:18:46.191 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 7 form subprocess:7468
2024-01-17 10:18:47.191 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 6 form subprocess:7468
2024-01-17 10:18:47.191 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 6 form subprocess:7468
2024-01-17 10:18:48.191 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 5 form subprocess:7468
2024-01-17 10:18:48.191 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 5 form subprocess:7468
2024-01-17 10:18:49.192 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 4 form subprocess:7468
2024-01-17 10:18:49.192 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 4 form subprocess:7468
2024-01-17 10:18:50.192 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 3 form subprocess:7468
2024-01-17 10:18:50.192 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 3 form subprocess:7468
2024-01-17 10:18:51.193 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 2 form subprocess:7468
2024-01-17 10:18:51.194 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 2 form subprocess:7468
2024-01-17 10:18:52.195 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 1 form subprocess:7468
2024-01-17 10:18:52.195 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 1 form subprocess:7468
2024-01-17 10:18:53.195 D [test_pipe] [7468-test_pipe] test_pipe.cpp:46 main | 子进程发送:message 0 form subprocess:7468
2024-01-17 10:18:53.195 D [test_pipe] [7468-test_pipe] test_pipe.cpp:49 main | 子进程退出
2024-01-17 10:18:53.195 I [test_pipe] [7458-event poller 0] test_pipe.cpp:33 operator() | 7458 recv:message 0 form subprocess:7468
2024-01-17 10:18:53.195 I [test_pipe] [7468-test_pipe] EventPoller.cpp:103 ~EventPoller | 0x56317f719780
2024-01-17 10:19:06.858 W [test_pipe] [7458-stamp thread] util.cpp:394 operator() | Stamp expired is abnormal: 5574479

 */

 3.示意图

 4.参考文章:

https://github.com/ZLMediaKit/ZLToolKit

《ZLToolKit源码学习笔记》(13)事件轮询模块之管道的简单封装_zltoolkit work-CSDN博客

 

posted @ 2024-01-17 16:31  OzTaking  阅读(3)  评论(0编辑  收藏  举报