при создании дочернего процесса с помощью fork(), вызове execvp и переназначении stdin/sdout с помощью pipe, read блокируется на чтении
Нужно запустить скрипт с помощью exec, передат ему значение в ввод и прочитать из его вывода, но при чтении операция блокируется, не могу опнять почему. Дело не в скрипте, он работает правильно(обычное эхо), если вызов exec отсутствует все тоже правильно работает. Не могу найти ошибку.
код
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vector>
#include <string.h>
int main()
{
pid_t pid;
int pipe_in[2];
int pipe_out[2];
pipe(pipe_in);
pipe(pipe_out);
pid = fork();
if (pid == 0){
close(pipe_in[1]);
close(pipe_out[0]);
dup2(pipe_in[0], 0);
dup2(pipe_out[1], 1);
char* arg[3];
arg[0] = "php";
arg[1] = "/home/anton/a.php";
arg[2] = NULL;
if (execvp(arg[0], arg) < 0)
_exit(0);
}
close(pipe_in[0]);
close(pipe_out[1]);
char buf[10];
write(pipe_in[1], "1111", 4);
read(pipe_out[0], buf, 10);
return 0;
}
скрипт
<?php
$line = trim(fgets(STDIN));
echo $line;
?>