0
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
//Functions Prototype
void dadstuff(int [], int);
void kidstuff(int,char*, int [3][2]);

#define NUM 3;
int main(int argc, char *argv[])
{
    int m = rand()%100 + 1;
    int fd [3][2];
    char token[] = "GO_AHEAD\n";
    char readbuffer[80];
    //printf("%d\n",m);
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: [filename]\n");
    }else
    {
        int val = NUM;
        int i, dad, kidid[val];
        char *name = "dad";

        for(i =0; i<val; i++)
        {
            if(-1 == (dad = kidid[i] = fork()))
            {
                printf("Could not produce kid # %d\n", i+1);
                //exit(99);
            }
            if(!dad)
                break;
        }

        if(dad)
        {
            dadstuff(kidid,i);
            pid_t pid;
            int status;

            for(i =0; i<val; i++)
            {
                if(-1 == pipe(fd[i]))
                {
                    fprintf(stderr, "pipe(): Failed to create piple\n");
                    exit(1);
                }
                printf("\nbefore");
                printf("\n");
                //close(fd[i][0]); //close up input side
                write(fd[i][1], token, strlen(token));


                //Waiting for all child to ends before processing in parent
                waitpid(kidid[i], &status, 0);
                printf("\nChild%d, my id is: %d end with status %d\n", i+1, kidid[i],status);
            }
            printf("\nParent: Good bye!\n");
            printf("\n");
        }
        else
        {
            kidstuff(i, argv[1], fd);
            if(i==0) name = "child1";
            if(i==1) name = "child2";
            if(i==2) name = "child3";

            exit(m);
        }
    }
    //return 0;
}
//Parent function
void dadstuff(int kid[], int n)
{
    printf("\nI am the father of the followings: ");
    while (n != 0)
    {
        if(n == 1)
            printf("and ");
        printf( "%d ", kid[--n]);
        //printf("%d----", n);
    }
    printf("\n");
}
//Child 1 to 3 function
void child1(char *fileName)
{
    //do something
}
void child2()
{
    //do something
}
void child3(char *fileName)
{
    //do something
}
void kidstuff(int i, char *fileName, int fd[3][2])
{
    //close(fd[i][1]); //close up output side
    char readbuffer[80];
    int nbytes = 0;

    printf("\nI am kid %d and my id is: %d\n", i+1, getpid());
    //child 1
    if(i+1 == 1)
    {
        //HOW COME PIPE WASN'T RETURNING ANYTHING??
        nbytes = read(fd[i][0],readbuffer,sizeof(readbuffer));

        printf("\n");
        printf("buffer: %s", readbuffer);

        while(readbuffer!="GO_AHEAD")
        {
            nbytes = read(fd[i][0],readbuffer,sizeof(readbuffer));
            printf("buffer: %s", readbuffer);
            printf("\n");
            sleep(1);
        }
        child1(fileName);
    }

    //child 2
    if(i+1 == 2)
    {
        child2();
    }
    //child 3
    if(i+1 == 3)
    {
        child3(fileName);
    }

}

我正在尝试将一个字符串从父进程传递给 3 个子进程。但是由于某种原因,所有子进程都没有从管道中得到任何东西。我做错什么了吗?就像我在子函数中打印读取缓冲区一样,我看不到我从父进程传入的字符串。

有人知道为什么吗?非常感谢你的帮助。

4

1 回答 1

5

在 fork 之后在父进程中创建一个管道。那是行不通的:孩子既看不到父母对fd数组的写入结果,也没有继承管道的文件描述符。

首先要解决的是:pipe在 fork 之前创建一个。有很多注意事项,例如,如果父级正在写入,而子级正在读取,则子级应该close继承管道的写入端才能检测 EOF。

于 2013-02-14T21:47:30.553 回答