林相當新的C,我不確定如何繼續。從父母讀取和寫入多個孩子與執行程序
有了這段代碼,我試圖創建多個子進程,將他們的標準輸出發送到他們的父母stdin,並讓他們的標準輸入寫入數組中的指針位置的fdprintf。
代碼似乎當一個基本的程序,讀取標準輸入並打印到標準輸出(應管道回)執行到不行。 (在主代碼的不同部分,我將fprintf指向管道啓動的地方,然後讀取stdin等待應該寫回的內容)。
int plumber(int *pipes[], int numChildren, char* command[]) {
int i;
char id;
int nullSpace = open("/dev/null", O_WRONLY);
for(i = 0; i < numChildren; ++i) {
id = 'A' + i;
pipe(pipes[2 * i]);
pipe(pipes[2 * i + 1]);
switch(fork()) {
case (-1):
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
break;
case 0:
//child
//close child's write, dupe its stdin to read
//close childs old read
close(pipes[2 * i][1]);
if(dup2(pipes[2 * i][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i][0]);
//close child's read, dupe its stdout to write
//close childs old write
close(pipes[2 * i + 1][0]);
if(dup2(pipes[2 * i + 1][1], 1) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][1]);
close(1);
//child stderr to nullspace
if(dup2(nullSpace, 2) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(2);
execlp(command[i], "childprocess", numChildren, id, NULL);
break;
default:
//parent
//close read pipe from writing pipe
close(pipes[2 * i][0]);
//close write pipes and dupe stdin to read
//close parents old read
close(pipes[2 * i + 1][1]);
if(dup2(pipes[2 * i + 1][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][0]);
}
}
close(nullSpace);
return 0;
}
的指令才運行的子進程這也需要孩子的數目和從A至D的ID *管[]是numChildren的* 2乘2(沿着它的子1讀管那麼回事,child1寫管道,讀的child2,寫的child2等 請在此先感謝幫助和