LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2007, 09:52 AM   #1
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Finding pid of a process opened by "popen"


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.

Last edited by Guttorm; 03-12-2007 at 10:01 AM.
 
Old 03-12-2007, 01:47 PM   #2
iamnothere
Member
 
Registered: Feb 2007
Location: UK
Distribution: Slamd 64, Slackware
Posts: 46

Rep: Reputation: 16
Quote:
Is there a way to find the pid of the process started by popen?
I don't think so. Why not just open a pipe(2) and use fork/exec. As for (not) killing the caller, you can use setpgrp(2) to create a new process group (i.e. set the process group id of the C wrapper to itself).
 
Old 03-12-2007, 02:35 PM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Original Poster
Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Thank you!

The real problem was I didn't know about process groups. I searched on it, but found all kinds of weird pages - all I read was "man kill".

Edit:
Eh, that was "man 2 kill"

Last edited by Guttorm; 03-12-2007 at 02:36 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
function for getting pid of "any" process nice_indian Programming 2 01-12-2005 09:54 AM
using bash to find the pid of a process called "name" poiuytrewq Linux - Newbie 3 10-25-2004 09:01 PM
RedHat AS3 problem :: "lease broken - owner pid = ####" dmoorhouse Red Hat 1 09-24-2004 12:45 PM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration