

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
5 messages in net.java.dev.jna.usersRe: Re: [jna-users] Help required wit...| From | Sent On | Attachments |
|---|---|---|
| rzo | Aug 16, 2008 7:40 am | |
| Daniel Kaufmann | Aug 16, 2008 10:42 am | |
| rzo | Aug 19, 2008 10:02 am | |
| Timothy Wall | Aug 19, 2008 10:43 am | .java |
| Daniel Kaufmann | Aug 19, 2008 5:55 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: Re: [jna-users] Help required with fork/exec | Actions... |
|---|---|---|
| From: | rzo (rz...@gmx.de) | |
| Date: | Aug 19, 2008 10:02:12 am | |
| List: | net.java.dev.jna.users | |
Daniel,
thanks for the reply.
Concerning " the JVM does not expect this to happen": Why would the JVM not expect this to happen ? Take a look at the implementation of Runtime.exec(). It is implemented using JNI call to fork/execv.
Given that the java Process class does work as I expect it, I have reimplemented it using JNA. With JNA I hope that I can keep my c-compiler and linker in the cupboard. For windows JNA does a perfect job.
I am now trying to implement the same for Linux/Unix.
As stated, the fork works fine. A second process is spawned. However, execv does not seem to work. There is a thread on this mail list with a similar issue. However it seems that the issue was not solved.
Has anyone been able to implement fork/exec using JNA ? If yes, please, please post sample code. If no: is this a JNA issue ? or am I missing something ? do I first have to pipe the streams for execv to function ?
- Ron (Yet Another Java Service Wrapper) http://sourceforge.net/projects/yajsw/
Daniel Kaufmann wrote:
I think calling fork in a java program is kind of unsafe and non-portable between JVM since the JVM does not expect this to happen. The new process will be sharing some OS resources with the original process like semaphores, shared memory, etc, so this might cause unexpected results.
Anyway, in almost any case you won't even need to use fork, because you can use threads or Process class (created using Runtime.exec methods or ProcessBuilder class) for most of the same purpose you use fork which will be more portable and better documented.
Only thing that is kind of difficult to get when using Process is the pid of the process you just launched in case you really needed, but in most cases you can get around that limitation, by using Process class methods or for example by launching the process using a small launcher shell script or C program instead of directly the process. Below an example using ksh shell script (it might work with others shells): shift exec $*
And since the process id doesn't change after an exec, you can get the it using variable $$ before the exec, so you can save it to a file (echo $$>pid.txt), write it to standard output so that java program can read it, or use it in other ways in the script.
Thanks, Daniel
----- Original Message ----- From: "rzo" < rz...@gmx.de <mailto:rz...@gmx.de> > To: < use...@jna.dev.java.net <mailto:use...@jna.dev.java.net> > Sent: Saturday, August 16, 2008 11:47 AM Subject: [jna-users] Help required with fork/exec
Hello,
I am trying to spawn a process using the following code. A new process is forked but hangs in execvp. A look at the process table shows that a java process is spawned but no konsole process. Is this an JNA issue ?
- Ron
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((System.getProperty("os.name").startsWith("Windows") ? "msvcrt" : "c"), CLibrary.class);
int atol(String s); int fork(); void exit(int status); /* * int execv (const char *filename, char *const argv[]) */ int execvp (String filename, String[] argv); } public static void main(String[] args) { System.out.println(System.getProperty("os.name")); int pid = 0; if ((pid = CLibrary.INSTANCE.fork()) == 0) { CLibrary.INSTANCE.execvp("konsole", new String[]{"konsole", "--noclose"}); CLibrary.INSTANCE.exit(-1); } else if (pid > 0) { System.out.println("forked "+pid); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } else if (pid < 0) System.out.println("could not fork a process"); }








.java