5 messages in net.java.dev.jna.usersRe: [jna-users] Help required with ...
FromSent OnAttachments
rzoAug 16, 2008 7:40 am 
Daniel KaufmannAug 16, 2008 10:42 am 
rzoAug 19, 2008 10:02 am 
Timothy WallAug 19, 2008 10:43 am.java
Daniel KaufmannAug 19, 2008 5:55 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Help required with fork/execActions...
From:Daniel Kaufmann (dani@gmail.com)
Date:Aug 16, 2008 10:42:21 am
List:net.java.dev.jna.users

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> To: <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"); }