Hi all
I hava a Java program that does some picture manipulation that someone else wrote. Unfortunately I don't have source, but I have contact with the developer of the program.
The problem is that the program hangs when an exception occurs. It will write a message like:
Quote:
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space"
|
Then the program will hang. If I run the java command from a shell, I have to hit Control-C to get back to the prompt.
The developer is fairly confident the bug is in JIMI (a Java image library) and not in his program. He said he can try to look into this, but it can take a lot of time, and he may not be able to solve it.
My problem is I am trying to run this Java program from another program - the other program then never gets to know some error occured, and will hang as well.
So I wrote a little C program as a wrapper - running the Java program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char cmd[] =
"java [lots of parameters...] 2>&1";
#define MAX_LINE_LEN 1000
int main(int argc, const char *argv[])
{
char line[MAX_LINE_LEN];
FILE *fp = popen(cmd,"r");
if (fp == NULL) {
fprintf(stderr,"Failed executing command: %s\n", cmd);
}
while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
printf("line: %s\n", line);
if (strstr(line,"Exception") != NULL) {
printf("DEAD!!!");
//pclose(fp); // This line hangs this program.
//kill(0,9); // Kill this processgroup - but this kills the caller as well.
return 1;
}
}
pclose(fp);
return 0;
}
I'm nearly there. This program will find the word "Exception", it will print "DEAD!!!", but now I don't know how to kill the process opened by popen.
The "pclose(fp)" hangs the program, I have to hit Control-C like before.
The kill(0,9) should kill the processgroup. Unfortunately, when I tried a little script running the C program, that script got killed as well.
Just "return 1;" seems to work. But the Java process is then not killed, taking up a lot of memory which will never be released. The same thing happened when I tried "kill(getpid(),9)".
Is there a way to find the pid of the process started by popen?
Or is there a way to kill the program including any children, but not kill the script starting this command?
Thanks in advance.
Guttorm.
Edit:
I forgot to add, I cannot use grep or anything, a "killall java" or using grep on "ps", since there might be more than one process running at once.