Results 1 to 7 of 7

Thread: Find child processes

  1. #1
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Find child processes

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Find child processes

    This is system specific.
    There is no common way to do it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Find child processes

    Could you please lead me to a link or example for windows and Linux ??

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Find child processes

    sure:
    here
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Find child processes

    This works on windows:


    Qt Code:
    1. void winTerminateChildProcesses()
    2. {
    3. HANDLE hProcessSnap;
    4. PROCESSENTRY32 pe32;
    5.  
    6. // Take a snapshot of all processes in the system.
    7. hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    8. if( hProcessSnap == INVALID_HANDLE_VALUE )
    9. {
    10. qDebug() << "Error: CreateToolhelp32Snapshot";
    11. return;
    12. }
    13.  
    14. // Set the size of the structure before using it.
    15. pe32.dwSize = sizeof( PROCESSENTRY32 );
    16.  
    17. // Retrieve information about the first process,
    18. // and exit if unsuccessful
    19. if( !Process32First( hProcessSnap, &pe32 ) )
    20. {
    21. qDebug() << "Error: Process32First";
    22. CloseHandle( hProcessSnap ); // clean the snapshot object
    23. return;
    24. }
    25.  
    26. ProcTree procTree;
    27.  
    28. // Now walk the snapshot of processes, and
    29. // display information about each process in turn
    30. do
    31. {
    32. procTree[pe32.th32ParentProcessID] << pe32.th32ProcessID;
    33. }
    34. while( Process32Next( hProcessSnap, &pe32 ) );
    35.  
    36. CloseHandle( hProcessSnap );
    37.  
    38. QHashIterator<DWORD, QSet<DWORD>> i(procTree);
    39. while (i.hasNext()) {
    40. i.next();
    41. qDebug() << i.key() << ": " << i.value() << endl;
    42. }
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 24th May 2011 at 08:42. Reason: code tags

  6. #6
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Find child processes

    Just to avoid confusion :- ProcTree here is a QHash

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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Find child processes

    On Linux there is a concept of process groups. When you kill a group leader, all its children die automatically.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2011, 07:55
  2. Find non child dialog
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 6
    Last Post: 5th April 2010, 06:48
  3. Replies: 1
    Last Post: 6th March 2007, 15:27
  4. how to find a child widget?
    By TheRonin in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 10:30
  5. Enumerate processes using Qt
    By Ben.Hines in forum Qt Programming
    Replies: 5
    Last Post: 14th February 2006, 15:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.