子进程不读取PIPE

时间:2013-12-12 21:33:13

标签: c process printf pipe fifo

#include    <sys/types.h>
#include    <sys/stat.h>
#include    <sys/mman.h>    
#include    <fcntl.h>
#include    <stdio.h>    
#include    <stdlib.h>  
#include    <string.h>  
#include    <unistd.h>
#include    <limits.h>  
#include    <pthread.h>
#include    <signal.h>
#include    <sys/select.h>
#include    <errno.h>

typedef struct rot* Rotacao; 
typedef struct rot{
   int idProcess;
   int rotacao;
   char url[50];
} roda;

struct stat statbuf;

void listen();
void * do_rotation();
void write_pixel();
void rotacao90();
void rotacao180();
void rotacao270();
void sig_handler();
struct pixel *get_pixel();
void trata();

char *src, buf[50];
int size, fileids90[2], fileids180[2], fileids270[2], idp;


int main(){
    int id[N], i = 0;

    int p, idPai = getpid();

    for(p = 0; p<3; p++){
        if(getpid() == idPai){
            if(fork() == 0){
                if(p == 0) rotacao90();
                else if(p == 1) rotacao180();
                else if(p == 2) rotacao270();
            }
        }
    }

    if(pipe(fileids90) != 0) printf("Não criou pipe 90!");
    if(pipe(fileids180) != 0) printf("Não criou pipe 180!");
    if(pipe(fileids270) != 0) printf("Não criou pipe 270!");

    if(getpid() == idPai){
        while(1){
        listen();
    }
}


void rotacao90(){
    printf("1 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());
    char str[20];
    while(1){
        read(fileids90[0], str, strlen(str)+1);
        printf("%s", str);

    }
}

void rotacao180(){
    printf("2 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());   
}

void rotacao270(){
    printf("3 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());
}

void listen(){
int pipe, rot, fdmax, n;
    char str[20];


    Rotacao new = (Rotacao) malloc(sizeof(roda));
    Rotacao final = (Rotacao) malloc(sizeof(roda));

    //OPEN PIPE WITH READ ONLY
    if ((pipe = open ("FIFO_PIPE", O_RDONLY))<0){
        perror("Could not open named pipe for reading.");
        exit(-1);
    }


    while(1){
        fd_set master;
        FD_ZERO(&master);

        FD_SET(pipe, &master);
        if(pipe > fdmax) fdmax = pipe;
        FD_SET(fileids90[0], &master);
        if(fileids90[0] > fdmax) fdmax = fileids90[0];
        FD_SET(fileids180[0], &master);
        if(fileids180[0] > fdmax) fdmax = fileids180[0];
        FD_SET(fileids270[0], &master);
        if(fileids270[0] > fdmax) fdmax = fileids270[0];

        n = select(fdmax+1, &master, NULL, NULL, NULL);
        if (n == -1) {
            perror("select");
        }

        if (FD_ISSET(pipe, &master)){


            //READ FROM PIPE
            if (read(pipe,new,sizeof(roda)) < 0 ){
                perror("Error reading pipe.");
                exit(-1);
            }

            final->idProcess = new->idProcess;
            final->rotacao = new->rotacao;
            strcpy(final->url, new->url);

            if(final->rotacao == 90){
                printf("\ndsadasdas1");
                strcpy(str, "ola");
                write(fileids90[1], str, strlen(str)+1);
            }
            else if(final->rotacao == 180){
                printf("\ndsadasdas2");
            }
            else{
                printf("\ndsadasdas3");
            }

            trata(final);



            //CLOSING PIPE
            if (close(pipe)<0){
                perror("Error closing FIFO.");
                exit(-1);
            }

            //OPEN PIPE WITH READ ONLY
            if ((pipe = open ("FIFO_PIPE", O_RDONLY))<0){
                perror("Could not open named pipe for reading.");
                exit(-1);
            }


        } 
        if (FD_ISSET(fileids90[0], &master) == 1) printf("\nPipe90 com conteudo!");
        if (FD_ISSET(fileids180[0], &master) == 1) printf("\nPipe180 com conteudo!");
        if (FD_ISSET(fileids270[0], &master) == 1) printf("\nPipe270 com conteudo!");

    }

    //CLOSING PIPE
    if (close(pipe)<0){
        perror("Error closing FIFO.");
        exit(-1);
    }
}

大家好。我有一个我不明白的问题。我有一个客户端和一个服务器。这是服务器代码。我从客户端收到一个结构,我想将该结构发送给在函数rotacao90()中运行的进程子进程。同时我正在做一些测试,我正在通过管道向父进程发送消息。 Select功能没有以正确的方式运行,我不明白为什么。换句话说,我已经尝试从父进程向子进程发送消息,但他没有阅读它。我需要从服务器到子节点以及从子节点到服务器进行通信。

有人可以解释我或帮助我吗?这是我大学的一个项目。

谢谢。

1 个答案:

答案 0 :(得分:1)

在子进程生成之后(使用pipe(fileids90)等)创建管道,因此子进程将不具有父进程的文件描述符。