Monday 15 February 2010

Adapting C fork code to a Java program -



Adapting C fork code to a Java program -

i trying create little programme using java fork 2 new kid processes. it's beginner's programming class who's tutorials in c, i'm looking help understand code tidbit trying , best way adapt java-based programme (to build on it).

#include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid t pid; /*fork kid process*/ pid = fork(); if (pid < 0) { /*error occurred*/ fprintf(stderr, "fork failed"); homecoming 1; } else if (pid == 0) {/*child process */ execlp("/bin/ls", "ls", null); } else { /*parent process*/ /*parent wait kid finish */ wait(null); printf("child complete"); } homecoming 0; }

update:

i supposed attach id each kid process , and parent, printing info when kid process executes , printing termination notification when terminates. see bit of code above lists contents of current directory , prints "child complete" when process has terminated. listing of entire directory considered 1 process? if so, where/how sec new kid process come picture?

in java, might -

public static void main(string[] args) { seek { process p = runtime.getruntime().exec("/bin/ls"); final inputstream = p.getinputstream(); thread t = new thread(new runnable() { public void run() { inputstreamreader isr = new inputstreamreader(is); int ch; seek { while ((ch = isr.read()) != -1) { system.out.print((char) ch); } } grab (ioexception e) { e.printstacktrace(); } } }); t.start(); p.waitfor(); t.join(); system.out.println("child complete"); } grab (exception e) { e.printstacktrace(); } }

java c

No comments:

Post a Comment