forking a new process


 
Thread Tools Search this Thread
Top Forums Programming forking a new process
# 1  
Old 06-18-2006
forking a new process

Hi
I'm currently working with C on UNIX (HPUX) and need to be able to fork a seperate Java process from within a running C process.
I can run the following code from the command line via a script but am having difficulty getting it to work from within the code.
I am trying to use execl. Is execl the correct command to be using?
Any help would be appreciated.
Thanks.

$JAVAHOME/jre/bin/java -cp \

/opt/mv36/em/SMD/data/jct_005/SMD_LCT.jar:/opt/mv36/em/SMD/data/jct_005/jh.jar:/nms/JamToolsTest/jct/MV36CoreProxyNOP.1.1.jar
\
com.marcx.lct.sma11d.sma11d_lct_applet \
-mv36 1 -host 19.3.11.13 -port 20020 \
-inifile /opt/mv36/em/SMD/data/jct_005/ -lang 1 -laf 0 \
-msgset 5 -emaccess 3 -command 221
# 2  
Old 06-18-2006
Well, you can use execl, but in that case, you cannot use the $JAVAHOME env variable. You need to get the value from the env using something like getenv() and create the complete path.

The fork-exec part of the code can look like this (assuming that the path to the java command is /usr/jre/bin):
Code:
pid=fork();
if(pid==-1) {
   perror("error in fork!");
}
if(!pid) { /*child process - execute the java command*/
   execl("/usr/jre/bin/java","java","-cp","/opt/mv36/em/SMD/data/jct_005/SMD_LCT.jar:/opt/mv36/em/SMD/data/jct_005/jh.jar:/nms/JamToolsTest/jct/MV36CoreProxyNOP.1.1.jar","com.marcx.lct.sma11d.sma11d_lct_applet","-mv36","1","-host","19.3.11.13","-port","20020","-inifile","/opt/mv36/em/SMD/data/jct_005/","-lang","1","-laf","0","-msgset","5","-emaccess","3","-command","221",(char*)0)
}
else { /*in parent process - wait for child to exit*/
   wait();
}

# 3  
Old 06-19-2006
Cheers.
Though could you tell me whether the current environment should be passed to the process run by the execl?
The execl is succesfull but the underlying java program doesn't start which is I think because it doesn't receive the DISPLAY variable from the calling code. Is there a way to set this for the child process?

Thanks.
# 4  
Old 06-19-2006
Check the man page for fork(2).
Quote:
Originally Posted by man page of fork
The child process inherits the following attributes from the parent
process:

+ Real, effective, and saved user IDs.
+ Real, effective, and saved group IDs.
+ List of supplementary group IDs (see getgroups(2)).
+ Process group ID.
+ Environment.
+ File descriptors.
+ Close-on-exec flags (see exec(2)).
+ Signal handling settings (SIG_DFL, SIG_IGN, address).
+ Signal mask (see sigvector(2)).
+ Profiling on/off status (see profil(2)).
+ Command name in the accounting record (see acct(4)).
+ Nice value (see nice(2)).
+ All attached shared memory segments (see shmop(2)).
+ Current working directory
+ Root directory (see chroot(2)).
+ File mode creation mask (see umask(2)).
+ File size limit (see ulimit(2)).
+ Real-time priority (see rtprio(2)).
It says that the environment does get passed from parent to child. Are you sure that the DISPLAY variable is set in the parent process? Again use getenv to verify that the parent process does indeed have the DISPLAY variable set. If getenv returns NULL, DISPLAY is not set.

Cheers
# 5  
Old 06-20-2006
Thanks for all the help Blowtorch.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

need help in forking

I have an input file with contents like: 5785690|68690|898809 7960789|89709|789789 7669900|87865|659708 7869098|65769|347658 so on.. I need to pass this file to 10 parallely running processes (forking)so that each line is processed by a process and no line is processed twice and write the... (1 Reply)
Discussion started by: rkrish
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Programming

Parent forking

My question is, how do you fork only the parent processes in unix? For example how would I use the fork function to fork the parent process more than once and leave the children processes alone. This way I do not have children of children. The way I have it set up now it the parent process forks 3... (7 Replies)
Discussion started by: TWhitt24
7 Replies

4. Shell Programming and Scripting

Forking a bunch of processes and filling up the process table

I have a bash script that has been used for months here at work for doing an SSH into other machines both Linux and Solaris and running a script on the remote machine. Recently I have started to noticed that things are being left being on the maching doing the SSH. For example.... tivoli ... (1 Reply)
Discussion started by: LRoberts
1 Replies

5. UNIX for Advanced & Expert Users

Forking a new process without parent dependance

hi, I want my program to fork a new process and then I want to kill the parent process. The parent program before dying will issue a SIGTERM to all its childs. Which eventually kills all Children. I cant handle the SIGTERM at the child level.:( What I was thinking of was the Parent... (3 Replies)
Discussion started by: tyler_durden
3 Replies

6. Shell Programming and Scripting

Forking with Tclsh vs Wish

Hello, I am new to this site, so sorry ahead of time if this is not the right place for this question.......anywhooooo I am having troubles with forking new processes in wish. Take the following code example: **************************** package require Tclx puts "TCL VER: " proc... (3 Replies)
Discussion started by: pghamami
3 Replies

7. UNIX for Dummies Questions & Answers

Testing the forking process.

Hey, first time poster and a new UNIX user here. My question is regarding the forking process. I logged in to tty1, and typed the command ls -1 and hit enter. How can i tell that the ls -1 command ran in a subshell? Thanks. (0 Replies)
Discussion started by: Vitamin254
0 Replies

8. UNIX for Advanced & Expert Users

VERY confused about forking of child process

hi, I thought that when a child shell is forked, it will inherit all the variables of the parent now in my .cshrc I have setenv X x then I do at command line setenv X y and X is now y. So far so good! I then have a very simple script, y.csh #!/usr/bin/csh echo X (7 Replies)
Discussion started by: JamesByars
7 Replies

9. Programming

forking process.

#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pID; int i; for (i = 0; i < 3; i++) { pID = fork (); if (pID == 0) { printf ("Value of i --> %d... (2 Replies)
Discussion started by: kymthasneem
2 Replies

10. UNIX for Advanced & Expert Users

Forking

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation with the schedular functionality it will help a lot. Because I am stuck with this. #include <stdio.h> main(){... (3 Replies)
Discussion started by: manjuWicky
3 Replies
Login or Register to Ask a Question