trouble with fork() and exec()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boddah
    New Member
    • Dec 2008
    • 15

    trouble with fork() and exec()

    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)
    Code:
    -------------------------------------------------------------------------------
    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;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    What u are doing is not OK.
    See this book which has a example program

    Raghu

    Comment

    • boddah
      New Member
      • Dec 2008
      • 15

      #3
      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?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        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

        Comment

        • boddah
          New Member
          • Dec 2008
          • 15

          #5
          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....

          Comment

          • boddah
            New Member
            • Dec 2008
            • 15

            #6
            Here's my fork & exec code:

            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;
            	}
            }

            Comment

            Working...