CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    2

    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 ?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Running programs simultaneously

    Just call CreateProcess for each exe. The function doesn't block.

    Viggy

  3. #3
    Join Date
    Oct 2010
    Posts
    2

    Re: Running programs simultaneously

    Quote Originally Posted by MrViggy View Post
    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 ?

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Running programs simultaneously

    What "doesn't work"? CreateProcess is a non-blocking function. It kicks off the new process, and immediately returns.

    Viggy

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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

  7. #7
    Join Date
    Nov 2007
    Posts
    35

    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
  •  





Click Here to Expand Forum to Full Width

Featured