Read/write value with pipe

VladutZzZ

I have a problem with this code, how i can send a number betwen this procces, like 1 send to 2, 2 send to 3, and 3 send to 1, and everytime decreases with an i*10, like first time 10, second time 20, ...30 , ... until the number is negative, and then stop the program? I make this code, but i have a problem, the value of number 'a', dont decrease like a=1342 in 1, decrease 20, and in 2 need to have 1322, it's start from 1342. Here is my code:

 #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;

}

  • I want to print like this:
  • Process 1, a is 1324 (decrease 0 and send to 2)
  • Process 2, a is 1324 (decrease 10 and send to 3)
  • Process 3, a is 1314 (decrease 20 and send to 1)
  • Process 1, a is 1294 (decrease 30 and send to 2)
  • ...
  • But the program si some like this:
  • Process 1, a is 1324
  • Process 2, a is 1324
  • Process 3, a is 1324
  • Process 1, a is 1314
  • ...

Someone can tell me where is the problem? Thanks.

WhozCraig

If this isn't what you're trying to do its damn close. You want to clearly identify which pipes are use for writing and reading for each process. The easiest way I've found to do that is by labeling the indexes with macros. For example. a sequence of six pipes (three pairs):

//  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

This makes the client code substantially easier to understand:

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

The resulting output looks something like this:

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

Toy with the numbers (i, a,) to your leisure. Hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to forward pipe a value to the left side parameter of a function?

From Dev

readonly public, readwrite private property

From Dev

How do I use pipe to keep the value that is in a child process?

From Dev

How to pipe a value into a popen3

From Dev

Linux pipe, fork and execlp: how to get the value written into stream 1

From Dev

Pass random value to gulp pipe template

From Dev

Ruby constant double pipe equals value vs. defined? constant

From Dev

reading a value from a pipe isn't working

From Dev

Impala Query: Find value in pipe-separated list

From Dev

Reentrant readwrite lock synchronization on isWriteLocked

From Dev

Fetching value from Pipe-delimited String using Regex (Oracle)

From Dev

Angular 2 Pipe - can't return value inside a promise

From Dev

Observable with Async Pipe in template is not working for single value

From Dev

How to populate html table value conditionally with date pipe?

From Dev

How do I pipe output to date -d "value"?

From Dev

Extracting a string value from a pipe-separated string in a table column

From Dev

Pass random value to gulp pipe template

From Dev

How to pass value to read variable with pipe (stdin) in bash

From Dev

Unable to get the accurate value of shell variable set inside a pipe

From Dev

Max Value search in array using fork and pipe

From Dev

passing value down the pipe in Scala parser combinator

From Dev

How to get all values from a variable made of value separated by a pipe ( | )?

From Dev

readwrite and readonly mutually exclusive

From Dev

Java unlock ReadWrite lock safely?

From Dev

bash return value in pipe to bash

From Dev

Check value in when inside an pipe function

From Dev

Angular2 pipe dynamic value not taking

From Dev

Angular - number pipe if there's a value

From Dev

explode pipe delimiter string into array with name and value

Related Related

  1. 1

    How to forward pipe a value to the left side parameter of a function?

  2. 2

    readonly public, readwrite private property

  3. 3

    How do I use pipe to keep the value that is in a child process?

  4. 4

    How to pipe a value into a popen3

  5. 5

    Linux pipe, fork and execlp: how to get the value written into stream 1

  6. 6

    Pass random value to gulp pipe template

  7. 7

    Ruby constant double pipe equals value vs. defined? constant

  8. 8

    reading a value from a pipe isn't working

  9. 9

    Impala Query: Find value in pipe-separated list

  10. 10

    Reentrant readwrite lock synchronization on isWriteLocked

  11. 11

    Fetching value from Pipe-delimited String using Regex (Oracle)

  12. 12

    Angular 2 Pipe - can't return value inside a promise

  13. 13

    Observable with Async Pipe in template is not working for single value

  14. 14

    How to populate html table value conditionally with date pipe?

  15. 15

    How do I pipe output to date -d "value"?

  16. 16

    Extracting a string value from a pipe-separated string in a table column

  17. 17

    Pass random value to gulp pipe template

  18. 18

    How to pass value to read variable with pipe (stdin) in bash

  19. 19

    Unable to get the accurate value of shell variable set inside a pipe

  20. 20

    Max Value search in array using fork and pipe

  21. 21

    passing value down the pipe in Scala parser combinator

  22. 22

    How to get all values from a variable made of value separated by a pipe ( | )?

  23. 23

    readwrite and readonly mutually exclusive

  24. 24

    Java unlock ReadWrite lock safely?

  25. 25

    bash return value in pipe to bash

  26. 26

    Check value in when inside an pipe function

  27. 27

    Angular2 pipe dynamic value not taking

  28. 28

    Angular - number pipe if there's a value

  29. 29

    explode pipe delimiter string into array with name and value

HotTag

Archive