LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2006, 07:11 AM   #1
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Rep: Reputation: 0
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

Last edited by rameshtekumudi; 09-13-2006 at 10:38 PM.
 
Old 09-13-2006, 10:22 AM   #2
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
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...

Last edited by soggycornflake; 09-13-2006 at 12:41 PM.
 
Old 09-14-2006, 03:30 AM   #3
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0
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.

Last edited by rameshtekumudi; 09-15-2006 at 04:10 AM.
 
Old 09-14-2006, 04:04 PM   #4
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
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.
 
Old 09-14-2006, 10:52 PM   #5
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 09-15-2006, 04:09 AM   #6
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0
Hi Thanks for answering.I found out where I went wrong.

Last edited by rameshtekumudi; 09-15-2006 at 04:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the execl() output? estratos Programming 4 06-07-2006 05:18 PM
system() and execl anyone got any sample code please? twirl Programming 2 09-08-2005 02:01 AM
How to use execl, man is not helping grupoapunte Programming 2 05-29-2005 07:27 PM
execl: couldn't exec `/bin/sh' Daredevil Linux - Newbie 2 04-12-2004 11:06 AM
execl on a script file iasion Linux - General 1 03-25-2004 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration