File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java Multi-Processing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Multi-Processing?" Watch "Java Multi-Processing?" New topic
Author

Java Multi-Processing?

Panku Panchal
Ranch Hand

Joined: Apr 24, 2003
Posts: 33
Hi,
This is urgent to know.
I know Java can handle Multiple-threads at the same time but does java handle multi-processing i.e. multiple processes going on at the same time?
I'm confused and feel that why not?
So can anybody of you can give me detailed explanation oh how java handles multi-processing?
Thank You,
Panku
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Does this have something to do with Developer Certification?
Each JVM takes up one process. You can start other processes with other JVM instances; these exist completely separate in memory, and generally are unrelated from each other. If you need some sort of communication between two different processes, you can achieve this by writing to / reading from a socket. Or they can modify files. Or use RMI. Special case - you can spawn a new process from java using the exec() command in the Runtime class. In this case you can also communicate with the new process using the various streams returned by getInput(), getOutput(), and getError() in the Process class.


"I'm not back." - Bill Harding, Twister
Panku Panchal
Ranch Hand

Joined: Apr 24, 2003
Posts: 33
Hi Jim,
First of all thank you for reply.
Actually , I and my friend (who is also working on SCJD) was discussing about multi-threading and milti-processing and she said you can not implement multi-processing in java so not in this assignment and my point was yes we can but I could not prove her as I had no strong base to prove it.

Each JVM takes up one process. You can start other processes with other JVM instances;

So can we install 2 JVMs on single hard disk? I'm confused. Please can you explain a little bit more.
If you need some sort of communication between two different processes, you can achieve this by writing to / reading from a socket. Or they can modify files. Or use RMI.

You mean considering other process as a remote Object?

Special case - you can spawn a new process from java using the exec() command in the Runtime class. In this case you can also communicate with the new process using the various streams returned by getInput(), getOutput(), and getError() in the Process class.


Can you provide me snippet.
I really appreciate your response, I'm searching on net so abdlly since yesterday and could get articles realted multiple processors but not multi-processing. I was literaaly frustrated.
Your answer is rejuvenating.

