> > 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
--