|
-
October 27th, 2010, 11:19 AM
#1
Running programs simultaneously
I want to run the programs r1-r4 and n1-n4 simultaneously instead of waiting for each one to finish executing,I've been stuck on this for a long time, i went through multi threading for c++ in dev-cpp and the Win API code is quite complicated. How do i execute them simultaneously ?
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
ifstream od,od2,od3,od4,od5,od6,od7,od8;
ifstream odd,odd2,odd3,odd4,odd5,odd6,odd7,odd8;
string line;
int num[8],nu=0;
system("r1.exe");system("CLS");
system("r2.exe");system("CLS");
system("r3.exe");system("CLS");
system("r4.exe");system("CLS");
system("n1.exe");system("CLS");
system("n2.exe");system("CLS");
system("n3.exe");system("CLS");
system("n4.exe");system("CLS");
od.open("r1.txt");
getline(od,line);
num[nu]=atoi(line.c_str());
nu++;
od.close();
od2.open("r2.txt");
getline(od2,line);
num[nu]=atoi(line.c_str());
nu++;
od2.close();
od3.open("r3.txt");
getline(od3,line);
num[nu]=atoi(line.c_str());
nu++;
od3.close();
od4.open("r4.txt");
getline(od4,line);
num[nu]=atoi(line.c_str());
nu++;
od4.close();
od5.open("n1.txt");
getline(od5,line);
num[nu]=atoi(line.c_str());
nu++;
od5.close();
od6.open("n2.txt");
getline(od6,line);
num[nu]=atoi(line.c_str());
nu++;
od6.close();
od7.open("n3.txt");
getline(od7,line);
num[nu]=atoi(line.c_str());
nu++;
od7.close();
od8.open("n4.txt");
getline(od8,line);
num[nu]=atoi(line.c_str());
nu++;
od8.close();
int best=0;
best=num[0];
for(int i=0;i<nu;i++)
{
if(num[i]>best){best=num[i];}
}
cout << best ;
cin.get();
return 0;
}
Then i tried to create a process
Code:
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
if(CreateProcess("c:\Dev-Cpp\r1.exe",
" example.txt",
0,
0,
FALSE,
CREATE_DEFAULT_ERROR_MODE,
0,
0,
&siStartupInfo,
&piProcessInfo) == FALSE)
But how do i make multiple programs run simultaneously using create process ?
-
October 27th, 2010, 11:37 AM
#2
Re: Running programs simultaneously
Just call CreateProcess for each exe. The function doesn't block.
Viggy
-
October 27th, 2010, 01:24 PM
#3
Re: Running programs simultaneously
 Originally Posted by MrViggy
Just call CreateProcess for each exe. The function doesn't block.
Viggy
Thanks for the reply,
You mean separate instances of each program like this
Code:
if(CreateProcess("c:\Dev-Cpp\DCS\Decom\r1.exe", // Application name
" example.txt", // Application arguments
0,
0,
FALSE,
CREATE_DEFAULT_ERROR_MODE,
0,
0, // Working directory
&siStartupInfo,
&piProcessInfo) == FALSE)
if(CreateProcess("c:\Dev-Cpp\DCS\Decom\r2.exe", // Application name
" example.txt", // Application arguments
0,
0,
FALSE,
CREATE_DEFAULT_ERROR_MODE,
0,
0, // Working directory
&siStartupInfo,
&piProcessInfo) == FALSE)
Tried this it does not work,how do i call another instances ?
-
October 27th, 2010, 02:09 PM
#4
Re: Running programs simultaneously
What "doesn't work"? CreateProcess is a non-blocking function. It kicks off the new process, and immediately returns.
Viggy
-
October 27th, 2010, 02:45 PM
#5
Re: Running programs simultaneously
I guess that the processes starts alright but that you are reading the output files to early. Use what's returned in piProcessInfo to wait until the spawned process has exited before reading the file.
See MrViggy's link, http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx and http://msdn.microsoft.com/en-us/libr...32(VS.85).aspx (or maybe http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx depending on how you decide to code it)
-
October 29th, 2010, 12:58 AM
#6
Re: Running programs simultaneously
Use what's returned in piProcessInfo to wait until the spawned process has exited before reading the file.
As long as the original point is not only about spawning processes but asynchronous result processing as well, this is where multithreading enters the scene...
Best regards,
Igor
-
November 16th, 2010, 01:38 PM
#7
Re: Running programs simultaneously
If the slave processes are also written by you, then you can have them communicate their status instead of creating a new thread just to wait. For example, by using messages. If you are using already existing programs then your options are more limited.
Last edited by MilesAhead; November 16th, 2010 at 01:49 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|