Question
If I try to executes these commands:
int main(int argc, char* argv[])
{
execl("/bin/echo","echo","list ","of", "files:\n",NULL);
execl("/bin/ls","ls","-al",NULL);
return 0;
}
Only the first one gets executed, why?
Solution
execl REPLACES the current process - it's not a subroutine. You might try using system()
instead.
OTHER TIPS
Because as soon as you run exec*()
your application stops existing. If you want to run more than one external process then you'll need to fork()
.
You need to fork first.
Try running
int main(int argc, char* argv[])
{
if( vfork() == 0 )
execl("/bin/echo","echo","list ","of", "files:\n",NULL);
if( vfork() == 0 )
execl("/bin/ls","ls","-al",NULL);
return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow