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:
Can anyone help me, please?? This is urgent!
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; } }
Comment