命令管道的理解

Posted 做个奇怪的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了命令管道的理解相关的知识,希望对你有一定的参考价值。

命令管道的理解

管道是一种简单的进程间通讯(IPC),命名管道可在同一台计算机的不同进程之间或在跨越一个网络的不同计算机的不同进程之间,支持可靠的、单向或双向的数据通信。

  • 简单的实现流程图,这是一个单向的,双管道结构, 以模块A先创建堵塞,后连接,模块B先连接,后创建堵塞,来形成两条单向的交互管道, 优点是,读写分离,不用去分辨个管道里的数据哪些是读的哪些是写的。

理解了上图,就有一个大概的思路了,下面是根据dr_ipc.h库实现的简单例子,一个简单的例子,仅供参考,下面实现了简单的交互,具体实现请查对应的API

  • 模块A

    #define DR_IPC_IMPLEMENTATION
    #include "dr_ipc.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    	string swrite_pipe_name =  "write123";
    	string sread_pipe_name =  "read123";
    
    	string log_file_tmp = "";
    
    	drpipe _write_pipe = nullptr;
    
    	cout << "wait .. DR_IPC_WRITE  write123" << endl;
    	//创建管道,堵塞等待链接
    	dripc_result result = drpipe_open_named_server(swrite_pipe_name.c_str(), DR_IPC_WRITE, &_write_pipe);
    	if (result != dripc_result_success)
    	{
    		cout << "error drpipe_open_named_server" << endl;
    		Sleep(1000000);
    	}
    	else
    	{
    		cout << "2" << endl;
    
    		dripc_result result = dripc_result_unknown_error;
    		int nCount = 1;
    		drpipe _read_pipe = NULL;
    
    		result = drpipe_open_named_client(sread_pipe_name.c_str(), DR_IPC_READ, &_read_pipe);//连接管道
    		if (result != dripc_result_success)
    		{
    			cout << "error drpipe_open_named_client" << endl;
    			Sleep(1000000);
    		}
    
    		string sParam = "123";
    		long lBytesWrite = 0;
    		dripc_result rc = drpipe_write(_write_pipe, sParam.c_str(), sParam.size(), (size_t*)&lBytesWrite);
    		if (dripc_result_success != rc)
    		{
    			cout << "error drpipe_write" << endl;
    			Sleep(1000000);
    		}
    
    		size_t pBytesRead = 0;
    #define PIPE_MSG_MAX_SIZE (1024*1024*10)
    		char* _msgbuf = new char[PIPE_MSG_MAX_SIZE];
    		memset(_msgbuf, 0, PIPE_MSG_MAX_SIZE);
    		rc = drpipe_read(_read_pipe, _msgbuf, PIPE_MSG_MAX_SIZE, (size_t*)&pBytesRead);
    		if (dripc_result_success == rc)
    		{
    			cout << "succeed" << endl;
    			cout << "sParam:" <<_msgbuf << endl;
    		}
    
    		delete[]_msgbuf;
    		drpipe_close(_write_pipe);
    		drpipe_close(_read_pipe);
    		Sleep(1000000);
    	}
    }
    
  • 模块B

    
    #define DR_IPC_IMPLEMENTATION
    #include "..\\dr_ipc.h"
    #include <string>
    
    #include <iostream>
    using namespace std;
    int main()
    {
    	drpipe pReadPipe = nullptr;
    	drpipe pWritePipe = nullptr;
    	//连接管道
    	cout << "connect pipe:" << endl;
    	dripc_result result = drpipe_open_named_client("write123", DR_IPC_READ, &pReadPipe);
    	if (dripc_result_success != result)
    	{
    		cout << "pipe connect fail. read pipename:" << "write123";
    		Sleep(1000000);
    	}
    	cout << "connect pipe success" << endl;
    
    	//创建写管道 会卡住等待连接
    	result = drpipe_open_named_server("read123", DR_IPC_WRITE, &pWritePipe);
    	if (dripc_result_success != result)
    	{
    		cout << "pipe connect fail. write pipename:" << "read123"<< endl;
    		drpipe_close(pReadPipe);
    		pReadPipe = nullptr;
    		Sleep(1000000);
    	}
    
    	//反复从读管道中获取数据,诊断,结果写入写管道
    
    #define RECV_BUF_MAX_SIZE	(10*1024*1024)
    	char* pszrecvbuf = new char[RECV_BUF_MAX_SIZE];
    	memset(pszrecvbuf, 0, RECV_BUF_MAX_SIZE);
    
    	while (true)
    	{
    		long lReadSize = 0;
    		cout << "ready to read msg from pipe" << endl;
    		dripc_result rc = drpipe_read(pReadPipe, pszrecvbuf, RECV_BUF_MAX_SIZE, (size_t*)&lReadSize);
    		if (dripc_result_success != rc)
    		{
    			cout << "read pipe msg fail.  error code:" << rc << endl;
    			break;
    		}
    		cout << "read msg from pipe:" << pszrecvbuf << endl;
    
    		string sResult = "finsih";
    		sResult.assign(pszrecvbuf);
    		cout << "Result:" << sResult << endl;
    		//反回结果
    		long lBytesWrite = 0;
    		rc = drpipe_write(pWritePipe, sResult.c_str(), sResult.size(), (size_t*)&lBytesWrite);
    		if (dripc_result_success != rc)
    		{
    			cout << "write msg to pipe fail. error code:" << rc << endl;
    			break;
    		}
    		cout << "finish" << endl;
    	}
    	delete[]pszrecvbuf;
    	drpipe_close(pReadPipe);
    	drpipe_close(pWritePipe);
    
    	Sleep(1000000);
    }
    
  • 执行结果

    模块A
    wait .. DR_IPC_WRITE  write123
    2
    succeed
    sParam:123
    
    模块B
    connect pipe:
    connect pipe success
    ready to read msg from pipe
    read msg from pipe:123
    diag Result:123
    diag finish
    ready to read msg from pipe
    read pipe msg fail.  error code:1
    

以上是关于命令管道的理解的主要内容,如果未能解决你的问题,请参考以下文章

[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础

理解Powershell管道

管道和xargs的理解

r 计算管道的步骤(基本片段)

15种Python片段去优化你的数据科学管道

死磕 Redis----- 理解 pipeline 管道