Thread: Running simultaneous processes

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Running simultaneous processes

    I am in the process of writing a service that is similar to UNIX cron.
    I need to be able to run multiple processes simultaneously. The code below always waits for one process to finish before continuing. Can someone point me in the right direction to allow the program to continue without waiting. Basically, allow processes to run in the background.

    Thanks.


    while (ServiceStatus.dwCurrentState ==
    SERVICE_RUNNING)
    {
    for(i=0; i<lines; i++)
    {
    if(check_time(i) == 1)
    {
    execute_process(i);

    }
    }

    }
    ServiceStatus.dwCurrentState =
    SERVICE_STOPPED;
    ServiceStatus.dwWin32ExitCode = -1;
    SetServiceStatus(hStatus,
    &ServiceStatus);
    LogMsg("MyCron stopped.");
    return;



    int execute_process(int i)
    {
    char command[64];

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    strcpy(command,cron_array[i].proc);

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL, command, NULL,
    NULL, FALSE, 0, NULL, NULL, &si, &pi )
    )
    {
    _ErrorExit( "CreateProcess failed." );
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );


    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>WaitForSingleObject( pi.hProcess, INFINITE );

    Without testing, I would say this line is the culprit..it's making the program wait for the child program to end

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    On an unrelated-to-your-question note, marksw, when you post your source make sure it's within a code block (by starting it with [code] and ending it with [/code]) so that it retains your formatting, thus making it easy on everyone's eyes.

  4. #4
    Cat
    Guest
    Fordy is right, if you simply remove that wait line, it will work just fine. It seems to be OK to close the handles before the thread & process terminate; it obviously won't terminate the process or thread and I don't think there are any memory leaks.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    2

    Thumbs up

    Thanks for the help.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No leaks.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running processes
    By pfs in forum Linux Programming
    Replies: 6
    Last Post: 01-01-2009, 07:23 PM
  2. Simultaneous processes
    By Lateralus in forum C Programming
    Replies: 5
    Last Post: 06-30-2005, 07:58 PM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. RE: Find out running processes from command prompt
    By sampatel in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 07:15 AM