Hello All,
I need to make parallel processing with Java. I know there are threads but i need to examine a distributed-like system. I will implement the MST algorithim and with a distributed syncronizer I will merge those sepereted nodes with rounds to find best and minimum spanning tree. This distributed syncronizer will work on different machines each of them connected with their neighbours with a wireless network.

For examining this system, my processes work like nodes in my wireless network area and those will make connections with each other.

For that purpose I should use processes. But I cannot find how to fork these processes, how to kill them, how to wait for them to return to parents.

Please, I need to make this project before next monday.
Thank you, anyway.

Dani AI

Generated

Short answer: Java does not provide a POSIX-style fork() like C. If your instructor requires real processes (separate heaps, separate JVMs) the normal Java approach is to make each node a standalone program (a runnable class or JAR) and launch those programs with ProcessBuilder / Runtime.exec. That gives true process isolation while letting you control start/stop and capture output.

Write each node as a small Java program that accepts node-id, port and neighbor list as args or via a config file. From your controller (or test harness) spawn nodes with ProcessBuilder, watch their streams, and wait for termination. Example pattern:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "node.jar", "--id", "3", "--port", "5003");
pb.redirectErrorStream(true);
Process p = pb.start();

// read output in another thread to avoid blocking
new Thread(() -> p.getInputStream().transferTo(System.out)).start();

// wait or kill
int exit = p.waitFor();          // blocks until exit
// or p.destroy(); p.destroyForcibly();

For inter-node communication use TCP sockets (ServerSocket/Socket) or a light RPC layer. For a distributed MST you typically send round-numbered messages with timeouts and ACKs so a lost message or slow node doesn't stall the whole round. Start by getting the protocol right on one machine (distinct ports for each process), then move to multiple machines: deploy the same JAR on each machine and start it (ssh or a small launcher). If you use Java 9+, ProcessHandle gives you PID info and onExit() for async termination handling.

Practical tips: always consume stdout/stderr (or redirect to files) to avoid blocking; give each node a unique port; use concise text/JSON messages for debugging; test with a handful of local JVMs before moving to wireless devices. As noted, threads are easier for pure simulation, but since you must use processes, package nodes as standalone programs and manage them with ProcessBuilder as shown.

Recommended Answers

All 2 Replies

So. . are you simulating this process by creating threads on your local machine? Or are you actually using multiple machines and communicating with some master-server that acts to combine the results and to spawn the processes on the other machines? My reply assumes you're simulating it, and only using your local machine. If you were using multiple machines you'd need to put your "spawned" threads software on multiple machines somehow and you'd have to set up your master thread so that it could communicate with all of the other threads (to get their results and to tell them what they need to compute). Anyway, again, this is assuming local machine setup:

http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html
You should take a look at that. To communicate you can share access to fields but that explains the issues with that. So lets say you have 5 threads that need to return their results, as an Object, to a 6th thread. The 5 threads could update an array with their results (each updating a different index) and then the 6th thread could use this array to combine the results. As far as letting the 6th thread know when it is time to combine the results, check this out:
http://java.sun.com/docs/books/tutorial/essential/concurrency/simple.html
It looks like the isAlive() method will tell you whether or not a thread is finished. P.S., in those links I pointed out to you, you'll notice that it says something about creating a lot of threads, and how instead, you can create WorkerThreads. You might want to take a look at that, although I'm not saying it's needed... just that you should investigate for yourself.

Thank you for your help bu, my instructor says that we need to do this project with processes, but I dont really know how to "fork" a child process in Java as we did in C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.