C++ and Java System Calls

This is a discussion on C++ and Java System Calls within the Linux Programming forums, part of the Platform Specific Boards category; How can you use system calls like fork and exec with Java and C++?...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    241

    C++ and Java System Calls

    How can you use system calls like fork and exec with Java and C++?

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    The answer is yes for C++, and no for java.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    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.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    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.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    from memory, to do it in java...

    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 );
    }
    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).

    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?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21