【问题标题】:Communication between processes with pipes进程与管道之间的通信
【发布时间】:2013-04-01 02:13:57
【问题描述】:

我正在尝试实现一个程序,该程序在输入中采用一系列参数,并取决于它创建由管道实现的相同数量的进程,每个进程在管道中写入,然后将其传递给父亲。

这是我的代码,它不能满足我的需要。

谢谢你的帮助。

#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

【问题讨论】:

  • 不清楚你想做什么。你能解释更多吗?
  • 父进程必须在通信结束时收集并打印所有子进程的输入。谢谢你:)

标签: c operating-system fork pipe inter-process-communicat


【解决方案1】:

你有两个问题:

首先,您没有将正确的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;
}

【讨论】:

  • 我试图运行该程序,但它没有正确完成,它卡在最后一个 for 循环中!
  • 我的意思是进程不会像以前那样结束。
  • @AnisNouri 已修复。这是for 语句的停止条件。它应该在这种情况下停止i &lt; argc - 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多