2013-04-09 91 views
2

我试图实现一个程序,它在输入中采用一系列参数,并依赖于它创建了相同数量的由管道实现的进程,其中每个进程都在管道中写入,然后通过它给了父亲。进程与管道之间的通信

这是我的代码,它不会做我所需要的。

谢谢你的帮助。

#include <stdio.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <string.h> 



int main(int argc ,char *argv[]) 
{ 
int i,pid; 
int fd[2];//crea i descriptor 
char phrase[30][30];//crea il buffer 
pipe(fd); /* crea la pipe */ 

    // Genera i 10 processi 
    for(i=0;i<argc-1;i++) 
    { 
    if((pid=fork())==0) 
    {    
     strcpy(phrase[i], argv[i+1]); 
     printf("ho scritoo :'%s'\n",*phrase); 
     close(fd[0]);       /* chiude in lettura */ 
     write(fd[1],phrase,strlen(*phrase)+1); /* invia anche 0x00 */ 
     close (fd[1]);     // chiude in scrittura 
           // pid=0 -> figlio 
     usleep(50000*(1+i));  // Ritardo iniziale 
     printf("Figlio: %d\n",i+1); // Stampa messaggio del figlio 
     usleep(500000*(1+i));  // Ritardo finale 
     return(101+i);   // Termina con codice di ritorno 
     } 
    else { 
     printf("Ho generato il figlio %d con pid %d\n",i+1,pid); 
     char message[100]; //creare il buffer 
     memset(message,0,100); 
     int bytesread; 
     close(fd[1]);       /* chiude in scrittura */ 
     bytesread = read(fd[0],message,sizeof(message)); 
     printf("ho letto dalla pipe %d bytes: '%s' \n",bytesread,message); 
     close(fd[0]); 
    } 
    } 

    // Attende che dieci processi terminino 
    for(i=0;i<argc-1;i++) 
    { 
    int status; 
    wait(&status);  // Attende termine di un figlio (uno qualunque) 
    printf("Terminato processo %d\n",WEXITSTATUS(status)); 
    } 
} 

输入:./pipes CMD1 CMD2 CMD3

I created the first child with pid 21552 
I wrote: 'cmd1' 
I read from the pipe 5 bytes: 'cmd1' 
I created the second child with pid 21553 
I read from the pipe -1 bytes:'' 
I wrote: ') ML?' 
I created the third child with pid 21554 
I read from the pipe -1 bytes:'' 
I write: ') ML?' 
Son: 1 
Son: 2 
Son: 3 
Ended process 101 
Ended process 102 
Ended process 103 
+0

目前尚不清楚你想要做什么。你能解释更多吗? – Bechir 2013-04-09 15:07:01

+0

父进程必须在通信结束时收集并打印所有子进程输入。谢谢你:) – 2013-04-09 18:15:46

回答

2

你有两个问题:

首先,你是不是写正确phrase到管道。你一直在写phrase。对于第一个孩子来说,这对于其他人来说是可以的,它将是空字符串。

其次,创建第一个孩子后,您正在关闭fd[0]。你永远不会收到来自其他进程的数据。

#include <stdio.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <string.h> 

int main(int argc ,char *argv[]) 
{ 
    int i,pid; 
    int fd[2];//crea i descriptor 
    char phrase[30][30];//crea il buffer 
    pipe(fd); /* crea la pipe */ 

    for(i = 0; i < argc - 1 ; i++) 
    { 
     if((pid=fork())==0) 
     {    
      strcpy(phrase[i], argv[i+1]); 
      printf("ho scritoo :'%s'\n",phrase); 
      close(fd[0]);       /* chiude in lettura */ 
      write(fd[1],phrase[i],strlen(phrase[i])+1); /* invia anche 0x00 */ 
      close (fd[1]);     // chiude in scrittura 
      // pid=0 -> figlio 
      usleep(50000*(1+i));  // Ritardo iniziale 
      printf("Figlio: %d\n",i+1); // Stampa messaggio del figlio 
      usleep(500000*(1+i));  // Ritardo finale 
      return(101+i);   // Termina con codice di ritorno 
     } else { 
      printf("Ho generato il figlio %d con pid %d\n",i+1,pid); 
      char message[100]; //creare il buffer 
      memset(message,0,100); 
      int bytesread; 

      bytesread = read(fd[0],message,sizeof(message)); 
      printf("ho letto dalla pipe %d bytes: '%s' \n",bytesread,message); 
      // close(fd[0]); 
     } 
    } 
    close(fd[0]);       /* chiude in scrittura */ 
    close(fd[1]);       /* chiude in scrittura */ 
    // Attende che dieci processi terminino 
    for(i=0;i<argc-1;i++) 
    { 
     int status; 
     wait(&status);  // Attende termine di un figlio (uno qualunque) 
     printf("Terminato processo %d\n",WEXITSTATUS(status)); 
    } 
    return 0; 
} 
+0

我试图运行该程序,但它没有正确完成,它停留在最后的循环! – 2013-04-09 18:14:30

+0

我的意思是这个过程并没有像以前那样结束。 – 2013-04-09 18:23:16

+0

@AnisNouri固定。这是'for'陈述的停止条件。它应该停止在这种情况下'我 Bechir 2013-04-09 19:27:31