Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor child processes of a process

Tags:

I'm running .exe file using this code:

Process proc = Process.Start("c:\program.exe"); proc.WaitForExit(); 

If I start Stopwatch before starting the process and stop it after proc.WaitForExit(); line, I can get the time that user was using that particular program.

The problem I'm facing is that some programs (and games) use launchers - some small .exe file that usually checks something and then launches another .exe file that is actually the program/game that the user wants to run. In these cases the code above doesn't work because it returns after launcher exists.

How can I track all processes that proc runs, and wait unitl all of them are terminated?

like image 398
xx77aBs Avatar asked Jul 29 '13 11:07

xx77aBs


People also ask

How do I see child processes in Linux?

Using the /proc File System It contains information about the kernel, system, and processes. We can find the PIDs of the child processes of a parent process in the children files located in the /proc/[pid]/task/[tid] directories.

What are child processes used for?

The child_process module enables us to access Operating System functionalities by running any system command inside a, well, child process. We can control that child process input stream, and listen to its output stream.

How do I find child processes in Windows?

The parent of the child process is init process, which is the very first process initiating all the tasks. To monitor the child process execution state, to check whether the child process is running or stopped or to check the execution status, etc. the wait() system calls and its variants is used.


2 Answers

Here is the solution that the asker found:

public static class ProcessExtensions {     public static IEnumerable<Process> GetChildProcesses(this Process process)     {         List<Process> children = new List<Process>();         ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));          foreach (ManagementObject mo in mos.Get())         {             children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));         }          return children;     } } 
like image 98
ANeves Avatar answered Sep 20 '22 16:09

ANeves


Take a look at this - Find all child processes of my own .NET process / find out if a given process is a child of my own? or http://social.msdn.microsoft.com/Forums/vstudio/en-US/d60f0793-cc92-48fb-b867-dd113dabcd5c/how-to-find-the-child-processes-associated-with-a-pid. They provide ways to find child processes by a parent PID (which you have).

You can write monitor the process you create and also get its children. You could then track everything, and wait for them all to finish. I say "try" because I'm not sure you could catch very rapid changes (a process starting others and then dying before you get his children).

like image 20
Vadim Avatar answered Sep 17 '22 16:09

Vadim