By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,483 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,483 IT Pros & Developers. It's quick & easy.

Need help on pipes!

P: 62
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5.  
  6.    char buff[2];
  7.    int odd =1;
  8.    int count =1;
  9.    int flag =0;
  10.    int even =2;
  11.  
  12.    int p[2], pid1, pid2, pid3;
  13.    pipe(p);
  14.    pid1 = fork();
  15.    pid2 = fork();
  16.    pid3 = fork();
  17.  
  18. while(count < 21)
  19.    {
  20.         if (( pid1 = 0 )&&( flag == 0 ))
  21.                 {
  22.                         printf("Odd number: \n");
  23.                         write(p[1], odd);
  24.                         odd = odd + 1;
  25.                         flag = 1;
  26.                 }
  27.         else
  28.         {
  29.           sleep(1);
  30.           read(p[0], buff);
  31.           printf("Number read: %s\n", buff);
  32.         }
  33.  
  34.         if (( pid2 = 0 )&&( flag == 1 ))
  35.                 {
  36.                         printf("Even number: \n");
  37.                         write(p[1], even);
  38.                         even = even + 2;
  39.                         flag = 0;
  40.               }
  41.         else
  42.           {
  43.            sleep(1);
  44.            read(p[0], buff);
  45.            printf("Number read: %s\n", buff);
  46.          }
  47.  
  48.      count = count + 1;
  49.   }
  50.  
  51. }
  52.  
  53.  
Can anyone help me, please?? This is urgent!
Feb 26 '08 #1
Share this Question
Share on Google+
1 Reply

P: 62
I have tried it also like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5.  
  6.    char buff[2];
  7.    int odd =1;
  8.    int count =1;
  9.    int flag =0;
  10.    int even =2;
  11.  
  12.    int p[2], pid1, pid2, pid3;
  13.    pipe(p);
  14.    pid1 = fork();
  15.    pid2 = fork();
  16.    pid3 = fork();
  17.  
  18. while(count < 21)
  19.    {
  20.         if (( pid1 > 0 )&&( flag == 0 ))
  21.                 {
  22.                         printf("Odd number: \n");
  23.                         write(p[1], odd);
  24.                         odd = odd + 1;
  25.                         flag = 1;
  26.                 }
  27.         else
  28.                 if (( pid2 > 0 )&&( flag == 1 ))
  29.                 {
  30.                         printf("Even number: \n");
  31.                         write(p[1], even);
  32.                         even = even + 2;
  33.                         flag = 0;
  34.                 }
  35.  
  36.            sleep(1);
  37.            read(p[0], buff);
  38.            printf("Number read: %s\n", buff);
  39.  
  40.      count = count + 1;
  41.   }
  42. }
  43.  
  44.  
This gives some response, basically it prints the printf statements in some arbitrary sequence, but I don't get any numbers printed..
Feb 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.