Skip to content Skip to sidebar Skip to footer

Read in Child Before Write in Parent Pipes

4.one Experimental purpose

By programming in the VS, the pipage advice of the parent child procedure is implemented, requiring the parent process to enter numbers from the window, the input number is transmitted to the child process, and the child process adds the input string and opens readme.txt. After the file adding is completed, read the line in the document, and finally use the class of the _ document a row string to return to the parent process. EG:

Parent Process DOS Enter: 45

Sub-process summation: 4 + 5 = nine

Document Current String: Just a Pipe Exam!

Parent Procedure DOS Display: 9_ But A Pipage Test!

4.two Experimental points

Due to the characteristics of the pipeline unidirectional communication, ii pipes are required during communication, one read pipe, a write pipage.

Notation that the buffer size is set, and improper buffer size settings cause data from the information.

Each time you display the parent process from the data read past the child, you need to redirect your handle, otherwise the impress failed.std_write = GetStdHandle( STD_OUTPUT_HANDLE );

Bearding pipes can only achieve advice between the local processes, and communication between the cross-network processes cannot be implemented past anonymous pipelines.

The anonymous pipe is communicated in a half-duplex manner, and between the processes of communication are blood relationships, and the parent-kid relationship or brother relationship process, so I tin know that two processes on the local computer can communicate via anonymous pipeline.

An anonymous piping has an important functionality that the redirection of the sub-procedure output tin can be achieved by bearding pipeline.

https://images.cnblogs.com/cnblogs_com/BoyXiao/201101/201101011901054917.png

Draw a lot of information in this surface area of ​​the cherry line from there? Why is it in this text box? In fact, this can be implemented by anonymous pipeline, in this screenshot of the QQ game, actually running ii processes, one is the process we encounter the output graphics interface, we call it unloading table icon process (father Process), and another process for performing uninstalling uninstallations is called uninstalling substance process (sub-process). In fact, the uninstalling table symbol process creates an uninstalling substance procedure during its execution to perform a real uninstallation operation, and so the unloading substance process will output information in the surface area with red rectangular tag, if we use the default output, uninstalling substance The process volition output the information in the blood-red area tag to the default black box, but we can utilise an bearding pipe to change the output of uninstalling the substance process, assuasive it to enter the output data into bearding pipe, and then unload the table icon procedure from anonymous pipeline These output data are read in the text boxes in which they are displayed in the text box that displays these data to the unloading table. The above-mentioned technique used to alter the output of the unloading substance process is called the output redirection. Of grade, there is also an input redirection. We can allow a process from anonymous pipe instead of we enter data in black box.

4.iii Operation effect

4.4 code

Parent procedure

          #include<iostream> #include <Windows.h> #include<string>  using namespace std;  #define buffsize 200 // buffer size   // Parent process  form ParentPip { public: 	ParentPip(); 	~ParentPip(); 	void InitPip(); 	void PipRead(); 	void PipWrite(); 	 Char Writes [buffsize]; // Write out 	 Char reads [buffsize]; // read back private: 	 Handle HparentRead; // Parent Process Reader Handle 	 Handle hParentWrite; // Parent process writing 	 Handle hchildread; // child process writing 	 Handle hchildwrite; // child process reading handle 	 Handle std_write; // DOS handle 	 Security_attributes sa = {0}; // Pipeline security attribute 	 STARTUPINFO SI = {0}; // How to make up one's mind how the master form of the new process is displayed 	 Process_information pi = {0}; // process information construction };  ParentPip::ParentPip() { 	hParentRead = NULL; 	hParentWrite = Aught; 	hChildRead = NULL; 	hChildWrite = NULL; 	std_write = NULL; 	 // ZeromeMory (& sa, sizeof (security_attributes)); // Fill with 0 	 // ZeromeMory (& Si, SIZEOF (STARTUPINFO)); // Fill with 0 	 // ZeromeMory (& Pi, sizeof (process_information)); // Fill with 0  	sa.nLength = sizeof(SECURITY_ATTRIBUTES); 	 sa.binherithandle = true; // Set up the handle to inherit 	 	si.cb = sizeof(si); }  ParentPip::~ParentPip() {  }  void ParentPip::InitPip() { 	 // Create a pipe 1. Parent procedure read -> child procedure write 	BOOL bRet = CreatePipe(&hParentRead, 		&hChildWrite, 		&sa, 		0);  	 // Create a pipe 2. Sub procedure read -> Parent process write. 	bRet = CreatePipe(&hChildRead, 		&hParentWrite, 		&sa, 		0);  	 / / Here, the sub-procedure will be redirected to STDOUT. The child process reads redirected to stdinput 	si.hStdInput = hChildRead; 	si.hStdOutput = hChildWrite; 	 Si.dwflags = startf_usestdhandles; // Setting the window hidden startup  	bRet = CreateProcess(NULL, 		 ".. \\ .. \\ kid \\ debug \\ kid.exe", // Create a CMD process. The default is looking for the CMD process. 		Null, 		Zippo, 		True, 		CREATE_NEW_CONSOLE, 		Zip, 		NULL, 		&si, 		&pi);  }  void ParentPip::PipWrite() { 	string input; 	cin >> input; 	input = input + "/0"; 	strcpy_s(Writes, &input[0]);  	DWORD dwWrite; 	bool wsuccess = fake; 	wsuccess = WriteFile(hParentWrite, Writes, strlen(Writes), &dwWrite, Nix); 	if (wsuccess == 0 || dwWrite == 0) { 		 COUT << "Write Pipeline Failure" << ENDL; 	}  }  void ParentPip::PipRead() { 	DWORD dwRead; 	DWORD dwWrite; 	HANDLE std_write; 	bool rsuccess = false; 	std_write = GetStdHandle(STD_OUTPUT_HANDLE); 	rsuccess = ReadFile(hParentRead, Reads, BUFFSIZE, &dwRead, Zippo); 	if (!rsuccess || dwRead == 0) 	{ 		 COUT << "Reading pipage failed" << Endl; 	} 	 rsuccess = writefile (std_write, reads, dwread, & dwwrite, null); // Write to standard output, ie DOS window 	 IF (! rsuccess) cout << "Output Failure" << ENDL; }  int main() { 	ParentPip mypip; 	mypip.InitPip(); 	int sum = x; 	while (sum--) 	{ 		mypip.PipWrite(); 		mypip.PipRead(); 	} 	return 0; }                  

