How can you use system calls like fork and exec with Java and C++?
How can you use system calls like fork and exec with Java and C++?
The answer is yes for C++, and no for java.
for C++ I think you simply include the needed headers just like from C, but try and use the c* librarys when you can, since those have had conversions made to work better with C++. As for java, it has its own system for multithreading built in, check out its language reference for more infomation, just search for threads.
You can use threads in Java but you can't fork creating a separate process. There is a System command to run external processes though.
from memory, to do it in java...
note that I have also included the stuff required to communicate with the thread (ie the bufferedreader - and you can also define a writer) - but its only the first line in the try statemnet that's essential to start the new thread (assuming you want an external program as the new thread... otherwise it's a bit different).Code:String command = <whatever the hell you want>; try { Process p = Runtime.getRuntime().exec( command ); InputstreamReader converter = new InputStreamReader( p.getInputStream() ); input = new BufferedReader( converter ); currentLine = input.readLine(); } catch ( Exception e ) { System.err.println( e ); }
Like our friend above has mentioned check out the java API docs - they are mostly all you need, but since the docs are kinda useless without knowing what to look for I thought I'd better post this.
hope it helps
eccles
Last edited by eccles; 12-10-2004 at 03:34 AM.
VIM + gcc + beer... does life get any better?