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)) {
        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;
        }
}

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

4

1 回答 1

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;
        }
}
于 2013-08-19T02:03:20.980 回答