paul
paul

Reputation: 11

Why the exit print more strlines in my screen?

I try to make a variation of "cut -c5- " with c using pipes and forks. When i run the program and giving a small .txt file to see if it runs, the program prints me more lines than the file lines.

Here is the code in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>

#define BUFFER_SIZE 1024

void function1(char *argv[]) {    //

        pid_t pid1, pid2;
        int pipefd1[2], pipefd2[2];
        char buffer[BUFFER_SIZE];
        ssize_t byteRead;

        if (pipe(pipefd1) == -1) {    // Create pipe1
            perror("pipe1");
            return;
        }

        if (pipe(pipefd2) == -1) {    // Create pipe2
            perror("pipe2");
            return;
        }

        pid1 = fork();    // Create fork1
        if (pid1 == -1) {
            perror("fork1");
            return;
        }

        pid2 = fork();    // Create fork2
        if (pid2 == -1) {
            perror("fork2");
            return;
        }

        if (pid1 == 0) {    // Child
            close(pipefd1[1]);
            dup2(pipefd1[0], STDIN_FILENO);
            close(pipefd1[0]); 

            close(pipefd2[0]);
            dup2(pipefd2[1], STDOUT_FILENO);
            close(pipefd2[1]); 

            execlp("cut", "cut", "-c5-", NULL);
            perror("exec");
            return;
        }
        else {    // Parrent
            close(pipefd1[0]);    // Close reading

            int filefd = open(argv[1], O_RDONLY);

            if (filefd == -1) {
                perror("Error opening file!");
                close(pipefd1[1]);
                return;
            } 

            memset(buffer, 0, BUFFER_SIZE);    // Clear the buffer before using it

            while ((byteRead = read(filefd, buffer, BUFFER_SIZE)) > 0) {    // Writting the infile to pipefd[1]
                write(pipefd1[1], buffer, byteRead);
            } 

            close(filefd); 
            close(pipefd1[1]);

            wait(NULL);

        }

        if (pid2 == 0) {    // Child
            close(pipefd2[1]);

            while ((byteRead = read(pipefd2[0], buffer, BUFFER_SIZE)) > 0) {

                char *token = strtok(buffer, "\n");
                while (token != NULL) {
                    printf("«Data received through pipe: %s\n", token);
                    token = strtok(NULL, "\n");
                }
            } 

            close(pipefd2[0]);
            return;
        }     
        else {    // Parent
            close(pipefd2[0]);    // Close reading
            close(pipefd2[1]);    // Close writting    

            wait(NULL);
        } 

} 

int main(int argc, char *argv[]) {

    if (argc == 2) {      
        function1(argv);
    }
    else {
        printf("Error! Incorrect syntax");
    }

    return 0;
}

for exampe i gave this a file with these 4 lines:

abcdefgh
123456789
101010101
ababababa

and the program printed me out this:

«Data received through pipe: efgh
«Data received through pipe: 56789
«Data received through pipe: 10101
«Data received through pipe: ababaabcdefgh
«Data received through pipe: 56789
«Data received through pipe: 10101
«Data received through pipe: ababa

and I expected this:

«Data received through pipe: efgh
«Data received through pipe: 56789
«Data received through pipe: 10101
«Data received through pipe: ababa

Upvotes: 1

Views: 19

Answers (0)

Related Questions