Link to home
Start Free TrialLog in
Avatar of nishzone
nishzone

asked on

create process

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.

#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! :)
Avatar of ChristopH1987
ChristopH1987

Hi!

Use this:

#include <iostream.h>
#include <windows.h>;
#include <stdio.h>


int main()
{

// array to hold the data
   PROCESS_INFORMATION *pi = (PROCESS_INFORMATION*)malloc((sizeof PROCESS_INFORMATION)*5);

// Create all processes
  for (i=0; i<5; 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]
                                         ))
 
       {

           printf("The ID of notepad [%d] is %d\n", i+1, pi.dwProcessId);
   
      }else
      {
         printf("Error creating process[%d]: %d", i, GetLastError());
         return;
      }
 }

// Here is the code to realize your problem

// Follows in some minutes!!
 
///       Cleanup
  for(int i=0;i<5;i++)
  {
           CloseHandle(pi[i].hThread);

           CloseHandle(pi[i].hProcess);
  }
  free(pi);

 }
SOLUTION
Avatar of ChristopH1987
ChristopH1987

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nishzone

ASKER

hey ChristopH1987 ,

i still have the problem of displaying how many processes r opened.
this looks very complicated. it will take me a while to undertand it.
Hey!

I have tested it and it works:

This is the output after running it:

The ID of notepad[1] is: xxxx
The ID of notepad[2] is: xxxx
The ID of notepad[3] is: xxxx
The ID of notepad[4] is: xxxx
The ID of notepad[5] is: xxxx


Current Notepads: 5
Current Notepads: 5
Current Notepads: 5
Current Notepads: 5

// If you close one
Current Notepads: 4
Current Notepads: 4
Current Notepads: 4

// If you close all
Current Notepads: 3
Current Notepads: 2
Current Notepads: 1
Current Notepads: 0

// then the program terminates!
this looks like it's exactly wat i want except that can't get <vcl.h> n link PSAPI.lib with the code. i new at c and c++ n this question is part of my assignment. i dun think that the lecturer would give me something this complicated. i'm pretty sure that there is another way to answer this question. thanxs 4 trying to help though ChristopH1987.
ok, i've done this. the mistake is at waitformultiple objects but i dun knoe how to corect it.

#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);
      }
}
Hi!

Sorry but with your code it isnt possible to realize your idea! The problem is in WaitForMultipleObjects.
You can't get the current count of the running notpads with this method.  My idea to use EnumProcesses()
compares the current processes with the ProcessIDs of the notepads... So you can look up if several notepads are closed!

>this looks like it's exactly wat i want except that can't get <vcl.h> n link PSAPI.lib with the code. i new at c and c++ n >this question is part of my assignment. i dun think that the lecturer would give me something this complicated. i'm >pretty sure that there is another way to answer this question. thanxs 4 trying to help though ChristopH1987.

#include <vcl.h> is only for C++ Builder! You can delete it!

PSAPI.h have to exist in Visual C++ 6 or so...
I think you have not to link PSAPI.lib with your code in Visual C++ 6 else just ask the question "How to link PSAPI.lib with my code" in a new request!

I hope this helps....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial