LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 892|回复: 1

打开管道阻塞的问题

[复制链接]
发表于 2007-7-13 22:14:29 | 显示全部楼层 |阅读模式
已经用mkfifo定义一个文件“DataToGps” 管道
但是在以下的程序中,在打开DataToGps管道时阻塞,不知如何解决。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
FILE * Outfile;
char buf[20];
    printf("open file!\n");
    Outfile=fopen("DataToGps","w");
        if(Outfile==NULL){
                perror("Error in foprn!");
                exit(1);
        }
        printf("file opened!\n");
    pid_t fork1,fork2;
    fork1=fork();
        if(fork1<0){
                printf("fork1 creat erro!\n");
               
        }
        else{
                if(fork1!=0){
                        fork2=fork();
                if(fork2<0){
                        printf("fork2 creat erro!\n");
                }
                else{
                                if(fork2==0){
                                         printf("fork1 creat ok! ID:%d \n",getpid());
                             execl("./fork2","fork2",0);
                                 perror("exec error");
                                }
                                else{
                            printf("fork ok! ID:%d \n",getpid());
                                        fwrite("tEST DATA!",1,8,Outfile);
                                        //write(DataToGps[OUTPUT],"test data!\n",strlen("test data!"));
                    }
                }
                }
                else{
                        //close(DataToGps[OUTPUT]);
                        //read(DataToGps[INPUT],buf,20);
                    printf("fork1 creat ok! ID:%d  \n",getpid());
                        execl("./fork1","fork1",0);
                        perror("exec error");
                }
        }
        while(1){;}
}
发表于 2007-7-17 17:15:51 | 显示全部楼层
Post by Jary

int main(){
FILE * Outfile;
    Outfile=fopen("DataToGps","w");
        }



打开FIFO文件一般应该用open(const char *path, open_mode)函数,返回值为文件描述符,fd。

int pipe_fd;
pipe_fd = open(DataToGps, open_mode);/*open_mode can be combination of O_RDONLY, O_WRONLY and O_NONBLOCK*/
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表