When I call execvp
, for example execvp(echo, b)
where b is an array of arguments for the command a, will changing this array later affect the execvp call made previously? When I try calling execp(echo, b), it ends up printing out (null) instead of the content inside of b. Can anyone point out why and what I have to do to pass the arguments correctly?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Remember, that after
exec
call your program is exchanged by a new one. It's not executing anymore, so any code in the same process afterexec
call is, in fact, unreachable.Are you sure that b array is terminated with NULL? The last element must be NULL for exec to work correctly. Also, remember to set your first parameter to "echo" as well (it's argv[0]).
Try
Btw,
execlp
is a bit more comfortable to use, you can pass as many parameters as you wish.After you call
exec()
or one if its relatives, your original program doesn't exist anymore. That means nothing in that program can affect anything after theexec()
call, since it never runs. Maybe you're not building your arguments array correctly? Here's a quick working example ofexecvp()
:From the
execvp()
man page:A common mistake is to skip the part about "The first argument, by convention, should point to the filename associated with the file being executed." That's the part that makes sure
echo
gets "echo" asargv[0]
, which presumably it depends on.