Hi,

This is my first project involving processes I am completely lost. I need to create 10 processes that run at the same time. I know that a process is a program in execution so I must write a program that executes 10 times. I used Win32 API to create a single process. How do I modify the CreateProcess function to create multiple processes and have them run at the same time? My code is in C++. I've been to several websites including Microsoft's website but they discuss the CreateProcess function only. I've been reading some of the posts on this site and it has been very helpful. I think I'm beginning to get a better understanding of what I am supposed to be doing. If I can get some steps to follow or code, or where to find additional information, I will be most grateful. Thanks

How do I modify the CreateProcess function to create multiple processes and have them run at the same time?

Contact microsoft and ask them for the source code to CreateProcess so you can modify it. Of maybe you can use CreateProcess 10 times?

Contact microsoft and ask them for the source code to CreateProcess so you can modify it. Of maybe you can use CreateProcess 10 times?

That was not the response I was expecting, but thanks all the same. I already have the code to create the process.

That was not the response I was expecting, but thanks all the same. I already have the code to create the process.

So it was just a test to see if someone gave you the answer you already knew? That's not even fair... :icon_wink:

Why do you use platform dependent functions like CreateProcess when the C++ standard library provides fork()?

Why do you use platform dependent functions like CreateProcess when the C++ standard library provides fork()?

I didn't know that. I am not familiar with UNIX which I think uses fork(). I wanted to use something I could quickly learn so I can complete my project.

So it was just a test to see if someone gave you the answer you already knew? That's not even fair... :icon_wink:

Looks like you did not understand my post. Please read it carefully before responding. Even though I posted this question, I am also doing my own research. Your response was very confusing in light of the question I asked.

I didn't know that. I am not familiar with UNIX which I think uses fork(). I wanted to use something I could quickly learn so I can complete my project.

Nvm, I thought fork() was part of the C++ standard library, bit it doesn't seem to be... then you are indeed better off using CreateProcess if your target is Windows :p.

Nvm, I thought fork() was part of the C++ standard library, bit it doesn't seem to be... then you are indeed better off using CreateProcess if your target is Windows :p.

Thanks. I will stick to CreateProcess. If I figure how to get the program (process) to execute multiple times, I'll post it here. It might be helpful to someone.

why don't you just call CreateProcess() in a loop and call it 10 times?

why don't you just call CreateProcess() in a loop and call it 10 times?

well, that would introduce a time delay between the instances and the requirement is that they all run at the same time :)

AFAIK the only way to do that is to, in each of 10 CPUs, create a trigger that launches a process on that CPU when the trigger fires and then to hope that all those triggers indeed fire at the exact same microsecond.

The OP didn't say anything about having 10 CPUs. He just wants 10 processes to be launched all at the same time. There is no way to do it on only a single CPU. Even if there were a way, they would not actually all run at the same time on a single CPU due to time slicing processes.

of course, I stated what would be needed to do what he wants to do, never mind if he's actually capable of doing it :)

When you set unrealistic requirements, expect solutions that require resources beyond what you had imagined.

why don't you just call CreateProcess() in a loop and call it 10 times?

I thought of that but wasn't sure it was going to work because of the way the code for CreateProcess is written. Maybe I should have tried it all the same. I'll try it and let you know. Thanks

@annette123: just wondering, what exactly is the difference between

system(const char *command);

and

BOOL WINAPI CreateProcess(
  __in_opt     LPCTSTR lpApplicationName,
  __inout_opt  LPTSTR lpCommandLine,
  __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in         BOOL bInheritHandles,
  __in         DWORD dwCreationFlags,
  __in_opt     LPVOID lpEnvironment,
  __in_opt     LPCTSTR lpCurrentDirectory,
  __in         LPSTARTUPINFO lpStartupInfo,
  __out        LPPROCESS_INFORMATION lpProcessInformation
);

I'm not familiar with the win32 api because I mostly code cross-platform stuff with the standard libraries or Qt's libraries :p.

CreateProcess() gives you a lot more flexibility to control how the new process is actually created. All but the 1st, 9th and 10th parameters can be 0.

Your program has no control at all with the system() command.

>> Dragon: why don't you just call CreateProcess() in a loop and call it 10 times?

Just listen to the Dragon, and treat everything else as white noise.

