The issuse: we create 2 pipes, the father forks and creates a child, pasing it both of the pipes. the father sends info through his pipe to the kid. the kid after reciving it should send it back. the kid should send it back through out the stdout using the library functions: the problem is in this section:
the parent expects output from child using read :
//READ FOR INT
read(pipe_child_to_father[0], &number_recived, sizeof(int));
printf("number recived: %d \n", number_recived);
the child sends iput using either printf and fflush or write I commented above each one which works and which dont
//---------------------------------SECTION WITH PROBLEM ------------------------
//-------------------WITH INTS
//WORKS
write(STDOUT_FILENO,&number, sizeof(int));
//NOT WORKING !!! sends wrong input
printf("%d",number);
fflush(stdout);
//NOT WORKING !! gets stuck waiting for input
//printf("%d",number);
//NOT WORKING !!! sends wrong input
fprintf(stdout, "%d", number);
fflush(stdout);
I am pasting the full code as well at the buttom.
the output i get: if the kid using printf to send data to the father(using fflush to clear buffer) - the numbers should be the same!:
number sent :34461
number recived: 909194548
number sent :168284
number recived: 842348086
number sent :138440
number recived: 909457719
number sent :141832
number recived: 909193265
if i dont use fflush - the father gets stuck waiting for input....
few notes:
- This thread is not about asking help in solving hw.
- This problem was stumbled on part of the assigment, I know there are things like message ques and named pipes that might be better for this kind of job, but we should only use named pipes.
the full code if some one wants to copy paste it:
#include <stdlib.h> //for exit_succes #include <stdio.h> // for prints #include <unistd.h> //sleep for debug #include <sys/types.h> // for pid_t #include <errno.h> //for perror #include <signal.h> //for signal #include <time.h> #define true 1 #define false 0 #define MAX_NUMBERS 2000 #define MAX_RND 200000 #define FATHER_RUNS 50 int iteri_search( int numbers[], int number); void father_process(); void init_array(int array[]); void kid(int array[], int pipe_recive[], int pipe_transmit[]); void get_data(int pipe_father_to_bianry[], int pipe_binary_to_father[]); int main (){ father_process(); exit(EXIT_SUCCESS); } void father_process(){ pid_t pid; //setting array int array[MAX_NUMBERS]; //initializing it init_array(array); //setting pipes int pipe_father_to_child[2]; int pipe_child_to_father[2]; //Open all pipes if(pipe(pipe_father_to_child) == -1) { perror("Error"); exit(EXIT_FAILURE); } if(pipe(pipe_child_to_father) == -1) { perror("Error"); exit(EXIT_FAILURE); } pid = fork(); //first kid if(pid == 0) { kid(array,pipe_father_to_child, pipe_child_to_father); } ///close unwanted pipes for father close(pipe_child_to_father[1]); close(pipe_father_to_child[0]); get_data(pipe_father_to_child, pipe_child_to_father); } void get_data(int pipe_father_to_child[], int pipe_child_to_father[]){ int index_of_run = 0; int number_recived = 0; double double_recived =0; while(index_of_run < FATHER_RUNS){ int number = rand()%MAX_RND; write(pipe_father_to_child[1], &number, sizeof(int)); printf("number sent :%d \n", number); //SENDING THE INPUT ------------------------ THE PROBLEMATIC PART //READ FOR INT read(pipe_child_to_father[0], &number_recived, sizeof(int)); printf("number recived: %d \n", number_recived); ++index_of_run; } } void kid(int array[], int pipe_recive[], int pipe_transmit[]){ //will mesure time //close unwanted pipes close (pipe_recive[1]); close (pipe_transmit[0]); dup2(pipe_transmit[1],STDOUT_FILENO); int number = 0; int index =0; while(index < 50) { //get number from father read(pipe_recive[0], &number, sizeof(int)); // turn on clock //---------------------------------SECTION WITH PROBLEM ------------------------ //WORKS - correct output //write(STDOUT_FILENO,&number, sizeof(int)); //NOT WORKING !!! sends wrong input as explained above //printf("%d",number); //fflush(stdout); //NOT WORKING !! gets stuck waiting for input //printf("%d",number); //NOT WORKING !!! sends wrong input as explained with printf //fprintf(stdout, "%d", number); //fflush(stdout); ++index; } exit(EXIT_SUCCESS); } //initializing array with values void init_array(int array[]){ srand(17); int index; for(index = 0; index < MAX_NUMBERS; ++index){ array[index] = rand(); } }
reads orwrites. Also, you've commented out large sections of the code. Comments like "WORKS" or "NOT WORKING" are not particularly descriptive. Which code exactly did what? Are we supposed to guess which comments should be undone? - William Purselldup2(x,y), you typically doclose(x)to avoid having 2 copies of the descriptor. - William Pursellwrite(1, &number, sizeof number)andprintf("%d", number). But I'm not sure what "NOT WORKING !!! sends wrong input" actually means. - William Pursell