Recent

Author Topic: List of process  (Read 2571 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1886
    • Lazarus and Free Pascal italian community
List of process
« on: June 25, 2019, 08:27:35 am »
Hi guys, where can I find an example of multi-platform system process management? My purpose is to understand on an Ubuntu system whether Firefox is open or crashed. With the excuse I'd like to find a cross-platform solution. Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

dbannon

  • Hero Member
  • *****
  • Posts: 587
    • tomboy-ng, a rewrite of the classic Tomboy
Re: List of process
« Reply #1 on: June 25, 2019, 09:35:37 am »
I suggest you need to do a different test on each platform and separate them with {$ifdef sort of things.  You are using os dependent commands, on linux its a ps command, on the mac its very similar (slightly different options from memory) but windows quite different.

David
Lazarus 1.8, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

xinyiman

  • Hero Member
  • *****
  • Posts: 1886
    • Lazarus and Free Pascal italian community
Re: List of process
« Reply #2 on: June 25, 2019, 11:01:48 am »
But isn't there a free pascal command for linux? should I parser the result that returns the command ps -x?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

trev

  • Full Member
  • ***
  • Posts: 102
Re: List of process
« Reply #3 on: June 25, 2019, 12:02:58 pm »
On macOS and FreeBSD you can use the sysctl interface. I know nothing of Linux, but if it has a sysctl interface, it might also work.

Check out the CTL_KERN, KERN_PROC and KERN_PROC_ALL MIB elements.
o Lazarus v2.1.0 r61574, FPC v3.3.1, macOS 10.14.5
o Lazarus v2.0.0, FPC v3.0.4, FreeBSD 12.0
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04

dbannon

  • Hero Member
  • *****
  • Posts: 587
    • tomboy-ng, a rewrite of the classic Tomboy
Re: List of process
« Reply #4 on: June 25, 2019, 12:37:43 pm »
This will check Linux and Mac .....

Code: Pascal  [Select]
  1. function AppsRunning(const ExeName: string): integer;
  2. var
  3.   t: TProcess;
  4.   s: TStringList;
  5.   Index : Integer = 0;
  6. begin
  7.   Result := 0;
  8.   t := tprocess.Create(nil);
  9.   t.Executable := 'ps';
  10.   {$IFDEF DARWIN}
  11.   t.Parameters.Add('x');
  12.   t.Parameters.Add('-c');
  13.   t.Parameters.Add('-ocommand');
  14.   {$ELSE}
  15.   t.Parameters.Add('-C');
  16.   t.Parameters.Add(ExeName);
  17.   {$ENDIF}
  18.   t.Options := [poUsePipes, poWaitonexit];
  19.     try
  20.     t.Execute;
  21.     s := TStringList.Create;
  22.       try
  23.         s.LoadFromStream(t.Output);
  24.         repeat
  25.                 Index := UTF8Pos(ExeName, s.Text, Index+1);
  26.                 if Index <> 0 then inc(Result);
  27.         until Index = 0;
  28.       finally
  29.       s.Free;
  30.       end;
  31.     finally
  32.     t.Free;
  33.     end;
  34. end;  

based on uappisrunning.pas from wiki ....

Davo
Lazarus 1.8, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: List of process
« Reply #5 on: June 25, 2019, 12:54:49 pm »
You can open /proc as a file directory  and obtain all running process ID's. You can subsequently query those processes for more information.
I'll see if I can come up with a simple example. That's the way ps and top work. For sure you do not need Tprocess and the likes.
Under Windows, you need a bit different code
« Last Edit: June 25, 2019, 12:56:40 pm by Thaddy »
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7224
Re: List of process
« Reply #6 on: June 25, 2019, 12:59:43 pm »
But isn't there a free pascal command for linux? should I parser the result that returns the command ps -x?

No, typically these are not publically documented APIs on *nix, so it is *nix version specific.  Parsing ps might even be OS specific, though less so than the APIs.

However tools like PS must also access something, and in practice there are some common cases, Linux usually favours /proc, but in recent times commands might also use systemd using D-Bus like systems. On FreeBSD it tends to be sysctl.

Further complications might be permissions as system tools are sometimes setuid.

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: List of process
« Reply #7 on: June 25, 2019, 01:06:36 pm »
https://wiki.freepascal.org/FindAllFiles works pretty good on linux. and /proc
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

xinyiman

  • Hero Member
  • *****
  • Posts: 1886
    • Lazarus and Free Pascal italian community
Re: List of process
« Reply #8 on: June 25, 2019, 03:03:15 pm »
You can open /proc as a file directory  and obtain all running process ID's. You can subsequently query those processes for more information.
I'll see if I can come up with a simple example. That's the way ps and top work. For sure you do not need Tprocess and the likes.
Under Windows, you need a bit different code

I would be immensely grateful for the example. I just need to see the pid and the name of the application to understand. Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

dbannon

  • Hero Member
  • *****
  • Posts: 587
    • tomboy-ng, a rewrite of the classic Tomboy
Re: List of process
« Reply #9 on: June 26, 2019, 10:37:25 am »
Using TProcees to run a ps command would see a heap easier than searching /proc yourself. A variant of ps works on just about all unix like systems, /proc is a linux and a few other OS thing only. The Mac for example does not use /proc (from memory, don't have my Mac with me).

The ps command does vary a little, some BSD variants (inc Mac) do need a different command syntax to Linux.  OSF was bit different too. But its quite easy to use ifdef to determine the actual option to ps and then same code to run the command and read its result is the same.

Even on systems using /proc, whats there has changed from time to time, while its unlikely anything there now will go away, its far better to allow ps to adjust for lower level behavior.

You do mention you are looking for cases where firefox has crashed. That might mean you need to watch for zombie processes, they will still be listed but not responding. ps will show them as 'z'. Mac seems particularly good at zombie processes. And note that firefox launches a number of processes......

Davo
Lazarus 1.8, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: List of process
« Reply #10 on: June 26, 2019, 11:40:23 am »
You can open /proc as a file directory  and obtain all running process ID's. You can subsequently query those processes for more information.
I'll see if I can come up with a simple example. That's the way ps and top work. For sure you do not need Tprocess and the likes.
Under Windows, you need a bit different code

I would be immensely grateful for the example. I just need to see the pid and the name of the application to understand. Thank you
It is not "just", it is more complex. Retrieving the pids is easy (I hope you tried my comments!) you are not a noob.
Subsequently you have to interrgogate those pids to retrieve further info by using fpxxx calls. But I will - given time - provide a complete example.
It is not very difficult, but requires some effort. NOT "just" Sometimes what seems easy on the mind is actually a lot more effort.
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.