I need to code a simplified version of bash for a school project, and I'm struggling to recreate the behavior that bash has when inputted "cat | ls"
What happens is that the terminal displays the output for ls, and then lets you enter one line.
I think this is because ls doesn't need to read from cat, and therefore it doesn't wait for it to be finished, so as soon as cat reads one line it tries writing it into ls's input, and since that it is closed, it fails and exits.
I tried emulating this by creating a pipe, forking my main process, duping the write end, executing cat, forking again, duping the read end, executing ls, then waiting for cat, then waiting for ls.
Unfortunately this doesn't seem to work, as cat goes on until i terminate it with a signal, or it reaches EOF.
How does bash handle this to get that result?
What should I do?