Hi all,
I'm writing a manager application (Linux/C++) that'll spawn 2 instances of a program (myApp). The myApp program needs to stay alive after being spawned (both instances) so a 3rd party program could connect to them thru socket. My problem is at the fork and exec function which I'm not really familiar with yet. Could anyone help to point me in the right direction or maybe enlighten me with the correct usage of fork and exec.
Thanks and regards.
Here's the main part of my manager application (that's supposed to spawn 2 instances of myApp) -
-------------------------------------------------------------------------------
-
int main(int argc, char **argv)
-
{
-
int buff;
-
char msg[MAXLINE];
-
int result;
-
int count = 0;
-
do{
-
printf("\n1) To bring up myApp: instance 1\n");
-
printf("2) To bring up myApp: instance 2\n");
-
printf("Input choice or 0 to exit:");
-
-
scanf("%d", &buff);
-
-
switch(buff){
-
case 1:
-
if(fork())
-
wait(0);
-
else
-
{
-
result = execlp("../experiment/myApp","myApp", "1", "8050", (char *)NULL);
-
perror("Result1:");
-
}
-
break;
-
case 2:
-
if(fork())
-
wait(0);
-
else
-
{
-
result = execlp("../experiment/myApp","myApp", "2", "8051", (char *)NULL);
-
perror("Result2:");
-
}
-
break;
-
case 0:
-
break;
-
}
-
}while(buff != 0);
-
-
return 0;
-
}
5 3372
What u are doing is not OK.
See this book which has a example program
Raghu
Thanks gpraghuram. I'll go thru the PDF file and update here again. BTW, do u have any sample of urs which I can refer to as well?
I dont have any examples now.
The chapter gives a good explanation about how the fork wrks and the example present there is also very helpful
Raghu
Problem solved. Actually the prob wasn't with my Parent code but it was the other program code which I was trying to spawn. Added a while(1){} to the code and it worked well. Previously the code had exited (thru return 0) thus, resulting the myApp to appear <defunct> when spawned.
Thanks Raghu and cheers....
Here's my fork & exec code: - , "Fork failed.\n");
-
log_messaint spawn(int b, int a)
-
{
-
int result;
-
char msg[MAXLINE];
-
pid_t mypid;
-
-
mypid = fork();
-
-
if(mypid < 0)
-
{
-
sprintf(msgge(RM_LOG_FILE, msg);
-
}
-
else if(mypid == 0)
-
{
-
printf("I'm the child\n");
-
result = execlp("./myApp", "myApp", itoa(b), itoa(a), NULL);
-
if(result == -1)
-
perror("Execlp failed:\n");
-
}
-
else
-
{
-
printf("I'm the parent: %d\n", getpid());
-
return mypid;
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Benoit Dejean |
last post by:
hello, i have a question about forking processes
atm, i have some code which i want to rewrite
os.system("cd ~ && exec " + cmd + " & disown")
i want to remove this os.system call
|
by: Lingyun Yang |
last post by:
Hi,
I want to use python as a "shell like" program,
and execute an external program in it( such as mv, cp, tar, gnuplot)
I tried:
os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot <...
|
by: blah |
last post by:
i m using Windows XP, and by tomorrow i will have have fedora core
installed too. the problem is, when i use these "fork() and exec()" my
windows doesnt do anything, python gives an error about the...
|
by: Alexander N. Spitzer |
last post by:
I am trying to write a program that will fork a process, and execute the
given task... the catch is that if it runs too long, I want to clean it up.
this seemed pretty straight forward with a...
|
by: thrillseekersforever |
last post by:
The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
A]
void...
|
by: rahulthathoo |
last post by:
Hi,
I have to call a perl program from within PHP, but since that program
takes a long time to finish, i fork a process in my php code and then
call the perl code, using exec. But I am not able...
|
by: Rafael Giannetti Viotti |
last post by:
Hi,
I am working with the subprocess.py module in Python 2.4.4 and I am
confused about it's functionality. It uses the standard pipe-fork-exec
method to start a subprocess:
# create pipes
...
|
by: Dan Upton |
last post by:
I have a Python script that does a fork/exec, so the parent process
can get the child's PID and monitor /proc/PID/stat (on a CentOS
system). Most of my processes' command lines are straightforward...
|
by: Jatek |
last post by:
Hi i have learned how fork.pl and exec.pl work as a starting point,but i want to create a program called shell.pl that will repeatedly read a line of user inpu until the user enters CTRL-D (end of...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |