c++ pipe实现父子进程通信

1、父子进程通信pipe编程流程

-创建管道

-设置进程的输出到管道

-创建进程

-关闭管道写句柄

-读管道读句柄,把数据读到一个buffer里

2、注意事项

-读管道数据的时候,一定要关闭写句柄;

-父子进程通信时,句柄的传递多通过继承来完成,父进程允许这些句柄为子进程继承;创建子进程,是否继承的属性要设置为true;

 // pdfprintconsole.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <Windows.h>
#include<tchar.h>
#include <string.h>

int main()
{

    int page_index = 0;
    char currentBuff[1000] = { 0 };
    char cachFileName[1000] = { 0 };
    char printCommand[1000] = { 0 };
    char command[500] = { 0 };
    HANDLE handle = 0;
    BOOL                bTest = 0;
    SECURITY_ATTRIBUTES sa = { 0 };
    DWORD               dwNumberOfBytesRead = 0;
    CHAR szBuffer[10000] = { 0 };
    DWORD ret = 0;
    HANDLE   hPipeOutputRead = NULL;
    HANDLE    hPipeOutputWrite = NULL;

    STARTUPINFOA si = { 0 };
    PROCESS_INFORMATION pi = { 0 };    

    sa.bInheritHandle = TRUE; // TRUE为管道可以被子进程所继承
    sa.lpSecurityDescriptor = NULL; // 默认为NULL
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    // Create pipe for standard output redirection.
    CreatePipe(&hPipeOutputRead,  // read handle
        &hPipeOutputWrite, // write handle
        &sa,      // security attributes
        0      // number of bytes reserved for pipe - 0 default
    );
    // Make child process use hPipeOutputWrite as standard out,
        // and make sure it does not show on screen.
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    //si.hStdInput = hPipeInputRead;
    si.hStdOutput = hPipeOutputWrite;
    si.hStdError = hPipeOutputWrite;
    //strcpy_s(command, " -printer \"FX DocuCentre S2011\" -paper 9 -printermargins C:\\Users\\QJ\\Desktop\\f3044688ce88a4b0a78c16ba85076570-5378-0010-0.png");
    strcpy_s(command," -printer \"FX DocuCentre S2011\" -listpapers");
    //一共执行三次
    for (int i = 0; i < 3; i++)
    {

        if (!CreateProcessA("C:\\Users\\QJ\\source\\repos\\WindowsFormsApp1\\x64\\Debug\\pdfprint.exe", command, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
        {
            //AfxMessageBox("缺失pdfprint.exe文件",0,0);
            break;
        }
        else
        {
            HANDLE hProcess = pi.hProcess;
            //等待进程退出    //CloseHandle(hPipeOutputRead);
            while (WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0);
            GetExitCodeProcess(hProcess, &ret);
            //如果ret!=0,异常退出;

            //
            CloseHandle(hPipeOutputWrite);
            while (true)
            {
                bTest = ReadFile(
                    hPipeOutputRead,      // handle of the read end of our pipe
                    &szBuffer,            // address of buffer that receives data
                    sizeof(szBuffer),                  // number of bytes to read
                    &dwNumberOfBytesRead, // address of number of bytes read
                    NULL                  // non-overlapped.
                );

                if (!bTest)
                {
                    break;
                }
                // do something with data.
                szBuffer[dwNumberOfBytesRead] = 0;  // null terminate
            }
            if (!ret)
            {
                printf("123%s456\nbtest:%d\n", szBuffer, bTest);
                CloseHandle(hProcess);
                CloseHandle(hPipeOutputRead);
                break;
            }

        }
    }
    //std::cout << "Hello World!\n";
    system("pause");
}

原文地址:https://www.cnblogs.com/freedomworld/p/11703447.html

时间: 2024-07-30 10:17:56

c++ pipe实现父子进程通信的相关文章

Java 父子进程通信

由父进程创建子进程,收发消息 package org.tango.process.parent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.tango.process.signal.Signal; import java.io.*; import java.lang.reflect.Field; import java.util.Map; impo

pipe实现兄弟进程通信

pipe实现进程间通信,首先关闭第一个子进程的读入端,然后关闭第二个子进程的写入端 #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { int fd[2]; pipe(fd); pid_t pid = fork(); if(pid==0) { close(fd[0]); write(fd[1],"Hello",6); exit(0); close(fd[1]); }

父子进程通过mmap进行通信

本来打算使用pipe进行父子进程之间的数据交互(应用场景是父进程向多个子进程分发数据,子进程进行处理):但是担心pipe的性能,转而使用mmap实现. 废话少叙,上代码. #include <stdio.h> #include <sys/types.h> #include <signal.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <

linux进程通信之管道

1.介绍: 1)同一主机: unix进程通信方式:无名管道,有名管道,信号 system v方式:信号量,消息队列,共享内存 2)网络通信:Socket,RPC 2.管道: 无名管道(PIPE):使用一个进程的标准输出作为另一个进程的标准输入建立的一个单向管道,执行完成后消失.主要用于父进程与子进程之间,或者两个兄弟进程之间.采用的是单向 1)创建无名管道:(#include(unistd.h)) extern int pipe(int pipes[2]),pipes[0]完成读操作,pipes

AF_UNIX域通信(基于socket和pipe的通信,只适于UNIX系统S&C同在一个主机上,用于进程通信)

服务器端: #include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include <sys/socket.h>#include <sys/un.h>#include <stddef.h>char buf[100];void main

父子进程共享内存通信的三种方法

1.  mmap MAP_ANONYMOUS 在支持MAP_ANONYMOUS的系统上,直接用匿名共享内存即可, 2. mmap  /dev/zero 有些系统不支持匿名内存映射,则可以使用fopen打开/dev/zero文件,然后对该文件进行映射,可以同样达到匿名内存映射的效果. 3. shmget shmat shmctl shmget 是老式的system V 共享内存模式,很多系统都支持这种方法. 父子进程共享内存通信的三种方法

第七课 进程通信

unix_c_07.txt================第七课 进程通信================一.基本概念------------1. 何为进程间通信~~~~~~~~~~~~~~~~~进程间通信(Interprocess Communication, IPC)是指两个,或多个进程之间进行数据交换的过程.2. 进程间通信分类~~~~~~~~~~~~~~~~~1) 简单进程间通信:命令行参数.环境变量.信号.文件.2) 传统进程间通信:管道(fifo/pipe).3) XSI进程间通信:

进程通信(一)—— 管道

父子进程可以通过管道进行数据交互,一个管道只能有一个数据流向,要实现双工通信,可以使用两个管道实现. 管道工作原理: 向内核申请管道描述符 父子进程fork()后均有该管道资源,但处于不同内存地址 通过对描述符读写实现通信 数据交互图: 单工通信代码实现: 1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <iostream> 5 6 using names

进程通信和线程通信

Linux系统中的线程通信方式主要以下几种: *  锁机制:包括互斥锁.条件变量.读写锁 进程通信: 管道(PIPE):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系(父子进程)的进程间使用.另外管道传送的是无格式的字节流,并且管道缓冲区的大小是有限的(管道缓冲区存在于内存中,在管道创建时,为缓冲区分配一个页面大小). 有名管道 (FIFO): 有名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信. 信号(Signal): 信号是一种比较复杂的通信方式,用于通知接