进程间通过管道进行通信

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

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

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

感谢您的帮助。

#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

最佳答案

您有两个问题:

首先,您没有向管道写入正确的短语。您一直在写短语。对于第一个 child ,这没问题,对于其他 child ,这将是空字符串。

其次,您将在创建第一个子项后关闭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;
}

关于进程间通过管道进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905496/

相关文章:

c - 全局数组是否分配在栈上

在 OpenCL 中检查两个字符 vector 是否相等?

c - 共享内存在 C 中不与进程共享

linux - Signals在Linux和Windows下的实现?

java - JVM 执行 Java 应用程序时,OS 的作用是什么?为什么我们需要操作系统?

c - 在 for 循环 C 中使用 fork() 时出现奇怪的输出

c++ - Unix如何通过两种方式进行管道通信

c - 为什么段错误(核心转储)错误适用于我的 C 程序?

c++ - 为多个测试文件构建可执行文件

c - 使用命令行参数 fork