This is almost certainly the problem you were asked to solve, but just fantasize for a moment that your program is to run on (an otherwise idling) machine with, say, sixteen processors, and these ten processes need to start running at about the same measurable instant in time. You could do something like this:

enum { N = 10 } ;
char path[] = "<path to executable image>" ;

PROCESS_INFORMATION pi ;
STARTUPINFO si ;
std::memset( &si, 0, sizeof(si) ) ;
si.cb = sizeof(si) ;
std::vector<HANDLE> threads ;

for( int i=0 ; i<N ; ++i )
{
   if( CreateProcess( path, nullptr, nullptr, nullptr, FALSE,
                      CREATE_SUSPENDED, nullptr, nullptr, &si, &pi ) )
   {
       CloseHandle( pi.hProcess ) ;
       threads.push_back( pi.hThread ) ;
   }
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL ) ;
   std::for_each( threads.begin(), threads.end(), ::ResumeThread ) ;
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_NORMAL ) ;
   // ..

   WaitForMultipleObjects( threads.size(), &threads.front(), TRUE, INFINITE ) ;
   // ...
   std::for_each( threads.begin(), threads.end(), ::CloseHandle ) ;
}
commented: good job +2

>> Dragon: why don't you just call CreateProcess() in a loop and call it 10 times?

Just listen to the Dragon, and treat everything else as white noise.

This is almost certainly the problem you were asked to solve, but just fantasize for a moment that your program is to run on (an otherwise idling) machine with, say, sixteen processors, and these ten processes need to start running at about the same measurable instant in time. You could do something like this:

enum { N = 10 } ;
char path[] = "<path to executable image>" ;

PROCESS_INFORMATION pi ;
STARTUPINFO si ;
std::memset( &si, 0, sizeof(si) ) ;
si.cb = sizeof(si) ;
std::vector<HANDLE> threads ;

for( int i=0 ; i<N ; ++i )
{
   if( CreateProcess( path, nullptr, nullptr, nullptr, FALSE,
                      CREATE_SUSPENDED, nullptr, nullptr, &si, &pi ) )
   {
       CloseHandle( pi.hProcess ) ;
       threads.push_back( pi.hThread ) ;
   }
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL ) ;
   std::for_each( threads.begin(), threads.end(), ::ResumeThread ) ;
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_NORMAL ) ;
   // ..

   WaitForMultipleObjects( threads.size(), &threads.front(), TRUE, INFINITE ) ;
   // ...
   std::for_each( threads.begin(), threads.end(), ::CloseHandle ) ;
}

Thanks vijayan121, I'll try this. Sorry it took so long to respond to everyone. Lots of school work!!!

>> Dragon: why don't you just call CreateProcess() in a loop and call it 10 times?

Just listen to the Dragon, and treat everything else as white noise.

This is almost certainly the problem you were asked to solve, but just fantasize for a moment that your program is to run on (an otherwise idling) machine with, say, sixteen processors, and these ten processes need to start running at about the same measurable instant in time. You could do something like this:

enum { N = 10 } ;
char path[] = "<path to executable image>" ;

PROCESS_INFORMATION pi ;
STARTUPINFO si ;
std::memset( &si, 0, sizeof(si) ) ;
si.cb = sizeof(si) ;
std::vector<HANDLE> threads ;

for( int i=0 ; i<N ; ++i )
{
   if( CreateProcess( path, nullptr, nullptr, nullptr, FALSE,
                      CREATE_SUSPENDED, nullptr, nullptr, &si, &pi ) )
   {
       CloseHandle( pi.hProcess ) ;
       threads.push_back( pi.hThread ) ;
   }
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL ) ;
   std::for_each( threads.begin(), threads.end(), ::ResumeThread ) ;
   SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_NORMAL ) ;
   // ..

   WaitForMultipleObjects( threads.size(), &threads.front(), TRUE, INFINITE ) ;
   // ...
   std::for_each( threads.begin(), threads.end(), ::CloseHandle ) ;
}

What is the "path" in the parameters for the CreateProcess function? How do you determine it? I know it's supposed to be the command line but I don't know what path to use. Please help. Thanks

> What is the "path" in the parameters for the CreateProcess function?

The complete path to the executable file.
For example, "C:\\windows\\system32\\notepad.exe"

An option is to leave the lpApplicationName as NULL and put both the application name and the command line parameters in a modifiable string and pass that as lpCommandLine (The search path could be used in this case).
For example, "notepad.exe myfile.txt"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.