3

Okay, I got into a conversation with a friend about Ada (I'm the local proponent here), and in his project he's having a pain trying to get Java (using JNI) to get the applications running on the client machine (only Windows, Mac, and Linux) to get a listing of applications.

I'm not familiar with Macs at all, and my Linux experience is mostly user-end within academia.

So, my question is this: does anyone know a good cross-platform way to get the process-list?

My solution would be to use a package spec with a general function returning the list in the manner the Java expects it and throw together three different bodies for each of the platforms which would get the process-list according to that system and compile the (resultant) three binaries for those targets individually.

Is there a [good] way to do it w/o resorting to three different versions? (This is an Ada question, but Java solutions are welcome.)

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
Shark8
  • 4,095
  • 1
  • 17
  • 31
  • Java is mentioned only because it is part of the existing problem, er, solution that is being used. While Java-centric answers are welcome (and encouraged), I am quite interested in the Ada-centric answer as the problem did pique my interest and Ada is so high-level that I feel as if I'm missing something in my gut/initial approach. – Shark8 Oct 18 '11 at 01:41
  • 1
    Cross-posted to [`comp.lang.ada`](http://groups.google.com/group/comp.lang.ada/browse_thread/thread/33f90678fd18cbc7). – trashgod Oct 18 '11 at 01:46
  • I was wondering if I should have mentioned I posted it there too... I suppose it wouldn't have hurt. – Shark8 Oct 18 '11 at 01:49
  • +1 for an interesting question; `cla` usually has good discussion; I defer to you on whether to add the [tag:java] tag; there's may be a cross-platform framework for this that I don't know about. – trashgod Oct 18 '11 at 02:02
  • I'm afraid this is really more of an OS issue than a language one. Ada is a language that tries to encapsulate a lot of what other languages leave to the OS, but I'm afraid heavyweight processes are out of its scope. – T.E.D. Oct 18 '11 at 14:52

5 Answers5

3

Java has no cross-platform API to list running processes. ProcessBuilder may be used to excecute the ps command, as shown here and here. The (rough) equivalent in Ada would be GNAT.Os_Lib.Spawn in GNAT. I'm not sure about other implementations.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hm, I'll have to poke at OS_Lib then. – Shark8 Oct 18 '11 at 01:47
  • To be honest, I've always avoided it, but there's an example [here](http://oopweb.com/Ada/Documents/AdaLinux/Volume/14.html). – trashgod Oct 18 '11 at 01:52
  • What's the reason to avoid it? – Shark8 Oct 18 '11 at 01:55
  • 2
    It's not standard. The spec may change, typically _after_ I commit code that uses it. :-) – trashgod Oct 18 '11 at 02:08
  • @Shark8 - Agree with trashgod. Anything starting with `GNAT` is portable (to some degree) across the various Gnat implementations, but not across compilers and is not in the standard. That's OK if you are just trying to get it working in one place, but true portability may require more thought. – T.E.D. Oct 18 '11 at 15:00
2

JavaSysMon can provide a list of running processes (as well as other system information) in a platform-independent manner. Currently it supports Mac OS X, Linux, Windows, and Solaris. As an added bonus, it is BSD licensed.

Wiki

JavaDocs

jt.
  • 7,625
  • 4
  • 27
  • 24
1

Do you just mean to get a list of running processes?

If so, you can just Google the commands to get this (1) the name of the OS on which the program is running, then (2) run Runtime.getRuntime.exec(stringCommandToGetProcessList); to based on #1, and output the results.

You don't need a different Java binary for every OS. You only need one. Just Google the command to find the OS name/version, and the command to get the list of running applications.

You also don't need JNI to do this. Use the Runtime class to run commands as if they were on the command-line.

There's no cross-platform way to do it, because the commands are different on each OS. But since there's only three major OS's (maybe a dozen total that you want to support, in some crazy extreme example), then it's just a matter of making a list of the 12 different commands to do this.

On Macs, and many Linux versions, OS name/version:

$ uname -a
Darwin normalocity 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64

Running processes (by highest usage):

$ top
Processes: 92 total, 5 running, 87 sleeping, 408 threads                                                          20:38:35
Load Avg: 0.18, 0.20, 0.17  CPU usage: 7.26% user, 1.95% sys, 90.78% idle
SharedLibs: 6272K resident, 7300K data, 0B linkedit. MemRegions: 12204 total, 730M resident, 29M private, 393M shared.
PhysMem: 1076M wired, 1184M active, 1859M inactive, 4119M used, 4062M free.
VM: 207G vsize, 1041M framework vsize, 1851231(0) pageins, 603(0) pageouts.
Networks: packets: 1727104/1746M in, 984226/269M out. Disks: 295257/6745M read, 397634/15G written.

PID    COMMAND      %CPU TIME     #TH   #WQ  #PORT #MRE RPRVT  RSHRD  RSIZE  VPRVT  VSIZE  PGRP  PPID  STATE    UID
12547  top          3.5  00:00.26 1/1   0    24    34   1208K  264K   1784K  17M    2378M  12547 12217 running  0
12217  bash         0.0  00:00.08 1     0    17    25   1328K  856K   1988K  17M    2378M  12217 12211 sleeping 502
12212  bash         0.0  00:00.08 1     0    17    25   1276K  856K   1980K  9688K  2378M  12212 12200 sleeping 502
12211  login        0.0  00:00.01 1     0    22    54   512K   312K   1648K  11M    2379M  12211 12196 sleeping 0
12202  bash         0.0  00:00.07 1     0    17    25   1276K  856K   1980K  9688K  2378M  12202 12199 sleeping 502
12201  bash         0.0  00:00.07 1     0    17    25   1276K  856K   1980K  9688K  2378M  12201 12198 sleeping 502
12200  login        0.0  00:00.01 1     0    22    54   512K   312K   1648K  11M    2379M  12200 12196 sleeping 0
12199  login        0.0  00:00.01 1     0    22    54   512K   312K   1648K  11M    2379M  12199 12196 sleeping 0
12198  login        0.0  00:00.01 1     0    22    54   512K   312K   1648K  11M    2379M  12198 12196 sleeping 0
12196  Terminal     33.9 00:01.84 5     1    114-  137  5736K+ 32M    23M+   90M    2768M  12196 300   sleeping 502
11803- Google Chrom 0.0  04:06.79 7     1    99    365  45M    84M    79M    112M   1199M  11788 11788 sleeping 502
11800- Google Chrom 0.0  00:00.25 7     1    98    215  9632K  77M    23M    110M   1090M  11788 11788 sleeping 502
11799- Google Chrom 0.0  00:07.92 7     1    99    288  25M    82M    43M    109M   1108M  11788 11788 sleeping 502
11797- Google Chrom 0.0  00:01.49 7     1    99    316  27M    81M    48M    111M   1109M  11788 11788 sleeping 502
11796- Google Chrom 0.0  00:00.44 4     1    91    115  2824K  65M    8304K  96M    1012M  11788 11788 sleeping 502
11795- Google Chrom 0.0  00:00.96 7     1    98    215  9172K  77M    23M    111M   1091M  11788 11788 sleeping 502
11794- Google Chrom 0.0  00:07.64 8     1    100   294  20M    75M    36M    113M   1101M  11788 11788 sleeping 502
11793- Google Chrom 0.0  00:01.42 8     1    95    185  9732K  73M    24M    104M   1057M  11788 11788 sleeping 502
11788- Google Chrom 0.6  04:04.31 30    1    307   390  61M    110M   96M    254M   1298M  11788 300   sleeping 502
4328   ssh-agent    0.0  00:00.19 2     1    33    63   1300K  396K   2688K  59M    2420M  4328  300   sleeping 502
3855-  Microsoft Of 0.0  00:36.14 4     1    121   337  12M    30M    22M    93M    1027M  3855  300   sleeping 502
492    AppleSpell   0.0  00:10.56 2     1    34    72   4608K  9028K  10M    88M    2469M  492   300   sleeping 502
jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Hm, I think there was a reason they couldn't do that; I don't know what it is as I only got the "executive" version of the complaint. – Shark8 Oct 18 '11 at 01:35
  • 1
    Note that `ps` and `top` options vary by platform. – trashgod Oct 18 '11 at 01:44
  • Did they not want to parse the output of said commands? Regex is your friend. :) – jefflunt Oct 18 '11 at 01:45
  • I think all they're interested in is the PID and the Application... it's for an desktop/application sharing suite (IIUC) and so I'm assuming they just want a way to grab all the 'interesting' applications (probably their own filtering procedure) which they will then pass over to some user-input/selection method which will give them the main window of the indicated program. – Shark8 Oct 18 '11 at 01:47
  • regex is NOT my friend; I've seen a 'simple' parse go to crap in a hurry because they thought regex was appropriate, but as soon as things get moderately complicated (say the needs change, or more likely someone notices the regex didn't work with a special case), I find it becomes onerous. (My experience.) – Shark8 Oct 18 '11 at 01:53
  • Empathy; I'd be tempted to `awk` the columns I wanted in the command line. – trashgod Oct 18 '11 at 02:04
  • @trashgod - Yeah. I'm picturing a platform-specific awk script outputting a "standard" (documented) format of data to the Ada/Java program. – T.E.D. Oct 18 '11 at 15:03
1

You were almost at the Ada solution. As you only want 1 procedure to execute & look at the system call (top/ps in linux/unix) i would suggest a separate procedure. This will live in its own directory, and only be referenced by the correct compilation (per os). As for the actual commands per os, that is not part of my answer.

NWS
  • 3,080
  • 1
  • 19
  • 34
1

Ada doesn't really have the concept of "processes" within the language. In fact, Ada code can run on platforms that do not support heavy processes at all (eg: Many smallish embedded platforms, like vxWorks).

That means you are going to have to use some kind of API (most likely supplied by your OS) to get that information.

If your OS supports POSIX, you may be able to use Posix bindings like Florist to get that info. There are full Unix subsystems available for Windows (Cygwin) and I believe MacOS is built on a flavor of Unix. So it might be possible to use Unix as sort of a lingua-franca so you can get your process info from a single (POSIX) API.

Now where Java is concerned, there are two issues: The Java language and the Java Platform (JVM). Java language fans like to conflate the two, but there are actually Ada compilers that target the JVM, and they can call all the same JVM API's that code written in the Java language can call. If there's one that allows Java programs to get a list of all the threads or processes that the JVM knows about, you could call that same routine from Ada too (if it is running under the JVM as well).

T.E.D.
  • 44,016
  • 10
  • 73
  • 134