fork() で複数の子プロセスを作成し、execl() で実行ファイルを実行させました。
失敗した execl() があるかどうかを確認したい (例: 存在しないファイルを実行しようとする)。によって、すべてのプログラムを execl() しようとし、そのうちの 1 つが失敗した場合は、start が任意のプログラムと通信する前に 1 を返します。
ここに参照コードがあります(すべての設定が正しいと仮定します)
#DEFINE NUMBEROFCHILD 4
char** exeFile = {"./p1", "./p2", "./p3", "nonexist"); //"nonexist" is a non-exist program
int main(int argc, char** argv){
pid_t childPID [numberOfChild];
for (int i = 0; i<numberOfChild; i++) {
//setting up pipes
pid_t childPID[i] = fork();
if(childPID[i] < 0) {
//close all pipes and quit
}else if (childPID[i] == 0) {
//redirect pipe
execl(exeFile[i],"args",(char*)0;
return 1;
//I'm expecting this to return when it try to execute "nonexist"
//but it didn't and keep running successed execl()
}else {
//close un-use pipes
}
}
while(true) {
for (int i = 0; i<numberOfChild; i++) {
//communicate with childs through pipe
}
}
for (int i = 0; i<numberOfChild; i++) {
//close reminded pipes
if (waitpid(childPID[i], NULL, 0) == -1){
return 1;
}
}
return 0;
}
このプログラムはまだメッセージを「存在しない」に送信しました (ただし、期待どおりに何も返されませんでした)。
とにかく私の目標を達成する方法はありますか?ありがとうございました!