0
私はCで条件を満たすとnodejsスクリプトを開始する必要があります。 私はsystem( "node /path_to_file/sample.js")を使用しています。 これはnodejsスクリプトを実行するための正しい方法ですか、それとも他の方法ですか?C関数からnode.jsスクリプトを呼び出す/実行する
私はCで条件を満たすとnodejsスクリプトを開始する必要があります。 私はsystem( "node /path_to_file/sample.js")を使用しています。 これはnodejsスクリプトを実行するための正しい方法ですか、それとも他の方法ですか?C関数からnode.jsスクリプトを呼び出す/実行する
execve
(man 2 execve)
とすべてのファミリexecvp
(man 3 execvp)
を使用すると、プログラムとスクリプトをCプログラムから実行できます。これらの呼び出しを使用すると、呼び出し後にプログラムが強制終了され、これを避けることができます。fork()
(man 2 fork)
これは動作する小さな例です(/ :
int main(int ac, char **av, char **env)
{
pid_t pid;
char *arg[3];
arg[0] = "/bin/ls";
arg[1] = "-l";
arg[2] = "/";
pid = fork(); //Here start the new process;
if (pid == 0)
{
//You are in the child;
if (execve(arg[0], arg, env) == -1)
exit(EXIT_FAILURE);
//You need to exit to kill your child process
exit(EXIT_SUCCESS);
}
else
{
//You are in your main process
//Do not forget to waitpid (man 2 waitpid) if you don't want to have any zombies)
}
}
fork()
は理解するのが難しいシステムコールかもしれませんが、それは
を学ぶために彼は本当に強力かつ重要な電話は、あなたがこれを読んで持っていますか? https://stackoverflow.com/questions/3736210/how-to-execute-a-shell-script-from-c-in-linux – CIsForCookies