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 3384 gpraghuram 1,275
Recognized Expert Top Contributor
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?
gpraghuram 1,275
Recognized Expert Top Contributor
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 < plot.tmp"'))
since it's in a for-loop, it should be executed many times, but
It exits after the first time running.
|
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 module,
the kind of error when u know u r wrong.
is it because these commands work on linux?
if so, is it better for me to stick with fedora for my python programs
or use windows?
--
|
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 single executable being run
from the fork. The problem I am having now is that if I call a shell
scripts, then lets say calls "xterm &", after the timeout has occurred,
I kill the shell script, but the xterm is still running... I cannot seem
to kill...
|
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 main() {
....
pid1 = fork();
if (pid1) pid2 = fork();
.....
| |
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 to run the perl
program, but I think i m not able to fork in the first place, in the
sense that I (apache) may not have permissions to fork. Is there any
way to remedy this?
Rahul
|
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
pid = fork()
|
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
enough to do this with, but I have a handful that use < on the command
line, eg
../gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst
The only thing I could really think of to try was
|
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 file). For each line entered, the program should fork a process that will execute the provided command using exec. i want the parent process to use wait to await the completion of the child process. I want the program to handle invalid commands by...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |