3
我在學習如何使用信號燈和_popen。我有兩個過程。C++信號燈和_popen用法
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <process.h>
#include <fstream>
#using <System.dll>
using namespace System;
using namespace System::Threading;
using namespace std;
過程中的一個:
int main(){
FILE *pPipe;
Semaphore^ _pool = gcnew Semaphore(1, 1, "pool");
Semaphore^ _eater = gcnew Semaphore(0, 1, "eater");
char psBuffer[128];
if((pPipe = _popen("D:\gen.exe", "rt")) == NULL)
exit(1);
while(!feof(pPipe)){
_eater->WaitOne();
fgets(psBuffer, 128, pPipe);
_pool->Release();
cout<<psBuffer;
}
printf("\nProcess returned %d\n", _pclose(pPipe));
} ;
過程中的兩個(gen.exe):
int i=0;
Semaphore^ crt = nullptr;
crt = Semaphore::OpenExisting("pool");
Semaphore^ eat = nullptr;
eat = Semaphore::OpenExisting("eater");
while(true)
{
i++;
crt->WaitOne();
cout<<i;
eat->Release();
}
};
他們什麼都不做。讓他們做某事的唯一方法是刪除fgets(psBuffer, 128, pPipe);
(我不知道它爲什麼)。我想讓他們正常工作與信號量,我試了很多次,但沒有結果:(這個程序有什麼問題?
非常感謝你非常非常^ _ ^。 – Ophelia 2011-12-29 18:48:22