PDA

View Full Version : سوال: نوشتن یک لوله بدون نام با C++‎‎‎



masood69
دوشنبه 07 آذر 1390, 21:40 عصر
سلام
استادمون برای درس سیستم عامل تمرین زیر راداده :
برنامه ای که یک پردازه ی فرزند ایجاد کند واین پردازه فرزند دنباله فیبوناچی را محاسبه کند وآخرین مقدار را با یک پایپ به پردازه والد برگرداند
من خودم کد زیر رو نوشتم پردازه اجرا می شود ولی پایپ کار نمی کند و ارور (error)زیر را می دهد
error:readfile failed with error 109
برای پردازه ی والد کد زیر را نوشتم:


#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include<conio.h>
#include<iostream>
#include <strsafe.h>

#define s 25
using namespace std;

HANDLE hChildStdoutRd ,hChildStdoutWr;
int _tmain(int argc, TCHAR* argv[])
{ DWORD dwRead;
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;

STARTUPINFO si;
long unsigned int b[s];
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si));
ZeroMemory(&pi,sizeof(pi));
si.cb=sizeof(STARTUPINFO);
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
si.hStdOutput=hChildStdoutWr;
si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);

if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 3))
cout<<"pipe process";

SetHandleInformation(hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);



if(!CreateProcess(NULL,"D:\masood1.exe",NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
cout<<"false";


WaitForSingleObject(pi.hProcess,INFINITE);
if ((hChildStdoutRd == INVALID_HANDLE_VALUE))
cout<<"INVALID_HANDLE_VALUE";
CloseHandle(hChildStdoutWr);
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

// Display the error message and exit the process

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)"CloseHandle")+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf),
TEXT("%s failed with error %d: %s"),
"CloseHandle", dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

//for(int i=0;i<6;i++)
//{
if (!ReadFile(hChildStdoutRd, b,sizeof(b)-1,
&dwRead, 0)) cout<<"error";
///cout<<"dwre"<<dwRead;
//}

dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

// Display the error message and exit the process

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)"ReadFile")+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf),
TEXT("%s failed with error %d: %s"),
"ReadFile", dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);

CloseHandle(hChildStdoutRd);
cout<<b[0];
getch();
return 0;
}

برا پردازه فرزند کد زیر را نوشتم:


// masood1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<tchar.h>
#include<Windows.h>
HANDLE hChildStdoutwr;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
long unsigned int f1=1,f2=1,f3=0,n;

DWORD dw, dwWritten;

hChildStdoutwr=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Ennter a positive Number"<<"\n";
cin>>n;
if(n<0)
{
cout<<"the Negative Number is'n legal";
getch();
return 0;
}
for(int i=1;i<=n;i++)
{
if(i==1||i==2)
cout<<1<<"\n";
else
{f3=f2+f1;
cout<<f3<<"\n";
int t=f2;
f2=f3;
f1=t;
}
}

// if ((hChildStdoutRd == INVALID_HANDLE_VALUE))
long unsigned int b[]={f3};
if(!WriteFile(hChildStdoutwr,b ,sizeof(b),&dwWritten,NULL))
cout<<"child process faild:";
//CloseHandle(hChildStdoutwr);
dw=GetLastError();

// Display the error message and exit the process


cout<<dw;

exit(1);
return 0;
}
اگه میشه هر کی میتونه به من تا فردا جواب بده
درمورد error109 میگه لوله پایان یافته است و قسمت readfile اجرا نمی شه
پیشاپیش از کمک شما تشکر می کنم:گریه: