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: -
#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!
1 1811
I have tried it also like this: -
#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..
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |