나는 fork와 execvp를 통해 명령을 실행하는 다음과 같은 기능을 가지고있다. 포크에서 시작한 내 스크립트는 입력 데이터를 듣고 있습니다. 어떻게 데이터를 myscript로 보낼 수 있습니까?파이프 포크에 쓰는 방법은 무엇입니까?
int external_command()
{
int pfds[2];
if (pipe(pfds) < 0)
return -1;
if ((uproc.pid = fork()) == -1)
return -1;
if (uproc.pid == 0) {
/* child */
const char *argv[4];
int i = 0;
argv[i++] = "/bin/sh";
argv[i++] = "myscript.sh";
argv[i++] = NULL;
close(pfds[0]);
dup2(pfds[1], 1);
close(pfds[1]);
execvp(argv[0], (char **) argv);
exit(ESRCH);
} else if (uproc.pid < 0)
return -1;
/* parent */
close(pfds[1]);
int status;
while (wait(&status) != uproc.pid) {
DD("waiting for child to exit");
}
char buffer[64];
ssize_t rxed;
char *c;
int t;
//read from fork pipe
*value = NULL;
while ((rxed = read(pfds[0], buffer, sizeof(buffer))) > 0) {
if (*value)
t = asprintf(&c, "%s%.*s", *value, (int) rxed, buffer);
else
t = asprintf(&c, "%.*s", (int) rxed, buffer);
if (t == -1) return -1;
free(*value);
*value = strdup(c);
free(c);
}
// how to write to the pipe fork?
}
'fork'가 음수 * 2 회 *로 반환되는지 확인합니다. –
파일 설명자 duping이 나에게 의심스러워 보입니다. – trojanfoe
코드'} else if (uproc.pid <0)가 -1을 반환하면'else '는 전혀 필요치 않습니다. 당신은 이미 그 사건을 다루었습니다. –