creating n monitoring 5 processes (notepad)
i'm suppose to create 5 processes, each an instance of notepad.exe. A digit (between 1 and 5) should b displayed every second. e.g if 3 processes are opened, it should display the digit 3 every second. when the user closes another process it will display the digit 2 every second.
i've written these lines of codes but there is a problem.
Code:
#include <iostream.h>
#include <windows.h>;
#include <stdio.h>
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD dwCode = 0;
ZeroMemory ( &si, sizeof ( STARTUPINFO));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
int i;
for (i=5; i>0; i--)
{
if(CreateProcess ( NULL,
"notepad.exe",
NULL,
NULL,
0,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi
))
{
cout<<"The ID of notepad is "<<pi.dwProcessId <<endl;
while(WaitForSingleObject(pi.hProcess, 1000) == WAIT_TIMEOUT)
{
cout<<i;
}
cout<<endl;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
cout<< "Error creating process:"<< GetLastError()<<endl;
}
}
}
the problem with these codes is that, it doesn't create the next process until the current process is closed. e.g the second process is not created until the first one closes. i want to b able to create all 5 processes n then display the numbers.
i think i'm suppose to use waitForMultipleObjects function but i'm really struggling with it.
thanx a lot 4 ur help! :)
waitformultipleobjects function
hey,
i think i'm close to getting my program to work but i have a problem. I'm not sure wat to put in the second parameter of waitForMultipleObjects. Can somebody plz tell me.
Here is the code:
Code:
#include <iostream.h>
#include <windows.h>
#include <stdio.h>
int main()
{
PROCESS_INFORMATION pi[5];
int i;
for (i=5; i>0; i--)
{
STARTUPINFO si;
DWORD dwCode = 0;
ZeroMemory ( &si, sizeof ( STARTUPINFO));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if(CreateProcess ( NULL,
"notepad.exe",
NULL,
NULL,
0,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi[i]
))
{
cout<<"The ID of notepad is "<<pi[i].dwProcessId <<endl;
}
else
{
cout<< "Error creating process:"<< GetLastError()<<endl;
}
}
for (i=5; i>0; i--)
{
while(WaitForMultipleObjects(5, pi.hProcess,TRUE, 1000) == WAIT_TIMEOUT)
{
cout<<i;
}
cout<<endl;
}
for (i=1, i<6, i++)
{
CloseHandle(pi[i].hThread);
CloseHandle(pi[i].hProcess);
}
}