Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

JAVA process hang when expectts error stream from C++ application

Thread view: 
krishnamurthi.g@gmail.com - 20 Sep 2007 07:10 GMT
Hi friends,

My JAVA program has to call c++ (kkk) application and JAVA opens 2
threads to read outputstream and errorstream from c++ application.
See the code below). Since errorstream is expecting some data it hangs
unless c++ feeds the same.
When I used alternative 'perror("")'/'perror(NULL)' in kkk.c hang gets
solved but c++ standalone program keeps throwing dummy error on
screen.
Could anybody help me here how it can be rectified.
Thanks in advance
- Krish

------------
start.java:
-----------
public class start
{
    public static void main(String [] args) throws
IOException,InterruptedException
    {
        String cmd = "/home/user/kkk";
        Process process = Runtime.getRuntime().exec(cmd);
        MyReader outReader = new MyReader(process.getInputStream());
        Thread outThread = new Thread(outReader);
        outThread.start();
        MyReader errReader = new MyReader(process.getErrorStream());
        Thread errThread = new Thread(errReader);
        errThread.start();
        System.out.println("exit code is " + process.waitFor());
        //process.destroy();
        //    process.getInputStream().close();
        //    process.getErrorStream().close();
       outThread.join();
       errThread.join();
        System.out.println("joins completed");
  }
}

/***** MyReader.java *******/

import java.io.*;
public class MyReader implements Runnable
{
    private final InputStream is;
    public MyReader(InputStream is)
    {
        this.is = is;
    }
    public void run()
    {
        // normally would read in while here, but these threads never get
any
        // they are blocked on the first read() call forever.
        int length;
        try
        {
          length = is.read();
          if (length == -1)
          {
            System.out.println("-1");
            return;
          }
          if (length == 0)
          {
            System.out.println("0");
            return;
          }
          System.out.println("read something");
        } catch (Throwable t) {
        System.out.println(t);
     }
 }
}

kkk.c:
-------
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
# include <sys/wait.h>

int main()
{
       perror ("");
//    perror (NULL);
       return 0;
}
Gordon Beaton - 20 Sep 2007 07:37 GMT
> My JAVA program has to call c++ (kkk) application and JAVA opens 2
> threads to read outputstream and errorstream from c++ application.
> See the code below). Since errorstream is expecting some data it
> hangs unless c++ feeds the same.

I don't understand how this is a problem - it will only "hang" while
the child process is still running. But when the child exits, EOF will
be indicated by is.read() allowing the reader thread to exit too.

If you know in advance that the child will *never* write to stderr,
close it in the c++ program. That too will cause EOF to be indicated
by in.read(), and the reader thread can exit.

/gordon

--
krishnamurthi.g@gmail.com - 20 Sep 2007 08:14 GMT
> > My JAVA program has to call c++ (kkk) application and JAVA opens 2
> > threads to read outputstream and errorstream from c++ application.
[quoted text clipped - 12 lines]
>
> --

======
Dear Gordon,

Thanks for your quick response.
Sorry that I forgot to mention one more thing.
The kkk c++ exe calls myfork c++ exe which simply forks and keeps
child in loop and parent gets exited.
In this scenario, kkk too gets exited and myfork is running in loop
and JAVA application hangs if there
is no perror() in kkk. If perror() is inserted JAVA won't hang at all.
So independantly running kkk will throw

# kkk
I am child 23520
I am Parent 23519
Invalid argument    <----- error message
#

Thanks
- Krish

kkk.c:
-------
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
# include <sys/wait.h>

int main()
{
    system("/home/user/myfork");
       perror ("");
//      perror (NULL);
       return 0;
}

myfork.c:
--------
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
# include <sys/wait.h>
int main()
{
       switch(fork())
       {
         case 0:
               printf("\nI am child %d", getpid());
               break;
         case -1:
               printf("\nError");
               break;
         default :
               printf("\nI am Parent %d", getpid());
               exit(0);
       }
       while(1)
       {
       }
}
Gordon Beaton - 20 Sep 2007 08:57 GMT
> Sorry that I forgot to mention one more thing. The kkk c++ exe calls
> myfork c++ exe which simply forks and keeps child in loop and parent
> gets exited.

Don't waste people's time by posting half the problem. The fork() is
extremely significant to the issue.

> In this scenario, kkk too gets exited and myfork is running in loop
> and JAVA application hangs if there is no perror() in kkk. If
> perror() is inserted JAVA won't hang at all.

It isn't apparent what you are trying to achieve by creating this tree
of processes.

Every time you fork(), both parent and child processes share the same
set of descriptors. Your use of system() complicates the picture
somewhat, since it also creates a shell in addition to the "real"
child process. And all of these processes share the same set of
descriptors.

Here is one simple rule: as long as any process still holds an open
stderr descriptor, the reader thread will continue to block on read.
EOF will be reached only when there are *no* remaining processes
holding the descriptor.

It isn't necessary to write nonsense messages to stderr to make this
work, but you need to keep proper track of your processes and
descriptors, closing any unused ones as you go, or making sure that
all of your processes exit properly.

/gordon

--
Roedy Green - 20 Sep 2007 07:47 GMT
>My JAVA program has to call c++ (kkk) application and JAVA opens 2
>threads to read outputstream and errorstream from c++ application.
>See the code below). Since errorstream is expecting some data it hangs
>unless c++ feeds the same.

see http://mindprod.com/jgloss/exec.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.