# How to close all other MathKernel before opening the new one?

I use MathLink to call Mathematica from C program. After any opening to Mathematica, I do close the connection. However, I don't know why some connections are still there. I've been debugging for days but couldn't find the point.

Thus, I want to ask if there is any way to close all connection before I open the new one? Seems like not an optimal solution but for now its ok for me.

Thanks,

-

Try:

LinkClose[#] & /@ Links[];


It maps the list of all Links (Links[]) through LinkClose

-
Sorry, how to do it from C? –  Loi.Luu Nov 13 '13 at 14:55
@Loi.Luu how about sending an MLPutFunction with an EvaluatePacket containing Quit[] to close your kernel? page 29 edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf –  PlaysDice Nov 13 '13 at 15:29
I want to close other connections which are supposed to be closed already. To close the current connection, I call the MLPutFunction(lp, Exit, 0L); statement. –  Loi.Luu Nov 13 '13 at 15:34
@Loi.Luu good point. Is a 'simple' way to use MLPutFunction to get MMA to do a variant of the LinkClose map (above), but not drop the current Link ? –  PlaysDice Nov 13 '13 at 15:43
@Loi.Luu. As written, yes, in MMA it closes all open 'Links' (incl JLinks etc). You may have to figure out whether the kernel can only close Links that are open to it (i.e not 'zombie' links left from other kernels) and how to specify the 'Link' you need to retain. If the one you need to preserve is the last 'Link' you could make the command 'LinkClose[#]&/@Drop[Links[],-1]' i.e 'LinkClose' the list of all 'Links' but omit the last one –  PlaysDice Nov 13 '13 at 16:45