PDA

View Full Version : Find child processes



rajji_saini
20th May 2011, 01:28
Hi ,

My project works on both Linux and Windows. So I want to be able to find a common/unique way to get the list of child processes of a process(QProcess). So that when I kill the main process I kill the child/descendants as well.

Regards,
Raj

high_flyer
20th May 2011, 08:52
This is system specific.
There is no common way to do it.

rajji_saini
20th May 2011, 18:28
Could you please lead me to a link or example for windows and Linux ??

high_flyer
23rd May 2011, 09:03
sure:
here (http://lmgtfy.com/?q=list+running+processes+windows)

rajji_saini
23rd May 2011, 23:22
This works on windows:



void winTerminateChildProcesses()
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
qDebug() << "Error: CreateToolhelp32Snapshot";
return;
}

// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );

// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
qDebug() << "Error: Process32First";
CloseHandle( hProcessSnap ); // clean the snapshot object
return;
}

ProcTree procTree;

// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
procTree[pe32.th32ParentProcessID] << pe32.th32ProcessID;
}
while( Process32Next( hProcessSnap, &pe32 ) );

CloseHandle( hProcessSnap );

QHashIterator<DWORD, QSet<DWORD>> i(procTree);
while (i.hasNext()) {
i.next();
qDebug() << i.key() << ": " << i.value() << endl;
}

}

rajji_saini
27th May 2011, 23:07
Just to avoid confusion :- ProcTree here is a QHash

typedef QHash<DWORD, QSet<DWORD> > ProcTree;

wysota
28th May 2011, 08:13
On Linux there is a concept of process groups. When you kill a group leader, all its children die automatically.