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 |
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){ 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... |
problem with execl ()
I got the out put for the following code
Code:
switch(i){ got executed. My Program is working fine but I got problem with switch case Code:
m=strcasecmp(c.buff,"C"); |
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. |
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 |
Hi Thanks for answering.I found out where I went wrong.
|
All times are GMT -5. The time now is 01:49 AM. |