English 中文(简体)
使用管道将多个字符串发送到子进程
原标题:Sending multiple strings using pipes to child process

我有一个任务在Linux 我不能使它工作。

我有一个程序, 接收文本文件作为参数。 然后它使用 < code> fork () 创建一个子进程, 并发送到子进程, 按行排列收到的文本文件内容作为参数。 子进程需要计算行数, 然后返回到父进程 。

这是我到现在为止拥有的, 但有些儿童程序并不接收所有的线条。 我测试时用的是9行的文本文件。 父母发送了9行作为字符串, 但儿童程序只接收了2或3行。

我做错什么了?

#include <stdio.h>      
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{  
  char string[80];
  char readbuffer[80];
  int pid, p[2];
  FILE *fp;
  int i=0;
  if(argc != 2)
  {
    printf("Syntax: %s [file_name]
", argv[0]);
    return 0;    
  }
  fp = fopen(argv[1], "r");
  if(!fp) 
  {    
    printf("Error: File  %s  does not exist.
", argv[1]);
    return 0;
  }
  if(pipe(p) == -1)
  {
    printf("Error: Creating pipe failed.
");
    exit(0);
  } 
  // creates the child process
  if((pid=fork()) == -1)
  {
   printf("Error: Child process could not be created.
");
    exit(0);
  }  

  /* Main process */
  if (pid) 
  { 
    // close the read
    close(p[0]);    
    while(fgets(string,sizeof(string),fp) != NULL)
    {                
       write(p[1], string, (strlen(string)+1));
       printf("%s
",string);
    } 

    // close the write
    close(p[1]);
    wait(0);
  }

  // child process
  else 
  {   
    // close the write
    close(p[1]); 

    while(read(p[0],readbuffer, sizeof(readbuffer)) != 0) 
    {
      printf("Received string: %s
", readbuffer);    
    }

    // close the read
    close(p[0]); 
  } 
  fclose(fp);       
}
最佳回答

管道是一个单向进程间通信频道。 您必须创建两个管道, 一个可以与儿童进程交谈, 另一个可以读回数据 。

记住关闭两个流程的管道的未用边 。

问题回答

您正在将无效终止者发送到其它进程 :

   write(p[1], string, (strlen(string)+1));

结果是令人困惑的,因为当你打印收到的东西时,你只看到终止者。

以下列方式代替:

   write(p[1], string, strlen(string));

你应该得到你所期望的

您不计行数,而是计数返回 read(2) 的次数。

当使用管道时, read(2) 将尽可能多地从管道中提取数据: min( ipop_ operation, space_ occess) 。 它不关心新线, 0 字节等。 使它发挥作用的简单技巧 :

  • Use a loop to walk readbuffer and look for
  • Use fdopen + fgets (I have a feeling this is probably flawed)

查看管道( man 2 pip ) 的 人页( man 2 pip ), 您试图写入的程序就是一个例子, 比较您的程序 :





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签