Linux:使用管道在进程之间发送信息

时间:2015-10-24 21:42:52

标签: c linux pipe

使用fd []和fd1 []创建管道。我正在写管道。拿两个管道给我错误,因为我引用了STDIN_FILENO,所以我删除了它。我只是用管道来读写。现在它仍然没有从管道读取。文件描述符fd []的管道工作。但是fd1 []没有。

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

    int main(int argc, char *argv)
    {

    int fd[2]; 
    pid_t childpid;
    pipe(fd);
    int fd2[2];
    pipe(fd2);
    int array[5] = {1,2,3,4,5};
    int array2[5] = {11,12,13,14,15};
    //fprintf(stderr,"size%ld",sizeof(int));

    //char buffer[111];
    int *subarr1;

    int buffer_num;
    int status;

    if ((childpid = fork()) == 0) {
        fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());

        //dup2(fd[1], STDOUT_FILENO);
        close(fd[0]); //read end
        write(fd[1],array,100);
        close(fd[1]);
        //execl("/bin/ls", "ls", "-l", NULL);
        perror("The exec of ls failed\n");

            if ((childpid = fork()) == 0)
             {
                 fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());

                //dup2(fd2[1], STDOUT_FILENO);
                close(fd2[0]); //read end
                write(fd2[1],array2,sizeof(array2));
                close(fd2[1]);
                //execl("/bin/ls", "ls", "-l", NULL);
                perror("The exec of ls failed\n");
            }
            else
            {// parent 
                wait(NULL);
            }
    }
        else {
                wait(&status);
                fprintf(stderr,"\nTHis is parent");
                subarr1 = (int*)malloc(sizeof(int)*(5));
                fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());

            //dup2(fd[0], STDIN_FILENO);
            close(fd[1]);

            int j;
            for(j=0;j<5;j++)
            {
                read(fd[0],&buffer_num,sizeof(int));
                subarr1[j] = buffer_num;
                //printf("%s",buffer);

            }

            for(j=0;j<5;j++)
            {
                fprintf(stderr,"\n%d\n",subarr1[j]);
            }

            close(fd[0]);//read end


            //dup2(fd2[0], STDIN_FILENO);
            buffer_num = 0;

            close(fd2[1]);
            //read(fd2[0],&buffer_num,100);

            for(j=0;j<5;j++)
            {
                read(fd2[0],&buffer_num,sizeof(int));
                subarr1[j] = buffer_num;
                //printf("%s",buffer);

            }

            for(j=0;j<5;j++)
            {
                fprintf(stderr,"\n%d\n",subarr1[j]);
            }
            //fprintf(stderr, "\nThis is second pipe%s", buffer);
            close(fd2[0]);


        /*//printf("\nAAAAAAAA%s\n",buffer);
            dup2(fd2[0], STDIN_FILENO);
            close(fd2[1]);
        printf("\nAAAAAAAA%s\n",buffer);

            read(fd2[0],buffer,100);
            printf("%s",buffer);
            close(fd2[0]);//read end
        printf("\nAAAAAAAA%s\n",buffer);

    */
            //execl("/usr/bin/sort", "sort", "-n", NULL);
            perror("The exec of sort failed\n");
        }

    return 0;

    }

1 个答案:

答案 0 :(得分:0)

这个电话:

read(fd2[0],&buffer_num,100);

尝试从local(int)变量buffer开始向内存中读取100个字节,因此覆盖堆栈之后的其他内容,粉碎堆栈并导致未定义的行为(可能在某些时候崩溃)。

你的write调用遇到了类似的问题,从更小的对象写入100个字节,但那些可能只是写入垃圾然后忽略,而不是导致崩溃(尽管这同样是未定义的行为)。