execvpたとえばexecvp(echo, b)、bがコマンドaの引数の配列である場合、この配列を後で変更すると、以前に行われたexecvp呼び出しに影響しますか?execp(echo、b)を呼び出そうとすると、b内のコンテンツではなく出力(null)になってしまいます。引数を正しく渡すためになぜそして何をしなければならないのか誰かが指摘できますか?
2 に答える
電話をかけた後、exec()またはその親戚の場合は1つ、元のプログラムはもう存在しません。つまり、そのプログラムはexec()実行されないため、呼び出し後に何も影響を与えることはありません。引数配列を正しく構築していないのではないでしょうか。以下の簡単な例を次に示しexecvp()ます。
#include <unistd.h>
int main(void)
{
char *execArgs[] = { "echo", "Hello, World!", NULL };
execvp("echo", execArgs);
return 0;
}
The
execv(),execvp(), andexecvpe()functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by aNULLpointer.
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" as argv[0], which presumably it depends on.
電話をかけた後、execあなたのプログラムは新しいものと交換されることを忘れないでください。それはもう実行されていないので、exec呼び出し後の同じプロセス内のコードは実際には到達できません。
b配列がNULLで終了していることを確認しますか?execが正しく機能するには、最後の要素がNULLである必要があります。また、最初のパラメーターも「echo」(argv [0])に設定することを忘れないでください。
試す
execlp("echo", "echo", "something", NULL);
ところで、execlpもう少し使いやすく、必要な数のパラメーターを渡すことができます。