By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

trouble with fork() and exec()

P: 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
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
What u are doing is not OK.
See this book which has a example program

Raghu
Mar 19 '09 #2

P: 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
Expert 100+
P: 1,275
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

P: 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

P: 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

Post your reply

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