중첩 파이프
글쓴이: 베리스타 / 작성시간: 월, 2005/10/03 - 5:42오후
아래 구현은 cmd가 ls -a | grep a | grep b 이거를 구현하고자 하는건데 책보고 파이프 하나까진 성공해서 2개로 늘릴려고 하는데 구현이 잘 안됩니다.
out에는 ls -a
in에는 grep a
last에는 grep b가 들어가서 출력이 되게 하고 싶습니다.
int mypipe(char *cmd)
{
int pfd[2];
int i=1, j,cnt=0;
char *in[40], *out[40], *last[40];
char *dummy[40];
bzero(out, sizeof(out));
bzero(in, sizeof(in));
bzero(last, sizeof(last));
bzero(dummy, sizeof(dummy));
dummy[0] = strtok(cmd, "|");
/* if(dummy[0][strlen(dummy[0])-1]==' ')
dummy[0][strlen(dummy[0])-1]='\0';
*/ while((dummy[++cnt]=strtok(NULL, "|")) != NULL);
printf("cnt=%d\n", cnt);
for(j=0;j<cnt;j++)
printf("dummy[%d]=%s\n",j, dummy[j]);
// dummy[1] = strtok(NULL, "|");
out[0]=strtok(dummy[0], "\t ");
while ((out[i++]=strtok(NULL, "\t ")) != NULL);
i=1;
in[0]=strtok(dummy[1], "\t ");
while ((in[i++]=strtok(NULL, "\t ")) != NULL);
i=1;
last[0]=strtok(dummy[2], "\t ");
while ((last[i++]=strtok(NULL, "\t ")) != NULL);
pipe(pfd);
if (! fork())
{ /* first child executing */
close(1); /* Close standard output */
dup(pfd[1]);
close(pfd[0]);
close(pfd[1]);
execvp(out[0], out);
}
if (! fork())
{ /* second child executing */
close(0); /* Close standard input */
dup(pfd[0]);
close(pfd[0]);
close(pfd[1]);
execvp(in[0], in);
}
if (! fork())
{ /* first child executing */
close(1); /* Close standard output */
dup(pfd[1]);
close(pfd[0]);
close(pfd[1]);
execvp(last[0], last);
}
close(pfd[0]);
close(pfd[1]);
while (wait(NULL) != -1);
return 0;
}
파이프 하나를 책보고 해결하고, 무한 파이프를 돌리기 전에 파이프 두개를 성공 시키고 싶은데 어떻게 고쳐야 하나요..ㅠ.,ㅜ
고수님들 도와주세요^^;;
Forums:


댓글 달기