Java waitfor waits for completion of forked child

P

pwu

I'm starting a C program from Java via the exec method. The C program
forks off a child and execvp another program over the child process.
The parent process exits after printing out the process pid.
The Java program does a waitFor. However the waitFor waits for both the
main C program AND the child. I only want to wait for the main C
program
only.

The Java program:
// Get our runtime
me=Runtime.getRuntime();
command=launcher + " /bin/ksh " + editedwrapper;
newproc=me.exec(command);
pin = new BufferedReader(new
InputStreamReader(newproc.getInputStream()));
// Echo back what the program spit out
while ((pline=pin.readLine())!=null)
{
System.out.println(pline);
}
newproc.waitFor();
status=newproc.exitValue();


The C program forks off a new process as follows:
/* Create the new process which is a copy of this process */
if ((pid=fork()) < 0)
{
exit(-1);
};
/* The child process: */
if (pid == 0)
{
if (execvp(argv[1], newarglist) < 0) /* execute the command */
{
exit(-1);
}
/* Child goes not proceed past here */
}

/* Parent */
exit(0);

The java waitFor waits for BOTH the exec'ed process and the child. I
want
java to wait for the exec'ed process but not the child.

How do I do this?
 
G

Gordon Beaton

I'm starting a C program from Java via the exec method. The C
program forks off a child and execvp another program over the child
process. The parent process exits after printing out the process
pid. The Java program does a waitFor. However the waitFor waits for
both the main C program AND the child. I only want to wait for the
main C program only.

waitFor() does what you want - it only waits for the process started
directly from Java. However your application doesn't reach that
statement until EOF has been reached on the InputStream, which is
shared by the child process and all of its descendents.

You need to rethink your handling of the standard streams in your C
processes. One solution is to close or redirect the streams in the
main C process before forking the child.

As well, you seem to be running a shell as part of your command. It's
unclear why without seeing the complete command line, but in most
cases a shell is unnecessary.

/gordon
 
S

Sebastian Scheid

I'm starting a C program from Java via the exec method. The C program
forks off a child and execvp another program over the child process.
The parent process exits after printing out the process pid.
The Java program does a waitFor. However the waitFor waits for both the
main C program AND the child. I only want to wait for the main C
program
only.

The Java program:
// Get our runtime
me=Runtime.getRuntime();
command=launcher + " /bin/ksh " + editedwrapper;
newproc=me.exec(command);
pin = new BufferedReader(new
InputStreamReader(newproc.getInputStream()));
// Echo back what the program spit out
while ((pline=pin.readLine())!=null)
{
System.out.println(pline);
}
newproc.waitFor();
status=newproc.exitValue();

In addition to Gordon: don't forget to read the errorStream. The best is to
read the input- and errorStream in separate threads. So you don't get
problems with blocking buffers.

Regards
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top