使用管道读/写值

时间:2014-05-17 18:21:38

标签: c unix pipe pipeline

我有这个代码的问题,我怎么能发送这个进程的数字,比如1发送到2,2发送到3,3发送到1,每次减少i * 10,就像第一次10,第二次20,...... 30,...直到数字为负,然后停止程序? 我制作了这段代码,但我有一个问题,数字' a'的值不会像a = 1342 in 1,减少20,而2中需要有1322,它的开始从1342年。 这是我的代码:

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

int i=0;
int main()
{
    pid_t p;
    int a=1324;
    int fd[2];
    int fd1[2];
    pipe(fd);
    p=fork():
    if(p < 0)
    {
        perror("Fork error");
        exit(1);
    }
    else if(p==0)
    {
    while(1)
    {
        close(fd1[1]);
        read(fd1[0],&a, sizeof(int));
        printf("Procces 2, a is %d.\n",a);
        wait(1);
        close(fd[0]);
        a=a-(i*10);
        i++;
        write(fd[1],&a,sizeof(int));
    }
    }
    else
    {
    p=fork();
    if(p < 0)
    {
        perror("Fork error");
        exit(1);
    }
    if(p > 0)
    {
        while(1)
        {
            close(fd1[1]);
            read(fd1[0],&a, sizeof(int));
            printf("Procces 1, a is %d.\n",a);
            wait(1);
            close(fd1[0]);
            a=a-(i*10);
            i++;
            write(fd1[1],&a,sizeof(int));
        }
    }
    else
    {
        while(1)
        {
            close(fd[1]);
            read(fd[0],&a, sizeof(int));
            printf("Procces 3, a is %d.\n",a);
            wait(1);
            close(fd1[0]);
            a=a-(i*10);
            i++;
            write(fd1[1],&a,sizeof(int));
        }
    }
}
return 1;

}

  • 我想这样打印:
  • 过程1,a是1324(减少0并发送到2)
  • 过程2,a为1324(减少10并发送至3)
  • 过程3,a是1314(减少20并发送到1)
  • 过程1,a是1294(减少30并发送到2)
  • ...
  • 但程序有点像这样:
  • 过程1,a是1324
  • 过程2,a是1324
  • 过程3,a是1324
  • 过程1,a是1314
  • ...

有人可以告诉我问题出在哪里?感谢。

1 个答案:

答案 0 :(得分:0)

如果这不是你想要做的事情。您希望清楚地识别哪些管道用于写入和读取每个过程的 。我发现这样做的最简单方法是使用宏标记索引。例如。一系列六个管道(三对):

//  This arrangement means;
//
//  P1 listens to P2, writes to P3
//  P2 listens to P3, writes to P1
//  P3 listens to P1, writes to P2

#define P1_READ     0
#define P2_WRITE    1
#define P2_READ     2
#define P3_WRITE    3
#define P3_READ     4
#define P1_WRITE    5
#define NUM_PIPES   6

这使客户端代码基本更容易理解:

int main()
{
    int a = 1324;
    int i=1;

    int fd[NUM_PIPES];

    // create pipes
    if (pipe(fd) < 0 || pipe(fd+2) < 0 || pipe(fd+4) < 0)
    {
        perror("Failed to create pipes");
        exit(EXIT_FAILURE);
    }

    // fork P2 child process
    if (fork() == 0)
    {
        close(fd[P1_READ]);
        close(fd[P1_WRITE]);
        close(fd[P3_READ]);
        close(fd[P3_WRITE]);

        while (read(fd[P2_READ], &a, sizeof(a)) == sizeof(a))
        {
            fprintf(stderr, "P2: (i==%d) received %d\n", i, a);
            if ((a -= (i++ * 10)) < 0)
                break;
            write(fd[P2_WRITE], &a, sizeof(a));
        }

        // close our other pipes
        close(fd[P2_READ]);
        close(fd[P2_WRITE]);
        return EXIT_SUCCESS;
    }
    ////////////////////////////////////////////////////////////////


    // fork P3 child process
    if (fork() == 0)
    {
        close(fd[P1_READ]);
        close(fd[P1_WRITE]);
        close(fd[P2_READ]);
        close(fd[P2_WRITE]);

        while (read(fd[P3_READ], &a, sizeof(a)) == sizeof(a))
        {
            fprintf(stderr, "P3: (i==%d) received %d\n", i, a);
            if ((a -= (i++ * 10)) < 0)
                break;
            write(fd[P3_WRITE], &a, sizeof(a));
        }

        // close our other pipes
        close(fd[P3_READ]);
        close(fd[P3_WRITE]);
        return EXIT_SUCCESS;
    }
    ////////////////////////////////////////////////////////////////


    // parent process. close the pipes we don't need
    close(fd[P2_READ]);
    close(fd[P2_WRITE]);
    close(fd[P3_READ]);
    close(fd[P3_WRITE]);

    // kick things off with a write of the first value
    write(fd[P1_WRITE], &a, sizeof(a));

    // same code as before
    while (read(fd[P1_READ], &a, sizeof(a)) == sizeof(a))
    {
        fprintf(stderr, "P1: (i==%d) received %d\n", i, a);
        if ((a -= (i++ * 10)) < 0)
            break;
        write(fd[P1_WRITE], &a, sizeof(a));
    }

    // close the pipes we no longer need
    close(fd[P1_READ]);
    close(fd[P1_WRITE]);

    wait(NULL);

    return EXIT_SUCCESS;
}

结果输出如下所示:

P3: (i==1) received 1324
P2: (i==1) received 1314
P1: (i==1) received 1304
P3: (i==2) received 1294
P2: (i==2) received 1274
P1: (i==2) received 1254
P3: (i==3) received 1234
P2: (i==3) received 1204
P1: (i==3) received 1174
P3: (i==4) received 1144
P2: (i==4) received 1104
P1: (i==4) received 1064
P3: (i==5) received 1024
P2: (i==5) received 974
P1: (i==5) received 924
P3: (i==6) received 874
P2: (i==6) received 814
P1: (i==6) received 754
P3: (i==7) received 694
P2: (i==7) received 624
P1: (i==7) received 554
P3: (i==8) received 484
P2: (i==8) received 404
P1: (i==8) received 324
P3: (i==9) received 244
P2: (i==9) received 154
P1: (i==9) received 64

玩具有数字(ia),让您悠闲自在。希望它有所帮助。