ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.