C中的进程间通信,一次一个字符

时间:2014-06-01 06:39:54

标签: c pipe child-process inter-process-communicat

首先,这是IS作业,我不是要求答案,但我对某些事感到困惑。

我有编程课的作业,我对如何以教师提出的具体方式编写代码感到困惑。

程序首先创建一个子进程,然后继续从父进程发送命令行参数,通过管道,一次一个CHARACTER到子进程,然后将它们读入子进程ONE CHARACTER at a time,每次读入一个字符时递增子进程中的字符数。

我认为我已经完成了一次一个字符通过管道发送数据,但我不知道每次发送字符时如何“转到”子进程,读取它,增加字符数,以及然后回到父进程并重复。

这是我的代码,它可以工作并给出准确的答案,但是如何完成我的导师所要求的任何提示都将不胜感激,谢谢!

// Characters from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process counts number of characters sent through pipe.
//
// Child process returns number of characters counted to parent process.
//
// Parent process prints number of characters counted by child process.

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

int main(int argc, char **argv)
{
// set up pipe

    int    pA[2];
char   buff[50];

pipe(pA);
// call fork()

pid_t  childId = fork();

if (childId == 0) {
    // -- running in child process --
    int     nChars = 0;

    // close the output side of pipe
    close(pA[1]);

    // Receive characters from parent process via pipe
    // one at a time, and count them.

    nChars = read(pA[0], buff, sizeof(buff)); //this line of code is what i need to change to be reading characters in one at a time

    // Return number of characters counted to parent process.
    return nChars;
    }
else {
    // -- running in parent process --
    int     nChars = 0;
    int     size = 0;
    printf("CS201 - Assignment 3 - Timothy Jensen\n");

    // close the input side of the pipe
    close(pA[0]);

    // Send characters from command line arguments starting with
    // argv[1] one at a time through pipe to child process.

    for (int i = 1; i < argc; i++)
      {
        size = strlen(argv[i]);
        for (int z = 0; z < size; z++)
          {
        write(pA[1], &argv[i][z], 1);
          }
      }

    // Wait for child process to return. Reap child process.
    // Receive number of characters counted via the value
    // returned when the child process is reaped.

    wait(&nChars);

    printf("child counted %d chars\n", nChars/256);
    return 0;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要进行以下更改:

  1. read的调用中使最后一个参数为1。

    read(pA[0], buff, 1);
    
  2. 将上述调用置于while循环中,并为nChar的每次成功尝试增加read

       while ( read(pA[0], buff, 1) == 1 )
       {
          ++nChars;
       }
    
  3. 完成写入后,从父进程中关闭文件描述符。

  4. 这是main的工作版本。

    int main(int argc, char **argv)
    {
       // set up pipe
    
       int    pA[2];
       char   buff[50];
    
       pipe(pA);
       // call fork()
    
       pid_t  childId = fork();
    
       if (childId == 0) {
           // -- running in child process --
           int     nChars = 0;
    
           // close the output side of pipe
           close(pA[1]);
    
           // Receive characters from parent process via pipe
           // one at a time, and count them.
           while ( read(pA[0], buff, 1) == 1 )
           {
              ++nChars;
           }
    
           return nChars;
       }
       else {
           // -- running in parent process --
           int     nChars = 0;
           int     size = 0;
           printf("CS201 - Assignment 3 - Timothy Jensen\n");
    
           // close the input side of the pipe
           close(pA[0]);
    
           // Send characters from command line arguments starting with
           // argv[1] one at a time through pipe to child process.
    
           for (int i = 1; i < argc; i++)
           {
              size = strlen(argv[i]);
              for (int z = 0; z < size; z++)
              {
                 write(pA[1], &argv[i][z], 1);
              }
           }
    
           close(pA[1]);
    
           // Wait for child process to return. Reap child process.
           // Receive number of characters counted via the value
           // returned when the child process is reaped.
    
           wait(&nChars);
    
           printf("child counted %d chars\n", nChars/256);
           return 0;
       }
    }
    

答案 1 :(得分:1)

看起来有点傻,但你可以改变:

nChars = read(pA[0], buff, sizeof(buff)); 

到:

char ch;
nChars = read(pA[0], &ch, 1); 

当然,您可以将上述内容放入循环中,将字符串“一次一个字符”组合回buff