0

i am trying to implement a simple bash shell in C, which has to handle pipes and subshells. The main problem is that once I run the command in the subshell, how can I pass the output to the parent process to use the pipe?

the idea behind my program is that when it sees a parenthesis it calls the start function, which calls the function that executes pipes and execve and when it's done the parent picks up where it left off

for the pipe code i followed Implementation of multiple pipes in C, but now i don't know how to share information between two file descriptors.

EDIT:

code of pipe function


void open_pipe(int pipes[], int no_pipes)
{
    int i;
    
    i = -1;
    while (++i < no_pipes)
        pipe(pipes + i * 2);
}

void close_pipe(int pipes[], int no_pipes)
{   
    int j;

    j = 0;
    while (j < 2 * no_pipes)
        close(pipes[j++]);
}

void ft_exec_pipe(t_shell *shell, int nb_pipe)
{
    int     i;
    int     j;
    int     pipes[2 * nb_pipe];
    int     status;
    pid_t   pid;
    
    i = 0;
    j = nb_pipe + 1;
    open_pipe(pipes, nb_pipe);
    while (*(shell->token) && j-- > 0)
    {
        pid = fork();
        if (pid == 0)
        {
            if (*(shell->token + 1) != NULL)
                dup2(pipes[i + 1], STDOUT_FILENO);
            if (i != 0)
                dup2(pipes[i - 2], STDIN_FILENO);
            close_pipe(pipes, nb_pipe);
            if (ft_is_subshell(sstoken))
            {
                shell->fd = pipes[i + 1];
                ft_subshell(shell, sstoken);    
            }
            else
                ft_exec_cmd_fork(shell);
        }
        i += 2;
        shell->token++;
        if (j)
        {
            shell->last_operator = *(shell->operator);  
            shell->operator++;
        }
    }
    close_pipe(pipes, nb_pipe);
    while (i-- > 0)
        waitpid(pid, &status, 0);
        shell->exitstatus = WEXITSTATUS(status);
        shell->prev_exitstatus = shell->exitstatus;
}

the problem with this code is in reading from the file descriptor after pipe(), when the program executes commands in the subshell and the parent process filters the output with: wc, grep, head ... loop as if there are problems with input, instead with commands like ls, ps, echo ... work fine. I'm sure I don't handle the input well but I don't know how to fix.

federikowsky
  • 518
  • 2
  • 8
  • Possibly duplicate: https://stackoverflow.com/questions/4465959/python-errno-98-address-already-in-use – Lisa Jun 22 '22 at 18:07
  • To share information over a file descriptor, you use `read` and `write`! – William Pursell Oct 07 '22 at 13:10
  • why I have to use it? i can use pipe and dup2, they do all the work for me. The problem is how to use them – federikowsky Oct 07 '22 at 13:33
  • The question is unclear. If you want the parent to read the data from the pipe ( "pass the output to the parent process" seems to suggest that is what you want), then the parent will have to read the data from the pipe. If you simply want the output from the child to pass through to the original stdout of the parent (in which case you do not want to "pass the output to the parent process"), then the child should not be writing into the pipe at all. – William Pursell Oct 07 '22 at 14:17
  • the first case that's what interests me, the child have to write on pipe so the parent can read the output. – federikowsky Oct 07 '22 at 14:50
  • So, what's the question? If you want the parent to read the data, use `read`. – William Pursell Oct 07 '22 at 15:08
  • But if you are writing a shell, I suspect that in fact you do *not* want the parent to read the output. Instead, you want the parent to set up the child so that the child is just writing to the parent's original output stream. – William Pursell Oct 07 '22 at 15:09
  • Most likely, you'll have *two* children and have the first child write to the pipe and the second read from it. The parent needs to create the pipe before it forks the children and do the appropriate dup2/close/close in each child before exec (and just close/close in the parent itself) – Chris Dodd Oct 07 '22 at 16:58
  • ok to make it easier for everyone I'll post code of pipe. pls read it – federikowsky Oct 07 '22 at 19:08

0 Answers0