• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Create a process in java which stays alive always

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a scenario, where I need to start a C program in Java. This C program('command') started using ProcessBuilder, takes argument through the OutputStream and returns result through the InputStream. Here we need to start the C process once and the different operations need to be performed by changing the arguments passed to it. It is working fine for the first iteration in the below code, but the problem is the process is died after one iteration is done, i.e.,returning false for isAlive() once the single iteration is done.

 
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I shall duplicate this discussion in our threading forum.

Don't know about the process, but....
Why are you repeatedly creating data output stream objects in line 33 and not closing them? Why are you using data output streams rather than something like a print writer to write text? Do you really intend your loop to keep running forever?
 
Raghu varan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Campbell Ritchie
It is a server process which needs to be run forever.
 
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By "the process dies", do you mean the process no longer exists? If so, you should look into how you run this process - maybe start it outside of the Java code.

Also, it may not be a good idea to close any of the process' stream, as this is code is doing in line 23. The process may not react well to that.

The code also seems to mix running a server process with dealing with the process it started - it might be a good idea to separate those concerns.
 
Raghu varan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried removing the line 23 related to close(), but it is hanged at that stage itself. So I tried in that way
 
Tim Moores
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to handle all streams of a Process, otherwise a hanging process is just one of the unintended consequences.

This article talks about what you need to watch out for, even though it's geared towards the older Runtime.exec rather than ProcessBuilder: https://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html. The issues regarding Process objects are the same, though.
 
Destroy anything that stands in your way. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic