For some reason execl only runs once, why is that ?Code:for (int i = 0 ; i <5;i++)
execl ("/bin/ls", "ls")
Printable View
For some reason execl only runs once, why is that ?Code:for (int i = 0 ; i <5;i++)
execl ("/bin/ls", "ls")
Because you're not creating a new process. You're trashing the current one to run the executable you're asking it to run.
Hint: fork().
Also, it should be
execl( "/bin/ls", "ls", (char*)NULL );
Got it, thanks.
Another question:
How can I calculate the time each process took to complete? I am o linux and the clock() function does not work.
I basicly have something like this:
Code:...
for (int i = 0 ; i <5;i++) {
pid = fork ();
if (pid == 0)
execl( "/bin/ls -R", "ls", (char*)NULL );
}
What do you mean "clock doesn't work". What do you get back from clock()?
It may not work if "ls" completes very quickly, because the time taken is shorter than one clock-tick, but otherwise I'm 100% sure that clock works.
--
Mats
> execl( "/bin/ls -R", "ls", (char*)NULL );
No no no - execl doesn't parse arguments.
Try
execl( "/bin/ls", "ls", "-R", (char*)NULL );
It's
execl( path, argv0, argv1, argvn, (char*)NULL );
For experience learn to google.
man execl.
Essentially it loads another binary into the current process and executes it.
A similar function exists in MS environment, but it automatically starts a new process, rather than loading the binary into the same process.
MSDN _execl
--
Mats
That's really kind of stupid to name it after the Unix function and then make it behave differently. Why didn't they name it forkexecl() or something instead?
spawnl is the POSIX equivalent.