Need help on pipes!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jewel87
    New Member
    • Jan 2007
    • 62

    Need help on pipes!

    Hello everyone,
    I have problems with my code in C doing pipes. It is currently running into an indefinite loop, but I was trying all possible modifications I could think of, nothing have led me to the solution.
    I have this task:
    Write a program that creates three processes, two write down the pipe and one that reads from the pipe. The first child process writes the first 20 odd integers, and the second child process writes the first 20 even integers. Make the reading process (the third child process) print any messages it receives on its standard output. Find out a solution and modify the program to guarantee the reader can get the numbers in numerical order.


    My code is:

    Code:
    #include <stdio.h>
    
    main()
    {
    
       char buff[2];
       int odd =1;
       int count =1;
       int flag =0;
       int even =2;
    
       int p[2], pid1, pid2, pid3;
       pipe(p);
       pid1 = fork();
       pid2 = fork();
       pid3 = fork();
    
    while(count < 21)
       {
            if (( pid1 = 0 )&&( flag == 0 ))
                    {
                            printf("Odd number: \n");
                            write(p[1], odd);
                            odd = odd + 1;
                            flag = 1;
                    }
            else
            {
              sleep(1);
              read(p[0], buff);
              printf("Number read: %s\n", buff);
            }
    
            if (( pid2 = 0 )&&( flag == 1 ))
                    {
                            printf("Even number: \n");
                            write(p[1], even);
                            even = even + 2;
                            flag = 0;
                  }
            else
              {
               sleep(1);
               read(p[0], buff);
               printf("Number read: %s\n", buff);
             }
    
         count = count + 1;
      }
    
    }
    Can anyone help me, please?? This is urgent!
  • jewel87
    New Member
    • Jan 2007
    • 62

    #2
    I have tried it also like this:
    Code:
    #include <stdio.h>
    
    main()
    {
    
       char buff[2];
       int odd =1;
       int count =1;
       int flag =0;
       int even =2;
    
       int p[2], pid1, pid2, pid3;
       pipe(p);
       pid1 = fork();
       pid2 = fork();
       pid3 = fork();
    
    while(count < 21)
       {
            if (( pid1 > 0 )&&( flag == 0 ))
                    {
                            printf("Odd number: \n");
                            write(p[1], odd);
                            odd = odd + 1;
                            flag = 1;
                    }
            else
                    if (( pid2 > 0 )&&( flag == 1 ))
                    {
                            printf("Even number: \n");
                            write(p[1], even);
                            even = even + 2;
                            flag = 0;
                    }
           
               sleep(1);
               read(p[0], buff);
               printf("Number read: %s\n", buff);
    
         count = count + 1;
      }
    }
    This gives some response, basically it prints the printf statements in some arbitrary sequence, but I don't get any numbers printed..

    Comment

    Working...