亲子过程中的管道,分叉和民意调查

时间:2014-03-08 01:15:17

标签: c fork pipe parent-child polling

我正在进行一项任务,我必须从命令行参数中计算字符数。父母将一次传递一个char,孩子将计算chars的数量并将计数返回给父母,以便它可以打印chars的数量。当我运行我的程序时,它只是坐着而什么都不做。我想我的问题是当我进入将计数传递回父母并收割孩子的阶段时。我认为我的代码在那之前是相当坚实的,那就是我有点模糊。任何帮助将不胜感激。

// 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 <stdlib.h>
#include <unistd.h>        //for fork and pip
#include <sys/wait.h>
#include <string.h>

int main(int argc, char **argv)
{
   pid_t   pid;
   int     comm[2];
   int     status;
   char    src;

   // set up pipe
   if (pipe(comm))
   {
      printf("Pipe Error!\n");
      return -1;
   }

   // call fork()
   pid = fork();

   //check if fork failed
   if (pid < 0)
   {
      printf("Fork Error! %d\n", pid);
      return -1;
   }

   if (pid == 0) 
   {
         // -- running in child process --

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

        int     nChars = 0;
        printf("in child\n");

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

        while (read(comm[0], &src, 1))
        {
            ++nChars;
            printf("testing child loop = %d\n", nChars);
        }

        //close input side of pipe
        close(comm[0]);
        // Return number of characters counted to parent process.
        return nChars;
   }
   else 
   {
         // -- running in parent process --
        int     nChars = 0;

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

        printf("Assignment 3\n");

        // Send characters from command line arguments starting with
        // argv[1] one at a time through pipe to child process.
        int i;
        for (i = 1; i < argc; ++i) //loop through each argument
        {
            int j;
            for (j = 0; j < strlen(argv[i]); ++j)   //loop through each character in argument
                write(comm[1], &argv[i][j], 1);
        }

        //closing the write end of the pipe
        close(comm[1]);

        // Wait for child process to return. Reap child process.
        waitpid(pid, &status, 0);
        // Receive number of characters counted via the value
        // returned when the child process is reaped

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

}

1 个答案:

答案 0 :(得分:0)

基本上你应该如何完成它 - 除非你绝对被迫去返回代码路线。

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

#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)

#define c2p 0
#define p2c 1
#define READEND 0
#define WRITEEND 1

int main(int argc, char **argv)
{
   pid_t   pid;
   int     comm[2][2];
   char    src;

   for (int i = 0; i < 2; ++i)
       if (pipe(comm[i]))
           errExit("pipe");

   if ((pid = fork()) == -1)
       errExit("fork");

   if (! pid)
   {
       close(comm[p2c][WRITEEND]);
       close(comm[c2p][READEND]);

       int nChars = 0;

       while (read(comm[p2c][READEND], &src, 1))
           ++nChars;

       write(comm[c2p][WRITEEND], &nChars, sizeof(nChars));

       close(comm[c2p][WRITEEND]);  //sends eof to parent

       printf("child counted %d chars\n", nChars);

       return 0;
   }

   int nChars = 0;

   close(comm[p2c][READEND]);
   close(comm[c2p][WRITEEND]);

   printf("Assignment 3\n");

   for (int i = 1; i < argc; ++i) //loop through each argument
   {
       int len = strlen(argv[i]);

       for (int j = 0; j < len; ++j)
           write(comm[p2c][WRITEEND], &argv[i][j], 1);
   }

   close(comm[p2c][WRITEEND]); //sends eof to child

   read(comm[c2p][READEND], &nChars, sizeof(nChars));  //should really be in a loop - your task

   close(comm[c2p][READEND]);

   wait(0);

   printf("parent reports %d chars\n", nChars);

   return 0;

}
相关问题