LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how can i use execl to replce process (https://www.linuxquestions.org/questions/programming-9/how-can-i-use-execl-to-replce-process-483056/)

rameshtekumudi 09-13-2006 07:11 AM

how can i use execl to replace process
 
I want to use switch function to replace 3 process .I had
written code as shown below.

switch(i){

case 3: execl("./33","33",0); break;

case 2: execl("./22","22",0); break;

case 1: execl("./11","11",0);

}

When I tried to execute the above.only the first case written in the switch was successful.Can I use execl in this way.If it is not the case can anyone tell me how can I dothis.

Thanks in advance

soggycornflake 09-13-2006 10:22 AM

Unix/Linux does not have a "spawn process" function (Windows does apparently). In unix, this is split into fork and exec. fork creates a new process identical to the current one, and exec replaces the current process with a new one, which is why your code isn't working. After the first exec, the code doesn't exist anymore. You will need to do a fork/exec, such as

Code:

switch(i){
    case 3: if (!fork()) execl("./11", "11", 0); break;
    case 2: if (!fork()) execl("./22", "22", 0); break;
    case 1: if (!fork()) execl("./11", "11", 0); break;
}

fork returns the pid of the new process to the parent, and 0 to the child, which is how you distinguish which is which. In both cases, the program continues with the statement/instruction after the fork.

You'll also want to catch or ignore SIGCHLD or you will get a bunch of zombie processes, 'signal(SIGCHLD, SIG_IGN)' will suffice if you're not interested in the exit status.

If you want to do this a lot, you'll probably want to write a wrapper funtion.

See the man pages for full details.

edit:
"fork creates a new process identical to the current one,"

I should have said, except for the pid of course...

rameshtekumudi 09-14-2006 03:30 AM

problem with execl ()
 
I got the out put for the following code

Code:

switch(i){
    case 3: if (!fork()) execl("./11", "11", 0); break;
    case 2: if (!fork()) execl("./22", "22", 0); break;
    case 1: if (!fork()) execl("./11", "11", 0); break;
}

But when I tried to implement it in my program only case 3
got executed. My Program is working fine but I got problem with switch case



Code:

        m=strcasecmp(c.buff,"C");
       
        n=strcasecmp(c.buff,"CPP");
       
        o=strcasecmp(c.buff,"JAVA");
       
        if(m==0){i=1;printf("m=%d\ti=%d\n",m,i);}

        if(n==0){i=2;printf("n=%d\ti=%d\n",n,i);}

        if(o==0){i=3;printf("o=%d\ti=%d\n",o,i);}

        switch(i){

        case 1:  if (!fork())

        execl("./11", "11", 0); break;

        case 2:  if (!fork())

        execl("./22", "22", 0); break;

        case 3:  if (!fork())

        execl("./33", "33", 0);

        }

    }

Can u tell me where I went wrong.

Denes 09-14-2006 04:04 PM

Where is the break statement after each case?

Example:

case 1: if (!fork()) {
execl("./11", "11", 0); exit(0);
}
break;
default: exit(0);

When the fork returns 0 for the child process, the child process will just continue to the next case and execute the next one.

rameshtekumudi 09-14-2006 10:52 PM

Even if I place break in each switch cases.Only case 3 got executed.But
I want to execute the case based on the condition.How can I do this

rameshtekumudi 09-15-2006 04:09 AM

Hi Thanks for answering.I found out where I went wrong.


All times are GMT -5. The time now is 01:49 AM.