For some reason execl only runs once, why is that ?Code:for (int i = 0 ; i <5;i++) execl ("/bin/ls", "ls")
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 );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
> 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 );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
spawnl is the POSIX equivalent.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law