How to implement a process that differs from thread in java? - java

I know that threads and processes are related, and that threads are a small unit
of process. But I doubt that, how to implement a process using simple java code?
Please show me some code for doing this.
Thanks in advance.

You cannot implement a process using Java. Each Java program
runs within a single OS process; this OS process is the JVM.
If you want another process, start another (or the same) Java
program in another JVM instance.

Related

ptrace Java program using C/C++

I have problem similar to my previous one presented here.
This time I want use program written in c/c++ to track execution of JAVA program. So as I stated before same code which track stdout printing for c/c++ and register syscall 4 haven't done it for JAVA. I assume it's because execlp which I trace is used just to run jvm. And later on there are created more processes (by inner mechanism of jvm) which I do not track. I found this topic which seems to be partial solution. If I got it right every child will be traced. But that's is a problem as well I want to track only that process which handles my application and not all others that jvm might create. Is there any chance to get know which jvm thread/process handles my program and track only it?
For make it a bit easier let's assume my JAVA program is one-thread.
If you start the binary through your tracer app, all threads will be traced.
But if you attach to a process, then you won't attach to all it's threads. You have to attach to all of its threads using the threadids, that you can found listed eg. in /proc/%d/task/.
Also, I suggest reading through strace's source code, I've learnt a lot from it. If you can use strace to successfully follow java threads as you want, you can get the logic from it.

How to create a new Process in Java?

We have requirement of creating a new process from Java. I know we can create new sub-processes by using ProcessBuilder and Runtime.exec but our requirement is to create a complete new process. In other words, we do not want to have the spawned process exit when the JVM does.
Does anyone have ideas how we can do this using Java API or a 3rd party framework?
I've made a test java program which spawns another process. When java program exits, the spawned process is still alive. So what is the problem?
Anyway, in case of trouble you always can spawn a shell (.bat) file and spawn your process from it.
Do not think that can be done independently without taking help from OS.However, there is a similar thread here which talks about the same.
How do I launch a completely independent process from a Java program

Java: Get a process given a pid

Say I have a current running process known, how can I turn this into a Process object in Java?
The process is already running, so I don't want to spawn off another one, I just want to encapsulate it into a Process object that I can use within the java code.
Something along the lines of:
int pid = getPid();
Process proc = magicGetProcess(pid);
thanks
I don't think this is possible using only the builtin library. AFAIK, it is already non-trivial to get the running process' own PID (see the feature request and alternate mechanisms).
A quick look at the java.lang.Process class shows that you could go about writing your custom implementation of java.lang.Process using JNI and native code. Your custom class could then implement extra methods, such as the one in your question.
In *nix world grabbing exit code of a non-child process is not easy, because the exit code simply disappears together with the process as soon as the parent process has picked up the exit code of the child. You can attach to the running process using some tracing tool and pick up its exit code when the process dies. Most *nix OSes have command line tools which will let you do it (such as strace on Linux, truss on SunOS) in a non-intrusive way. However, you can only use them against your own processes or if you run as root. Another alternative is to configure audit subsystem of your OS to record exit codes of all processes.
You can't. Every operation in Process requires that the process is a child process. Not an arbitrary process.
what about using RMI? It it possible to pass a Process object to the Process which it is? Probably not, because Process is not Serializable

how to execute a java program on several cores?

I had a Java program that I run thousand times based on a loop (according to the number of files to be compiled to build a linux kernel) in a bash script.
There was a performance problem since the jvm was started several times...
What i've done then is implementing a wrapper in java that does the same as my bash script, reads one line from a file and then calls the main of my previous program... This way, I only have one jvm running...
The problem now is that only one core of my box is used which is another performance issue... Do I have to start some threads or can I use the same method but maybe calling the "former" main in a different way ?
If i have to start some threads, how I dispatch them throughout the multiple cores ?
thanks...
Your java program needs to become multi-threaded, in order to take advantage of many cores.
For example, create a thread pool using java.util.concurrent.Executors, encapsulate your data items as a Runnable, and submit the Runnable to the threadpool.
At the risk of oversimplifying it, just have your old class implement Runnable, taking what was in main() and putting it in Run(), then create a new thread with that class and start the thread.
In reality it might be more complicated than that if the threads need to share data, but based on what you said you are doing here, it doesn't seem like they would. So it might actually be just that easy.
You will need to make your program multi-threaded. You will have to do some learning to do this and I recommend you start with the Java Concurrency Tutorial.

Why/How does a application keep mutex references created by another process?

I have this somewhat unusual process structure:
Launch4J starts my Java application. It creates a mutex to provide single-instance functionality for the Java application.
The Java application starts a VB6 application, which can have multiple instances.
When the Java application terminates, the VB6 application is still running. (Desired behaviour)
The problem is: The mutex created by Launch4J is only released after the VB6 application terminates. Because of that it's impossible to start the Java application again.
Why would this happen? I'm not opening the mutex explictly...
I first suspected it is because of Java using CreateProcess with bInheritHandles == true, but the problem does not occour when I start notepad.exe for example.
EDIT: I still have this problem. Any pointers are appreciated!
Does Launch4J release the mutex and close its handle before terminating? I'm sorry, but I don't know how Java wraps the OS Mutex functions, but you should ensure you explicitly release the mutex and close its handle before your thread ends.
I faced the same kind of issue and realized that Launch4J creates an inheritable mutex on startup and when launching a process from withing the JVM, this mutex is then inherited by the new process.
After JVM shutdown the mutex is still held by the new process.
The simplest solution I found to avoid the mutex to be inherited is to use an intermediate program that launches a process as a detached process without inheriting the parent handles.
A working c++ example of this program can be found here https://stackoverflow.com/a/1582197/6894604
Just compile the program (“ex: rujob.exe”) using a c++ compiler and change your command to use the launcher instead of directly calling the process, for example :
new ProcessBuilder().command(
"runjob.exe",
"vbprogram.exe",
"/PARAM1",
"/PARAM2").start();
This way your VB program will not inherit the java application mutex and will not prevent your java app to start again.
Why don't you provide single instance functionality using VB instead of java? IMO it is wrong to provide single instance for the VB app using Launch4j. There are other ways, check this:
https://www.vbforums.com/showthread.php?342810-Classic-VB-How-can-I-allow-only-one-instance-of-my-application-to-run-at-a-time

Categories

Resources