473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

trouble with fork() and exec()

15
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)
Expand|Select|Wrap|Line Numbers
  1. -------------------------------------------------------------------------------
  2. int main(int argc, char **argv)
  3. {
  4.     int  buff;
  5.     char msg[MAXLINE];
  6.     int result;
  7.     int count = 0;
  8.     do{
  9.         printf("\n1) To bring up myApp: instance 1\n");        
  10.         printf("2) To bring up myApp: instance 2\n");        
  11.         printf("Input choice or 0 to exit:");        
  12.  
  13.         scanf("%d", &buff);
  14.  
  15.         switch(buff){
  16.         case 1:
  17.             if(fork())
  18.                 wait(0);
  19.             else
  20.             {            
  21.                 result = execlp("../experiment/myApp","myApp", "1", "8050", (char *)NULL);
  22.                 perror("Result1:");
  23.             }
  24.             break;
  25.         case 2:
  26.             if(fork())
  27.                 wait(0);
  28.             else
  29.             {
  30.                 result = execlp("../experiment/myApp","myApp", "2", "8051", (char *)NULL);
  31.                 perror("Result2:");
  32.             }
  33.             break;
  34.         case 0:
  35.             break;
  36.         }
  37.     }while(buff != 0);
  38.  
  39.     return 0;
  40. }
Mar 18 '09 #1
5 3372
gpraghuram
1,275 Expert 1GB
What u are doing is not OK.
See this book which has a example program

Raghu
Mar 19 '09 #2
boddah
15
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?
Mar 19 '09 #3
gpraghuram
1,275 Expert 1GB
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
Mar 19 '09 #4
boddah
15
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....
Mar 19 '09 #5
boddah
15
Here's my fork & exec code:

Expand|Select|Wrap|Line Numbers
  1. , "Fork failed.\n");
  2.         log_messaint spawn(int b, int a)
  3. {
  4.     int result;
  5.     char msg[MAXLINE];
  6.     pid_t mypid;
  7.  
  8.     mypid = fork();
  9.  
  10.     if(mypid < 0)
  11.     {
  12.         sprintf(msgge(RM_LOG_FILE, msg);
  13.     }
  14.     else if(mypid == 0)
  15.     {
  16.         printf("I'm the child\n");
  17.         result = execlp("./myApp", "myApp", itoa(b), itoa(a), NULL);
  18.         if(result == -1)
  19.             perror("Execlp failed:\n");
  20.     }
  21.     else
  22.     {
  23.         printf("I'm the parent: %d\n", getpid());
  24.         return mypid;
  25.     }
  26. }
Mar 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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
3
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 <...
2
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...
1
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...
3
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...
0
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...
2
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 ...
3
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...
5
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...
0
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.