473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Need help on pipes!

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
1 1811
jewel87
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

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

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
0
by: Christian Hammers | last post by:
Hello I would like to call a unix shellscript from within a PHP script and - write data to its STDIN - read data from its STDOUT *and* STDERR - get its exit code afterwards proc_open seems...
2
by: Serge A. Ribalchenko | last post by:
Hi all, I'm new in python coding... Can someone point me how can I do such a usual thing? I need to ping some hosts, and harvest results in one list. Just cause there is about 120 hosts in the...
5
by: glenn.owens | last post by:
In the process of doing some routine monitoring/clean-up we've discovered that several (many?) users are apparently set to access our SQL Server 2000 database instances via the Named Pipes...
2
by: Rudolf Bargholz | last post by:
Hi, DB2 7.1 FP11 Windows 2000 Earlier this evening, after dropping and recreating a trigger, DB2 locked up. I am not entirely sure that the cause of the problem was the replacing of the...
9
by: Hans J?rg Brinksmeyer | last post by:
Hi, does anyone have an idea for this problem: I use anonymous pipes to steer a console program under Win2000 with a second 'steering aplication'. The stdin and output are redirected to...
7
by: Alan Pretre | last post by:
I have an application installed at a customer site that has been getting a general network error for a couple of years. I was hoping that .NET 2.0 would clear it up, but unfortunately it didn't. ...
1
by: Jarrod Morrison | last post by:
Hi All Im looking for a way use named pipes between a service app and an app run when a user logs on and be able to pass string based data, im hoping that the service can contact the app that is...
2
by: fahad.usman | last post by:
I am making an application in which if the second instance of the same application is launched, it checks its command-line arguments and passes them to the already running instance. I have been...
7
by: andrewb | last post by:
Hi all, Having some trouble using named pipes and Visual Basic .NET and would appreciate and help you could offer. Essentially I am trying to develop a simple client/server application that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.