我正在开展一个学校项目,我正试图在StdOut上展示一个儿童过程,这个过程是在父母的StdIn中写的;反之亦然,即显示在父StdOut上的子进程StdIn上写的内容,但是我使用ReadFile和WriteFile操作达成了死锁 .

从我在研究这个主题时可以收集的内容来看,这是使用同步管道时的常见问题 .

管道操作的读写是否应该由事件同步?你有其他方法吗?任何建议将不胜感激,提前谢谢 .

Parent.cpp

   #include <windows.h>
   #include <iostream>
   #include <stdio.h>

   //read handle pipe1
   HANDLE r1 = NULL;

   //write handle pip1
   HANDLE w1 = NULL;

   //read handle pipe2
   HANDLE r2 = NULL;

   //write handle for pipe2
   HANDLE w2 = NULL;

   #define BUFSIZE 4096

   void CreateChildProcess() {
       TCHAR applicationName[] = TEXT("Child");
       PROCESS_INFORMATION pi;
       STARTUPINFO si;
       BOOL success = FALSE;

       ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
       ZeroMemory(&si, sizeof(STARTUPINFO));

       si.cb = sizeof(STARTUPINFO);
       si.hStdError = w1;
       si.hStdOutput = w1;
       si.hStdInput = r2;
       si.dwFlags |= STARTF_USESTDHANDLES;


       success = CreateProcess(NULL, applicationName, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

       if (!success) {
           printf("Error creating child process \n");
       }
       else {

           printf("Child process successfuly created \n");
           CloseHandle(pi.hProcess);
           CloseHandle(pi.hThread);
       }
   }
   void WriteToPipe() {
       DWORD read, written;
       CHAR chBuf[BUFSIZE];
       BOOL success = FALSE;

       HANDLE pStdIn = GetStdHandle(STD_INPUT_HANDLE);

       for (;;)
       {
           success = ReadFile(pStdIn, chBuf, BUFSIZE, &read, NULL);
           if (!success || read == 0) break;

           success = WriteFile(w2, chBuf, read, &written, NULL);
           if (!success) break;
       }
   }

   void ReadFromPipe() {
       DWORD read, written;
       CHAR chBuf[BUFSIZE];
       BOOL success = FALSE;

       HANDLE pStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

       for (;;)
       {
           success = ReadFile(r1, chBuf, BUFSIZE, &read, NULL);
           if (!success || read == 0) break;

           success = WriteFile(pStdOut, chBuf, read, &written, NULL);
           if (!success) break;
       }
   }

   int main()
   {

       DWORD dRead, dWritten;
       CHAR chBuf[BUFSIZE];
       BOOL bSuccess = FALSE;

       SECURITY_ATTRIBUTES secAttr;
       secAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
       secAttr.bInheritHandle = TRUE;
       secAttr.lpSecurityDescriptor = NULL;

       printf("Creating first pipe\n");
       if (!CreatePipe(&r1, &w1, &secAttr, 0)) {
           printf("\nError creating first pipe\n");
       }
       printf("Creating second pipe\n");
       if (!CreatePipe(&r2, &w2, &secAttr, 0)) {
           printf("Error creating second pipe \n");
       }


       if (!SetHandleInformation(r1, HANDLE_FLAG_INHERIT, 0)) {
           printf("r1 SetHandleInformation \n");
       }
       if (!SetHandleInformation(w2, HANDLE_FLAG_INHERIT, 0)) {
           printf("w2 SetHandleInformation \n");
       }


       printf("\nCreating child process..... \n");
       CreateChildProcess();

       WriteToPipe();
       ReadFromPipe();

       return 0;
   }

Child.cpp

   #include <windows.h>
   #include <stdio.h>
   #include "pch.h"

   #define BUFSIZE 4096

   int main()
   {
       DWORD dRead, dWritten;
       CHAR chBuf[BUFSIZE];
       BOOL success = FALSE;
       HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
       HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);


       if (stdIn == INVALID_HANDLE_VALUE || stdOut == INVALID_HANDLE_VALUE) {
           ExitProcess(1);
       }

       for (;;) {
           success = ReadFile(stdIn, chBuf, BUFSIZE, &dRead, NULL);
           if (!success || dRead == 0) break;
success = WriteFile(stdOut, chBuf, dRead, &dWritten, NULL);
           if (!success) break;
       }

       return 0;
   }