Panku
[ May 20, 2003: Message edited by: Panku Panchal ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Since this isn't really about SCDJ, I'll move it to Java in General - Intermediate and continue there...
Panku Panchal
Ranch Hand

Joined: Apr 24, 2003
Posts: 33
Hi Jim,
I was going through Java2 Complete Reference and it says "Proces based multitasking is not under the control of Java, however multithreaded multitasking is"
What does this mean? and then how about your solution regarding spawning process with exec() call.
Can you please elaborate on it more.
Thank You,
Panku
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
So can we install 2 JVMs on single hard disk? I'm confused. Please can you explain a little bit more.
Just like Jim said earlier, each JVM runs in its own process space. It has nothing to do with how many Java runtimes are installed on a hard disk. Just like you can have 2, 3 or however many cmd shells up in separate processes, you can have multiple JVMs running.
You mean considering other process as a remote Object?
In the case of RMI and sockets yes. Using a file for communincation really wouldn't be using a remote object, but dealing with its side effects.
Can you provide me snippet.

This just runs java -version and captures the the child process' error stream (which is where java -version prints) and copies it to stdout. Here's the results on my machine:


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I was going through Java2 Complete Reference and it says "Proces based multitasking is not under the control of Java, however multithreaded multitasking is"
That is not totally true as Jim mentioned earlier. The main problem with Process based multitasking is that you are at the mercy of the host OS whereas the JVM is in control of thread based multitasking. RMI and sockets to some extent overcome most of the problems with process based multitasking. Using Runtime.exec() and processing the I/O streams in separate threads is very similar to the old Unix fork() function.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sorry, had to run an errand before I could finish my reply.
[JY]: Each JVM takes up one process. You can start other processes with other JVM instances;
[PP]: So can we install 2 JVMs on single hard disk? I'm confused. Please can you explain a little bit more.

Maybe you're confusing JVM with JRE. A JRE is a set of files necessary to run Java; it's a subset of the JDK. Yes, you can install more than one JRE on your machine if you want, but this is only useful if they are different versions. There's no real need to have two copies of the same JRE. Now a JVM isn't a file or set of files - it's a program/process being run in memory on a machine, which used a JRE to get started, and continues to use a JRE whenever it needs to look something up (like dynamically loading a class definition from a library). You can have multiple JVMs using the same JRE, or different JREs. Every time you type "java MyClass" or whatever, you start a new JVM process which executes MyClass's main() method, until it exists. You can have many JVMs running simultaneously (if system memory allows it) - but in many cases, it will be more efficient to run multiple threads in a single JVM.
[JY]: If you need some sort of communication between two different processes, you can achieve this by writing to / reading from a socket. Or they can modify files. Or use RMI.
[PP]: You mean considering other process as a remote Object?
Ummm... the process itself isn't a remote object (I think), but objects in the other process can be remote objects seen through RMI. You can learn more about RMI from Sun's RMI tutorial, or our Distributed Java forum. I need to learn more about this myself (particularly since SCDJ may need it); apologies if my answer is in error here.
For a good discussion of how to use exec(), see this article.
[PP]: I was going through Java2 Complete Reference and it says "Proces based multitasking is not under the control of Java, however multithreaded multitasking is"
What does this mean? and then how about your solution regarding spawning process with exec() call.
Can you please elaborate on it more.

Well generally, a Java progam deals with only one process, the one that's running the JVM that your program is executing in. Most other processes have nothing to do with your JVM; if you want to interact with them in any way, you'd need to use the OS to do so. Using the exec() command is a spacial case (and an exception to the statment in the Complete Java Reference) - yes, you can interact with a process if you spawn it from Java using this technique. But the Process class is hard to use well; if you really want to work with a lot of different processes (as opposed to threads) you're probably better off working directly with the OS, e.g. through some shell script perhaps.
Hope that helps...
[ May 20, 2003: Message edited by: Jim Yingst ]
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Panku,
Regarding your posting "Java Multi-Processing?", true multi processing capabilities can be achieved if the machine is running on more than one processors (say the machines runs on dual processors). All the tasks are shared on these processors, which is beyond the scope of the programmer, and is in the hans of OS.
But one can span many threads in a program, but there is a limitation on number of threads by OS(I read some where previously, and please donot rely on this bracketed statement). On a single processor machine, two concurrent threads will never run simultaneously at the same point of time as the call stack used is same. One thread will be unloaded, and the other will be reloaded. Please don not get confused with multi-processor based and multi-thread based. They are two different entities.
One can run any number of JVM's in a machine. Running JVMs is not installing number of JDK or Java Runtime Environment.
If I use "java MyProgram" in one window, and other "java FriendProgram" in another command window, two JVMs are running. That means each java program runs on different JVM. If two programs running on different JVMs communicate each other, there should establish a network connection between the two programs (either socket based or RMI based). If you want to capture the output of one process from another, we use Runtime.exec() to launch the process, and capture the output of the process. For this please read java.lang.Runtime class API. In one of my previous projects, from java program, i launched c/c++/java programm compilation, and captured the compilation errors as output of the process.
For your information, assume you are running a java program.
The following things will happen at OS level.
1. Memory space is allocated for this program to run first, and it is divided into 4 areas. First is the program(exe) portion, second is the data portion where all static data is stored, third is the call stack portion, and the fourth is the heap portion.
first portion is loaded with JVM, which inturn loads your java class file.
second is the static global data(static final constants)
third is the place, methods/functions will get called, and run. All the java objects created during method are stored in the heap only(in case of java), which is the 4th(heap) area.
If the stack size increases, heap size decreases. If most of the memory is eaten by stack, it starts growing in heap also. This is called memory corruption/buffer over flow, which never happens in case of java. if you manage to overflow the buffer/corrupt the memory, you can hack the programs.
The concept of refractorying is basically reduce the size of methods/functionality is to minimize the usage of stack size, which is a better and safe design.
Hope I bored you a lot.
Let me halt at this stage.
Regards,
Ganapathy.
[ May 20, 2003: Message edited by: S. Ganapathy ]
[ May 20, 2003: Message edited by: S. Ganapathy ]
Panku Panchal
Ranch Hand

Joined: Apr 24, 2003
Posts: 33
Hi,
Thank you guys. I liked the explanation.
Moris, Jim and Ganapathy thank you for the insight!!
Moris, I'm trying to run the example you gave me, but it is giving me error like

I tried few things, didn't work.
I'll work on that, I just run it once.
I'll post more questions if I found confusing.
Panku
Panku
Panku Panchal
Ranch Hand

Joined: Apr 24, 2003
Posts: 33
OK Moris,
I could compile the program. I'm directly printing err.readLine().
Panku
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Multi-Processing?