プログラム内で定義された関数でexecvp()を使用できますか?
質問
プログラムの編成方法により、execvp()を使用して呼び出すC ++関数があります。
これは可能ですか?
解決
execvp()を含むすべてのexecバリアントは、ファイルシステムに表示される完全なプログラムのみを呼び出すことができます。幸いなことに、既にロードされているプログラムで関数を呼び出したい場合、必要なのはfork()だけです。この擬似コードのようになります:
int pid = fork(); if (pid == 0) { // Call your function here. This is a new process and any // changes you make will not be reflected back into the parent // variables. Be careful with files and shared resources like // database connections. _exit(0); } else if (pid == -1) { // An error happened and the fork() failed. This is a very rare // error, but you must handle it. } else { // Wait for the child to finish. You can use a signal handler // to catch it later if the child will take a long time. waitpid(pid, ...); }
所属していません StackOverflow