Thread: creating n monitoring 5 processes (notepad)

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    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!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    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); 
         }
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    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;
    
       HANDLE lpHandles[5];
       DWORD object;
       int i, j;
    
       for (i=0; i<5;)
       {
    
          if(CreateProcess   (   NULL,
                                     "notepad.exe",
                                     NULL,
                                     NULL,
                                     0,
                                     NORMAL_PRIORITY_CLASS,
                                     NULL,
                                     NULL,
                                     &si,
                                     &pi
                                             )) 
      
          {
    
             cout<<"The ID of notepad is "<<pi.dwProcessId <<endl;
             lpHandles[i++] = pi.hProcess;
          }
          else
          {
             cout<< "Error creating process:"<< GetLastError()<<endl;
          }
       }
    
       for (i=5; i>0; i--)
       {
          while((object = WaitForMultipleObjects((DWORD) i, lpHandles, FALSE, 1000)) == WAIT_TIMEOUT)
          {
             cout<<i;
          }
    
          cout<<endl;
          cout << "object:" << object << endl;
          CloseHandle(lpHandles[object]);     
          for (j=object; j<i-1; j++)
             lpHandles[j] = lpHandles[j+1];
    
       }
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    thanxs

    Thanxs swoopy, u've helped me a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Monitoring processes
    By DanFraser in forum C# Programming
    Replies: 1
    Last Post: 10-23-2008, 02:07 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Creating processes
    By xErath in forum Linux Programming
    Replies: 5
    Last Post: 10-09-2004, 01:36 PM