parent向child发送命令行参数

时间:2013-08-19 01:48:16

标签: c pipe parent command-line-arguments

我正在编写一个创建管道,forks的程序,然后父进程一次将命令行参数发送给子进程一个char。孩子应该计算它们,然后父母收割孩子并打印出有多少参数。以下是我到目前为止的情况:

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

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

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


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

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

    else if (pid == 0) {
        // -- running in child process --
        int     nChars = 0;
        close(comm[1]);

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

        while(read(comm[0], buffer, sizeof(buffer)) != '\n')
            nChars++;

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

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

        char endl='\n';
        for (int a = 1; a < argc; a++) {
            for (int c = 0; c < strlen(argv[a]); c++) {
                write(comm[1], &argv[a][c], 1);
            }
        }
        write(comm[1], &endl, 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.

        waitpid(pid, &status, 0);

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

它似乎无休止地运行。它必须卡在其中一个循环中。出了什么问题?

1 个答案:

答案 0 :(得分:0)

代码:

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

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

    // set up pipe
    if (pipe(comm) < 0) {
        printf("pipe error\n");
        return -1;
    }
    // call fork()
    if((pid = fork()) <0)
    {
        printf("fork error %d\n", pid);
        return -1;
    }
    else if (pid == 0) {
        // -- running in child process --
        int     nChars = 0;
        close(comm[1]);
        //printf("%d \n",BUFSIZ);
        // Receive characters from parent process via pipe
        // one at a time, and count them.
        int n;
        while( (n =read(comm[0], buffer, BUFSIZ)) >0)
    {
      buffer[n] = 0;
      int oneChar, i = 0,endflag = 0;
      while((oneChar = buffer[i])!=0)
      {
       // printf("%d\n",oneChar);
        if(oneChar!=EOF)
          nChars++;
        else
        {
          endflag = 1;
          break;
        }
        i++;
      }
      //printf("%s\n",buffer);
      if(endflag)
        break;
    }
    printf("nChar : %d",nChars);
        // Return number of characters counted to parent process.
        return nChars;
    }
    else {
        // -- running in parent process --
        //int     nChars = 0;
        close(comm[0]);

        // Send characters from command line arguments starting with
        // argv[1] one at a time through pipe to child process.
        int a,c;
        char endl='\n';
        for ( a = 1; a < argc; a++) {
            for ( c = 0; c < strlen(argv[a]); c++) {
                write(comm[1], &argv[a][c], 1);
            }
        }
    printf("write end\n");
    int end = EOF;
    write(comm[1],&end,4);


        waitpid(pid, &status, 0);

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