0
我有一個程序,我想從一個子進程中排序文件中的第一列,並將輸出返回到父進程。我如何從execlp中獲取響應並打印它?以下是我迄今爲止:從execlp獲取回報()
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define WRITE 1
#define READ 0
int main(int argc, char **argv)
{
int i, k;
int p1[2], p2[2];
int p1[2], p2[2];
pid_t childID;
if (pipe(p1) < 0 || pipe(p2) < 0) {
perror("pipe");
exit(0);
}
childID = fork();
if (childID < 0) {
perror("fork");
exit(0);
}
else if (childID == 0){
close(p1[WRITE]);
close(p2[READ]);
dup2(p1[READ], STDIN_FILENO);
close(p1[READ]);
dup2(p2[WRITE], STDOUT_FILENO);
close(p2[WRITE]);
execlp("sort", "-k1", "-n", "temp.txt", (char *)NULL);
perror("exec");
exit(0);
}
else {
//parent process
//Not sure how to get response from exec
}
}
感謝您的回覆。我如何才能寫出兒童流程的結果? – kirax
@kirax請嘗試我已更新的答案中的代碼。 –
謝謝!我想是什麼? – kirax