유닉스/리눅스 셸의 파이프 함수를 구현하는 간단한 코드를 작성했습니다.pipe() 함수가있는 간단한 셸
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void
cisshPipe(char* command1[], char* command2[])
{
int fd[2];//create array for two file descritors
pid_t childPid;//To set for child process
pipe(fd);//To create pipeline here
if((childPid=fork())==-1)
{
perror("fork here");
exit(1);
}
//The below is the real meat for this subroutine
if(childPid==0)//If child process
{
close(fd[0]);//To close the input of child
dup(fd[0]);//To duplicate the input, for the later process
}
else//For the real output
{
close(fd[1]);//To close the parent output first
execvp(command2[],command2);
}
}
그러나 여기서 "execvp (command2 [], command2)"에 대한 컴파일 오류가 있습니다. 부모 출력에 자식 출력을 전달하는 데 사용하는 dup() 함수 때문인 것 같습니다. 그것을 고칠 모든 제안을하시기 바랍니다?
일부 업데이트 :
John의 답변에 감사드립니다. 나는 컴파일 문제를 해결했다. 하지만 그것은 "ls | sort"를 입력 할 때 파이프 함수를 수행 중이다. 나는 여전히 dup() 문제가 여기에 있다고 생각한다.
정확한 메시지를 게시하시기 바랍니다. 오류는 무엇이 잘못되었는지 알려줍니다 ... – clcto
답장을 보내 주셔서 감사합니다. 지금 고쳐. –
전화를 건 기능에 대한 문서를 읽었습니까? [execvp (3)] (http://man7.org/linux/man-pages/man3/execvp.3.html)? 문서화 된대로 호출해야합니다 ... –