Metaozis
Metaozis

Reputation: 31

Pipe: How do I ensure if I successfully write into the pipe?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#define SIZE 50

struct record {
    int freq;
    char word[SIZE];
};

int main(){

int number_process = 3;
int pipes[number_process][2];

struct record r1;
r1.freq = 10;
strcpy(r1.word, "Cat");

struct record r2;
r2.freq = 20;
strcpy(r2.word, "Elephant");

struct record r3;
r3.freq = 30;
strcpy(r3.word, "Dragon");

struct record records_array[3] = {r1, r2, r3}; 

for (int i = 0; i < number_process; i++){

    if (pipe(pipes[i]) == -1){
        perror("pipe");
        exit(1);
    }

    // Create children.
    pid_t fork_result = fork();
    if (fork_result == -1){
        perror("Parent fork");
        exit(1);
    } else if (fork_result == 0){

        if (close(pipes[i][0]) == -1){
            perror("Child closes reading port");
            exit(1);
        }

        // Later children is going to close all reading port from pipe that parent creates.
        for (int child_no = 0; child_no < i; child_no++) {
            if (close(pipes[child_no][0]) == -1) {
                perror("close reading ends of previously forked children");
                exit(1);
            }
        }

        // Now, I am trying to write each strct record member from the above array into the pipe
        // when I run the program, it won't allow me to do so because of bad file descriptor exception.
        for (int j = 0; j < number_process; i++){
            if (write(pipes[i][1], &(records_array[j]), sizeof(struct record)) == -1){
                perror("write from child to pipe");
                exit(1);
            }
        }

        // Finishing writing, close the writing end in pipe.
        if (close(pipes[i][1]) == -1){
            perror("Child closes writing port");
            exit(1);
        }
        // Terminate the process.
        exit(0);

    } else {
        // Parent is closing all the writing ends in pipe.
        if (close(pipes[i][1]) == -1){
            perror("Parent close writing");
            exit(1);
        }

    }

}

struct record buffer;

for (int i = 0; i < number_process; i++){
    // Parent reads from the pipe.
    if (read(pipes[i][0], &buffer, sizeof(struct record)) == -1){
        perror("parent read");
        exit(1);
    }

    printf("buffer.freq = %d\n", buffer.freq);
    printf("buffer.word = %s\n", buffer.word);

}


    return 0;

}

I am new to system programming, the following code is some practice I implement to see the pipe functionality. I have a few questions to my code:

1) Is there any system call or library call that will help me to ensure the content that I want to write into the pipe has actually been successfully written into the pipe? In other words, is there any methods available for me to check the content/data I wrote into the pipe?

2) I feel my parent reading part is not implemented correct, when I run this code, my parent reading part reads consecutive 3 same things, although it should be different things each time it read.

3) I have confronted the bad addresses issue when I try to read something from the pipe in my parent process, what is the reason that this error occurs?

Can someone please help me understand this stuff? Really appreciated.

Upvotes: 0

Views: 316

Answers (1)

William Pursell
William Pursell

Reputation: 212634

You've got a simple cut-n-paste error. for (int j = 0; j < number_process; i++){

You need to increment j.

Upvotes: 2

Related Questions