Wow, I haven’t update this blog since November… long time ago!
I have to say to work has pretty much sucked up my life: have been very busy there (and probably will be more in the future). The cool part is that I have been working with different technologies, most of them unfortunately had to deal with Windows, some of them with both Windows and Linux, but it has been fun.
I’ve been playing around with JNA (Java Native Access), a bridge between the JVM and the underlying operating system: like JNI, but easier to use since what you only need to write is Java code. It all started with the problem of killing a process tree in Windows having only the root process PID: Windows, unlike Linux, will kill only that process PID, and the rest of the tree will be detached from where it was, staying alive.
The old way of killing a Windows process in our software was achieved with some kind of ActiveX magic (voodoo-poison, I say) and the use of the small Windows utility taskkill. Moving to use Windows functions to create processes, taskkill couldn’t be used anymore: what I experienced were strange behaviors (not always killing all the processes), and also lots of BSOD (still don’t know the exact cause, but removing the use of taskkill resolved the issue). Finding a way to kill a process tree in Windows has been a hard task: looking into MSDN is kind of a mystic experience. I find it a mess. But as always, when you find the solution to your problem, you find it easy and wonder why you didn’t think first about that.
So, to sum it up, if you need to create a process that spawns other processes, and you need to kill that process tree, these are the Windows functions from the kernel32.dll I used:
There are other things it is necessary to take into account, like the creation of the necessary structures, but basically you need to create a job object, create the process and attach it to the job object, and when it’s necessary, terminate the job object. All of this has been done with JNA, writing only Java, and is pretty robust: I tested it on a remote computer for 7 continuous days, creating and killing more than 500000 processes in batch of 4-5 processes at a time, processes that didn’t last more than 5-6 seconds (I even tested it for 3 days with 2-3 hours long processes).
An interesting part has been finding a way to check if a process was still alive, or had completed its run or had already been killed. The first implementation for this was using GetHandleProcessCount. Basically this functions checks how many handles a Windows process still has open, but with such many processes being created in a short timespan, I experienced OutOfMemory exceptions in the JVM (probably caused by many JNA Pointer objects being created and not cleaned up in time by the GC, but I didn’t investigate the cause). The solution has been using the WaitForSingleObject function, monitoring its return state. I only hope the API will not change with some of the too many Windows updates…
Now I’m playing with some really cool stuff: numpy and scipy. I’m trying to “embed” Python into our software, or at least to use numpy and scipy libraries and routines to avoid having to write complex mathematical algorithms when there are already valid and strong ones out there, and also to be able to write internal routines in an easier way with Python. I’m running some comparative tests right now (my computer at the office is still running): I’m trying to see if the same algorithm written in Java is slower or faster than the numpy/scipy internal functions. Right now I’m doing 1500 iterations over square matrices ranging from 10×10 to 700×700 calculating the determinant, and tomorrow I’ll implement the inverse of the matrix. Right now, looks like numpy is faster than Java on matrices from 150×150 and up.
Funny times ahead… also because on March 19th I’ll be heading to Chicago for the Desktop Help Summit with Shaun, Paul, Jim and other cool guys! Looking forward to that!