#include <unistd.h> 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define INPUT 0 
#define OUTPUT 1 

int main() 
{ 
	int file_descriptors[2]; 
	/*定义子进程号 */ 
	pid_t pid; 
	char buf[256]; 
	int returned_count; 
	/*创建无名管道*/ 
	pipe(file_descriptors); 
	/*创建子进程*/ 
	if((pid = fork()) == -1) 
	{ 
		printf("Error in fork\n"); 
		exit(1); 
	} 
	/*执行子进程*/ 
	if(pid == 0) 
	{ 
		printf("in the spawned (child) process...\n"); 
		/*子进程向父进程写数据,关闭管道的读端*/ 
		close(file_descriptors[INPUT]); 
		write(file_descriptors[OUTPUT], "test data", strlen("test data")); 
		exit(0); 
	} 
	else 
	{ 
		/*执行父进程*/ 
		printf("in the spawning (parent) process...\n"); 
		/*父进程从管道读取子进程写的数据,关闭管道的写端*/ 
		close(file_descriptors[OUTPUT]); 
		returned_count = read(file_descriptors[INPUT], buf, sizeof(buf)); 
		printf("%d bytes of data received from spawned process: %s\n", 
		returned_count, buf); 
	} 
 } 



#include "stdafx.h"

#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
    HANDLE hStdin, hStdout;
    char buff[256] = {0};
    char* QUIT = "q";
    DWORD dwRead;
    DWORD dwWrite;
    hStdin = GetStdHandle(STD_INPUT_HANDLE);
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
    {
        return 1;
    }
    cout<<"client is working!"<<endl;
    while(1)
    {
        if(ReadFile(hStdin, buff, 256, &dwRead, NULL) && buff[0] != 0)
        {
            if(strcmp(buff,QUIT) == 0)
                return 0;
            else
            {
                cout<<buff<<endl;
                memset(buff, 0, sizeof(buff));
            }
             
        }
         
        Sleep(500);
    }
    cout<<"client will shutdown in 3 sec!"<<endl;
    Sleep(3000);
    return 0;
}



// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <Windows.h>
#include <iostream>
 
using namespace std;

HANDLE hReadPipe;
HANDLE hWritePipe;

int main()
{
    
    wchar_t * pstrErr;
    char mzBuff[256] = {0};
    char *pstrQuit = "q";

    //设置得到的句柄可以被子进程继承,此项设置为匿名管道的关键之处.windwos下无linux的fork,此处只能通过如此设置来达到父子进程均识别管道的句柄
    SECURITY_ATTRIBUTES secuAttr;//具体参见http://msdn.microsoft.com/zh-cn/library/aa379560(v=vs.85).aspx
    secuAttr.nLength = sizeof(SECURITY_ATTRIBUTES); //该字段无需理解,都设置成这样既可
    secuAttr.bInheritHandle = TRUE;//该属性表明得到的HANDLE是否被新创建的进程继承
    secuAttr.lpSecurityDescriptor = NULL; //安全属性结构体指针
    
    // 创建匿名管道
    if(!CreatePipe(&hReadPipe, &hWritePipe, &secuAttr, 1024))
    {
        cout<<"can not create pipe!"<<endl;
        return 0;
    }
    // 设置系统内核对象继承属性
    if(SetHandleInformation(&hWritePipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
    {
        cout<<"set handle property error!"<<endl;
        DWORD d = GetLastError();
        return 0;
    }

    // 创建子进程
    wchar_t mzAppName[] = TEXT("ConsoleApplication1.exe"); 
    STARTUPINFO startinfo;//具体参见http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx
    memset(&startinfo, 0, sizeof(STARTUPINFO));
    startinfo.cb = sizeof(STARTUPINFO);  //该字段无需理解,都设置成这样既可
    startinfo.hStdInput = hReadPipe;//指定进程的输入句柄
    startinfo.hStdOutput = GetStdHandle(STARTF_USESTDHANDLES);
    startinfo.dwFlags |= STARTF_USESTDHANDLES;//该字段表明目标进程创建窗体时,使用该结构体中的那些附加信息
    PROCESS_INFORMATION procinfo;
    if (!CreateProcess(mzAppName, NULL, NULL, NULL, TRUE,CREATE_NEW_CONSOLE, NULL,NULL, &startinfo, &procinfo))
    {
        cout<<"can't create process:"<<mzAppName<<endl;
        DWORD d = GetLastError();
        return 0;
    }

    cout<<"press Q to quit, others to show!"<<endl;
    DWORD dwWT;
    while (cin>>mzBuff)
    {
        if (strcmp(mzBuff, pstrQuit) == 0)
        {
            cout<<"proc exit!"<<endl;
            return 0;
        }
        while (mzBuff[0]!= '\0')
        {
            if(!WriteFile(hWritePipe, mzBuff, sizeof(mzBuff), &dwWT, NULL))
            {
                cout<<"write file error!"<<endl;
                break;
            }
            memset(mzBuff, 0, sizeof(mzBuff));
        } 
    }
    CloseHandle(procinfo.hProcess);
    CloseHandle(procinfo.hThread);
 
    CloseHandle(hReadPipe);
    CloseHandle(hWritePipe);
 
    return 0;
 }

 

  

posted on 2013-01-16 20:21  BLoodMaster  阅读(212)  评论(0编辑  收藏  举报