Child procedure

          #include<iostream> #include <Windows.h> #include<cord> #include<fstream>  using namespace std; #ascertain buffsize 200 // buffer size   // kid process  class ChildPip { public: 	ChildPip(); 	~ChildPip(); 	void InitPip(); 	void PipRead(); 	void PipWrite(); 	void openfile(); 	int MyAdd(DWORD dwRead); 	 Char Writes [buffsize]; // Write out 	 Char reads [buffsize]; // read dorsum private: 	 Handle Hread; // Inherited Reading Pipe 	 Handle hwrite; // inherited write management 	 Ifstream commandList; // upper top directory 	string line;  	 Handle hchildread; // kid process writing 	 Handle hchildwrite; // child procedure reading handle 	 Handle std_write; // DOS handle 	 Security_attributes sa = {0}; // Pipeline security attribute 	 STARTUPINFO SI = {0}; // How to determine how the main class of the new procedure is displayed 	 Process_information pi = {0}; // procedure information structure };    ChildPip::ChildPip() { 	openfile(); }  ChildPip::~ChildPip() { }  void ChildPip::InitPip() { 	 HREAD = getStdHandle (std_input_handle); // Input Redirection 	 HWRITE = getStdHandle (std_output_handle); // Output Redirection 	if ((hRead == INVALID_HANDLE_VALUE) || (hRead == INVALID_HANDLE_VALUE)) 		 Cout << "Inherited handle is invalid" << ENDL; }  void ChildPip::PipRead() { 	DWORD dwRead; 	bool rsuccess = fake; 	 rsuccess = readfile (Hread, Reads, SizeOf (Reads), & dwread, cipher; // read data using standard input handles (ie anonymous pipe reading handle)   	int sum = 0; 	getline(commandList, line); 	int alphabetize = 0; 	int k = MyAdd(dwRead - ii); 	Writes[thousand++] = '_'; 	while (index < line.size()) { 		Writes[thou] = line[index]; 		index++; 		k++; 	} 	Writes[k++] = '\n'; 	Writes[one thousand++] = '\r'; 	Writes[chiliad] = '\0'; }  void ChildPip::PipWrite() { 	DWORD dwWrite; 	bool wsuccess = simulated; 	wsuccess = WriteFile(hWrite, Writes, strlen(Writes), &dwWrite, NULL); 	 If (! wsuccess) cout << "output failed" << endl; // Writing information using standard output handles (ie anonymous piping writing handle)   }  void ChildPip::openfile() { 	 CommandList.Open (".. \\ .. \\ information \ \ Readme.txt"); 	if (!commandList.is_open()) { 		 Cout << "Opens" Failure "<< ENDL; 	} }  int ChildPip::MyAdd(DWORD dwRead) { 	int ret = 0; 	int temp = 0; 	for (int i = 0; i < dwRead; i++) { 		temp += Reads[i] - '0'; 	} 	while (temp > 0) { 		Writes[ret] = temp % x + '0'; 		temp /= 10; 		ret++; 	} 	int left = 0, correct = ret - 1; 	char tp; 	while (left < correct) 	{ 		tp = Writes[left]; 		Writes[left] = Writes[right]; 		Writes[correct] = tp; 		left++; correct--;  	} 	return ret; }  int primary() { 	ChildPip mypip; 	mypip.InitPip(); 	printf("\north*******\nabcd\n"); 	int sum = ten; 	while (sum--) 	{ 		mypip.PipRead(); 		mypip.PipWrite(); 	} 	system("interruption"); 	return 0; }                  

longbetheraine.blogspot.com

Source: https://programmersought.com/article/97258112983/

Enregistrer un commentaire for "Read in Child Before Write in Parent Pipes"