Results 1 to 6 of 6

Thread: Get Process ID for a running application

  1. #1
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Get Process ID for a running application

    I need to detemine if an application/daemon is executing under Fedora 64-bit Linux.


    The application I need to check I do not have source for, and this is not
    the condition of trying to write a singleton solution.

    There have been a couple of solutions proposed at the following URLs for
    this:

    http://developer.qt.nokia.com/forums/viewthread/581

    Of the first solution, using the sysctl method, under Fedora, there are
    many undefines included KERN_PROC, KERN_PROC_ALL, and kinfo_proc. These
    appear to be BSD specific, and it is not clear that the implementation
    of sysctl under Fedora Linux would work if I were to cobble together the
    BSD header defining these values.

    Qt Code:
    1. struct kinfo_proc *result, *ptr;
    2. int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
    3. size_t length, i;
    4. result = NULL;
    5. length = 0;
    6.  
    7. if (sysctl((int *)name,(sizeof(name)/sizeof(*name)) - 1, NULL, &length, NULL, 0) == -1)
    8. return 0;
    9.  
    10. if ((result = (kinfo_proc *)malloc(length)) == NULL)
    11. return 0;
    12.  
    13. if (sysctl((int *)name, (sizeof(name)/sizeof(*name)) - 1, result, &length, NULL, 0) == -1)
    14. {
    15. free(result);
    16. return 0;
    17. }
    18.  
    19. for (i = 0, ptr = result; (i < (length/sizeof(kinfo_proc)); ++i, ++ptr)
    20. {
    21. // ptr->kp_proc.p_pid contains the pid
    22. // ptr->kp_proc.p_comm contains the name
    23. pid_t iCurrentPid = ptr->kp_proc.p_pid;
    24. if (strncmp(processName, ptr->kp_proc.p_comm, MAXCOMLEN) == 0)
    25. {
    26. free(sProcesses);
    27. return iCurrentPid;
    28. }
    29. }
    30.  
    31. free(result);
    To copy to clipboard, switch view to plain text mode 

    Moving on to the next suggested option, was to use a QDirIterator to simply
    iterate through the "/proc" file system looking for the executable name or
    cmd line which matches my application.

    The problem I am having with the 2nd approach is that "/proc" is so heavily
    nested and full of content that I blow up my stack as a result of the iteration.

    I have something like the following code segment:

    Qt Code:
    1. QDirIterator it("/proc", QDirIterator::Subdirectories)
    2. while (it.hasNext())
    3. {
    4. it.next();
    5. QFileInfo child = it.fileInfo();
    6.  
    7. if (child.fileName() == ".." || child.fileName() == ".")
    8. continue;
    9.  
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    I am out of ideas, anyone think of some other options or solutions.

    Is there a way of scoping QDirIterator so that it only goes 1 level deep?

    From what I can tell from inspecting "/proc" on my machine, there are
    running daemons for which I know the process id using the task manager,
    but inspecting the entries in "/proc" does not show anything for either
    the "cmdLine" or "exe" entries, which makes me question if this technique
    will work.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Get Process ID for a running application

    You could use "pgrep" to obtain the pid if the application is running.
    Qt Code:
    1. QProcess process;
    2. QString pgm("pgrep");
    3. QStringList args = QStringList() << "appName";
    4. process.start(pgm, args);
    5. process.waitForReadyRead();
    6. if(!process.readAllStandardOutput().isEmpty())
    7. // the app running
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    bob2oneil (8th September 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Get Process ID for a running application

    Thanks, I will give it a try

  5. #4
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Get Process ID for a running application

    Works great, thanks for the advice, must simpler than the other two approaches.

  6. #5
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Get Process ID for a running application

    Have to pay it forward a bit, QtCentre has been a great resource for me.

    Here is my source for the platform independant routine to get a list of PIDs for a program name. It works great as a singleton solution as
    well with the knowledge that when searching for your own program name, there will be one entry which is the program itself, so two entries
    indicates a duplicate running. I choose to return the list of PIDs in ASCII, but an integer collection could be used instead. The routine
    returns the count of running programs found for a program name for consumers that only care about the number.

    QtCentre as always rocks!

    Qt Code:
    1. unsigned int System::getProcessIdsByProcessName(const char* processName, QStringList &listOfPids)
    2. {
    3. // Clear content of returned list of PIDS
    4. listOfPids.clear();
    5.  
    6. #if defined(Q_OS_WIN)
    7. // Get the list of process identifiers.
    8. DWORD aProcesses[1024], cbNeeded, cProcesses;
    9. unsigned int i;
    10.  
    11. if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
    12. {
    13. return 0;
    14. }
    15.  
    16. // Calculate how many process identifiers were returned.
    17. cProcesses = cbNeeded / sizeof(DWORD);
    18.  
    19. // Search for a matching name for each process
    20. for (i = 0; i < cProcesses; i++)
    21. {
    22. if (aProcesses[i] != 0)
    23. {
    24. char szProcessName[MAX_PATH] = {0};
    25.  
    26. DWORD processID = aProcesses[i];
    27.  
    28. // Get a handle to the process.
    29. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
    30. PROCESS_VM_READ,
    31. FALSE, processID);
    32.  
    33. // Get the process name
    34. if (NULL != hProcess)
    35. {
    36. HMODULE hMod;
    37. DWORD cbNeeded;
    38.  
    39. if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
    40. {
    41. GetModuleBaseNameA(hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(char));
    42. }
    43.  
    44. // Release the handle to the process.
    45. CloseHandle(hProcess);
    46.  
    47. if (*szProcessName != 0 && strcmp(processName, szProcessName) == 0)
    48. {
    49. listOfPids.append(QString::number(processID));
    50. }
    51. }
    52. }
    53. }
    54.  
    55. return listOfPids.count();
    56.  
    57. #else
    58.  
    59. // Run pgrep, which looks through the currently running processses and lists the process IDs
    60. // which match the selection criteria to stdout.
    61. QProcess process;
    62. process.start("pgrep", QStringList() << processName);
    63. process.waitForReadyRead();
    64.  
    65. QByteArray bytes = process.readAllStandardOutput();
    66.  
    67. process.terminate();
    68. process.waitForFinished();
    69. process.kill();
    70.  
    71. // Output is something like "2472\n2323" for multiple instances
    72. if (bytes.isEmpty())
    73. return 0;
    74.  
    75. // Remove trailing CR
    76. if (bytes.endsWith("\n"))
    77. bytes.resize(bytes.size() - 1);
    78.  
    79. listOfPids = QString(bytes).split("\n");
    80. return listOfPids.count();
    81.  
    82. #endif
    83. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to bob2oneil for this useful post:

    bruceariggs (23rd October 2011)

  8. #6
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get Process ID for a running application

    It's not entirely independent approach because You don't check if pgerp is present in the system.
    And btw. this is potential security risk, because pgrep can be changed and will do "something else".
    Btw. You could use i.e. process pidof.

    Universal approach IMHO is to use (in Linux) /proc file sys. or sysctl

    PS. Thanks for shearing and treat this post only as my 2 cents on this subject.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

Similar Threads

  1. QTcreator - Check 1 process is running ?
    By nhs_0702 in forum Qt Programming
    Replies: 3
    Last Post: 29th April 2010, 10:35
  2. Replies: 4
    Last Post: 9th November 2009, 21:12
  3. Destroyed while process is still running
    By qtzcute in forum Qt Programming
    Replies: 5
    Last Post: 23rd July 2009, 08:26
  4. Connecting with QProcess to an already running process
    By high_flyer in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2007, 10:31
  5. Check wheter a process is running
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2006, 12:35

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.