Pipe Outlet Duplication

Can someone explain to me why my output has duplicates in it from the ls command. Normal operation ls -l | sortdoes not give me a duplicate result, so what could be the problem?

Essentially, I'm trying to pass the output from one command and enter it into another command. The program still works, but the output shows duplicate data. Plus, and an explanation of why I would need to close after dup2 would be really useful :)

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


// function declarations
void executeLs(int data_pipe[]);
void executeSort(int data_pipe[]);

int main(){

        int data_pipe[2]; // array storing the file descriptors
        int childls_pid; // ls child process
        int childSort_pid; // sort child process
        int rc; // return vaue of the pipe
        int child_status1;
        int child_status2;

        rc = pipe(data_pipe);
        if(rc == -1){
            perror("pipe");
            exit(1);
        }


        childls_pid = fork();
        childSort_pid = fork();

        // Ls Child process
        switch(childls_pid) {
        case -1:
                perror("fork childLs Error");
                exit(1);
        case 0:
                // inside of child process
                executeLs(data_pipe);
                exit(0);
        default:
            break;
        }

        // Sort child process
        switch(childSort_pid) {
        case -1:
                perror("fork childSort Error");
                exit(1);
        case  0:
                executeSort(data_pipe);
                exit(0);
        default:
                wait(&child_status2);
        }
        return 0;
}
void executeLs(int data_pipe[]){

    // Closes the read file descriptor
    close(data_pipe[0]);

    dup2(data_pipe[1], STDOUT_FILENO);

    // confused as to why this is necessary
    close(data_pipe[1]);

    execlp("ls", "ls", "-1", NULL);
}
void executeSort(int data_pipe[]){

    // close the write file descriptor
    close(data_pipe[1]);

    dup2(data_pipe[0], STDIN_FILENO);
    close(data_pipe[0]);
    execlp("sort","sort", NULL);
}
+4
source share
1 answer

The reason is that you are casting more processes than you planned. When you do:

    childls_pid = fork();
    childSort_pid = fork();

fork() , , fork(). , :

parent
    childls
        childSort
    childSort

childls childls->childSort, childls_pid 0, case 0:, executeLs().

fork , . switch.

, FD FD, , , , , . , EOF .

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

// function declarations
void executeLs(int data_pipe[]);
void executeSort(int data_pipe[]);

int main(){

    int data_pipe[2]; // array storing the file descriptors
    int childls_pid; // ls child process
    int childSort_pid; // sort child process
    int rc; // return vaue of the pipe
    int child_status1;
    int child_status2;

    rc = pipe(data_pipe);
    if(rc == -1){
        perror("pipe");
        exit(1);
    }

    childls_pid = fork();

    // Ls Child process
    switch(childls_pid) {
    case -1:
        perror("fork childLs Error");
        exit(1);
    case 0:
        // inside of child process
        executeLs(data_pipe);
        exit(0);
    default:
        break;
    }

    childSort_pid = fork();

    // Sort child process
    switch(childSort_pid) {
    case -1:
        perror("fork childSort Error");
        exit(1);
    case  0:
        executeSort(data_pipe);
        exit(0);
    default:
        wait(&child_status2);
    }
    return 0;
}

void executeLs(int data_pipe[]){

    // Closes the read file descriptor
    close(data_pipe[0]);

    dup2(data_pipe[1], STDOUT_FILENO);

    // confused as to why this is necessary
    close(data_pipe[1]);

    execlp("ls", "ls", "-1", NULL);
}

void executeSort(int data_pipe[]){

    // close the write file descriptor
    close(data_pipe[1]);

    dup2(data_pipe[0], STDIN_FILENO);
    close(data_pipe[0]);
    execlp("sort","sort", NULL);
}
+2

Source: https://habr.com/ru/post/1616628/


All Articles