2013-12-12 4 views
0
#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() 함수로 동작하는 프로세스 자식에게 보내고 싶습니다. 한편 나는 몇 가지 테스트를하고 파이프를 통해 부모 프로세스에 메시지를 보냅니다. 선택 기능이 제대로 작동하지 않아 그 이유를 모르겠습니다. 다른 방법으로 나는 부모 프로세스에서 자식에게 메시지를 보내려고 시도하지만 읽지는 않습니다. 나는 서버에서 자식으로 그리고 어린이에서 서버로의 커뮤니케이션을해야한다.하위 프로세스가 PIPE를 읽지 않습니다

누군가 나를 설명하거나 도와 줄 수 있습니까? 이것은 내 대학의 프로젝트를위한 것입니다.

감사합니다.

+1

코드를 단순화하여 작동하는 간단한 버전을 구할 수 있는지 확인하십시오. 예를 들어 4 개 또는 5 개가 아닌 2 개의 프로세스가 단순한 메시지와 통신하는 경우가 있습니다. –

+1

전공 오류는'fork '이전에 자식 *과 통신하기 위해 파이프를 생성해야한다는 것입니다. 부모/자식은 메모리를 공유하지 않지만 자식은 파일 설명자를 상속합니다. 부모 코드를 실행하기 위해 떨어지지 않도록 자녀를 종료하십시오 (또는 90의 경우처럼 영원히 실행하십시오). 처리가 끝나면 부모에게 자녀를 기다린다. – Duck

답변

1

자식 프로세스가 이미 생성 된 후에 (pipe(fileids90) 등으로) 파이프를 생성하므로 자식 프로세스에 부모가 수행하는 파일 설명자가 없게됩니다.

+1

아니요, 포크는 3 명의 자식 만 만듭니다. 그다지 정통적이지는 않다. – Duck

관련 문제