tags:

views:

180

answers:

4

Is it possible to create a complete fork of a 'PROGRAM' in execution into two sub-programs from a single execution sequence ?

The sub-programs produced are completely identical. They have the same execution sequences and values but now they are two different programs. It is like creating a clone of an Object, thus giving us two different objects of the same type to work on. But instead of just an object and some values, here we want to create a completely parallel execution sequence of a Program already loaded in the JVM (would prefer an answer for Java).

+1  A: 

Well, using ProcessBuilder you can spawn another program.

See http://stackoverflow.com/questions/857236/java-equivalent-of-fork-in-java-task-of-ant/857247#857247

alamar
+1  A: 

Please read up on threads here and here.

Bombe
+4  A: 

You seem to be looking for the Java equivalent of the fork system call from Unix.

That does not exist in Java, and it's unclear whether it would even be possible, as processes in Unix don't have a direct equivalent in the JVM (threads are less independent than processes).

There is however a fork framework planned for Java 7:

http://www.ibm.com/developerworks/java/library/j-jtp11137.html

It's not the same as Unix'es fork/join, but it shares some ideas and might be useful.

Of course you can do concurrent programming in Java, it's just not done via fork(), but using Threads.

sleske
I think you should make it clearer that the fork and join framework really isn't the same as UNIX fork...!
Neil Coffey
Well, yes. It is however (as far as I understand) a related concept, and might help solve the same problems. Answer edited.
sleske
It isn't really a related concept. I mean, yeah, it's stuff running in parallel, but the idea of the fork/join framework is for running lots of small, fine-grained tasks concurrently. Fork on Unix is for starting up another process; it happens to work by copying the current process, but in most cases you immediately do a "if this is the child process, do something else entirely."
Adam Jaskiewicz
Thanks for the answer. Let's suppose I can somehow implement a fork call and now I have two processes which are exactly the same. Will they use the same resources? I was looking for a way which in fact duplicates all the resources as well which ensures that no fork in anyway influences the others' resources. And at my discretion I can completely cut off a fork causing no change in the system whatsoever for the remaining fork. Is this possible ?
Mohan Krishna
Well, this is a hypothetical question, so I don't see how one could give a meaningful answer. I see no reason why it could not work the way you describe it, but it would probably require significant changes to the JVM itself.Again: What are you trying to accomplish?
sleske
Yes, that was a hypothetical idea I had in mind. The accomplishment I get with such control over a program is that I can see the future of my program staying in the present. If it goes well I will could proceed, else from the present point I can see that the program is malfunctioning and could kill it from 'the present' itself. Hope this abstraction of my idea helps ...
Mohan Krishna
+3  A: 

I'm not sure exactly what you're trying to do here. It sounds to me like you have a solution in mind to a problem that would best be solved in another way. Would something like this accomplish your end goal?

public class MyApp implements Runnable
  {
  public MyApp(int foo, String bar)
    {
    // Set stuff up...
    }

  @Override
  public void run()
    {
    // Do stuff...
    }

  public static void main(String[] argv)
    {
    // Parse command line args...

    Thread thread0 = new Thread(new MyApp(foo, bar));
    Thread thread1 = new Thread(new MyApp(foo, bar));

    thread0.start();
    thread1.start();
    }
  }

Though I would probably put main() in another object in a real app, since life-cycle management is a separate concern.

Adam Jaskiewicz
Thanks for the reply. But I was having something else in mind. I just don't want to run a single method (like the MyApp(..)) but I just want a fork, i.e. complete forking of all the resources used at runtime.
Mohan Krishna
I know what you *want* to do. I'm wondering what *problem* you're trying to solve for which you *need* that particular solution. In almost any case I can think of, something along the lines of my solution is more appropriate. If you're just asking academically, that's OK, too, but it sounds like you have a very specific problem that you are trying to solve, and you're going about it all wrong.
Adam Jaskiewicz
This was just a abstract thought I got. I just wanted to analyse it's practical feasibility.
Mohan Krishna