[Gambas-user] Killing an arbitrary process

Tobias Boege taboege at ...626...
Thu Aug 8 12:01:41 CEST 2013


On Thu, 08 Aug 2013, Bruce wrote:
> I am using gnuplot to draw some graphs in a panel. To do this I create a
> gnuplot script and then Shell that script with
>         $sop = Shell scmd
> where $sop is a process.
> 
> Here's the problem.  When gnuplot creates the plot it does so as a new
> process, typically the pid is one more than $sop.Handle. When the
> program is closed the gnuplot process remains, so I want to kill it.
> 

Don't count on the child having PID = $sop.Handle + 1. This would be racy.

You want to kill the gnuplot process which you have in $sop? Then it's as
simple as:

Public Sub Form_Close()
  Try $sop.Kill()
End

> So, how can I kill an arbitrary process?  Better still, is there a way
> to get information on an arbitrary process so that I can check it is a
> child of $sop and if so kill it?
> 

I've just got a similar problem. But I had to watch for arbitrary processes
to be killed, start them again and detect their threads.

My solution was to use Linux' /proc interface. If this is portable enough
for you: Look at "/proc/" & Str$($sop.Handle) &/ "tasks". You'll find the
TIDs of all threads spawned by $sop.

If gnuplot does not use threads but distinct child processes (let the PID of
a process you suspect to be a child be denoted by CPID), you can find the
parent PID in /proc/$CPID/status and in /proc/$CPID/stat.

For the "stat" file, a parser is relatively simple:

$ awk '{print $4;}' </proc/$CPID/stat

Then use Exec ["kill", iThePid] to kill it.

Hope this at least gets you started.

Regards,
Tobi




More information about the User mailing list