无法从管道中读取数据
本人有个linuxC语言的程序,想设计成核心程序独立,不需图形界面,然后在基于核心程序开发图形界面。故打算使用linux的管道,将文件描述符给重定向来进行核心程序的输出与输入的交互,但是现在遇到的问题却是无法读取数据的问题,代码如下,望高手回答。部分代码如下:
static int pipe_stdout[2],pipe_stdin[2],pipe_stderr[2];
int message_box(char *message,char *title,GtkMessageType message_type,GtkButtonsType button_type);
gboolean init_bridge()
{
int pid=0;
if(pipe(pipe_stdout)||pipe(pipe_stdin)||pipe(pipe_stderr))
{
if(message_box("Interface initialisation error","error",GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE)==GTK_RESPONSE_CLOSE)
return FALSE;
}
if(dup2(pipe_stdout[1],STDOUT_FILENO)==-1||dup2(pipe_stdin[0],STDIN_FILENO)==-1||dup2(pipe_stderr[1],STDERR_FILENO)==-1)
{
if(message_box("Interface initialisation error","error",GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE)==GTK_RESPONSE_CLOSE)
return FALSE;
}
pid=fork();
if(pid<0)
{
if(message_box("Interface initialisation error","error",GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE)==GTK_RESPONSE_CLOSE)
return FALSE;
}else if(pid==0)
{//child process
close(pipe_stdout[0]);
close(pipe_stdin[1]);
close(pipe_stderr[0]);
execl("Authentication",NULL);
exit(0);
}
else//parent process
{
char buf[1024];
close(pipe_stdout[1]);
close(pipe_stdin[0]);
close(pipe_stderr[1]);
read(pipe_stdout[0],buf,1024); //这里会阻塞,而读不到数据
}
return TRUE;
}