Reputation: 1807
So from what I found out on my own I know I'm going to need to use fork()
and execvp()
, but I can't quite figure out how to use execvp()
correctly from any examples online. Its supposed to let me essentially run command line stuff, but I don't understand the arguments it takes. Like if I wanted to run ls -a
how would I put that into execvp()
? And then there's the problem of exec()
doesn't return. How do I handle that?
Upvotes: 0
Views: 215
Reputation: 3006
First, exec()
doesn't return because it results in the process having it executing program replaced with whatever program was exec()
ed. That is if you run ls
via exec()
after a fork()
the PID of ls
will be the same as the one that called exec()
. That is exec()
doesn't create a new process so it can't return because all the memory and code from the program that called it is effectively gone and replaced with whatever it exec()
ed
Run ls -a
with execvp()
assuming ls
is in /bin
const char *file = "/bin/ls";
const char *arg1 = "ls";
const char *arg2 = "-a";
const char **argv = {arg1, arg2, NULL};
execvp(file, argv);
Upvotes: 3