2013-12-12 81 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通信。 –

+1

你的專業錯誤是你需要在fork之前創建管道來與孩子進行通信。父母/孩子不共享內存,但孩子們繼承文件描述符。退出你的孩子,這樣他們就不會執行父代碼(或者像90年那樣永遠運行)。處理完成後讓父母等待孩子。 – Duck

回答

1

在子進程已生成後(如pipe(fileids90)等)創建管道,因此子進程將不具有父進程所執行的文件描述符。

+1

不,叉子只會創建3個孩子。如果有點非正統的話,那很好。 – Duck