user3624403
user3624403

Reputation: 1

Making a basic shell in C and have trouble regarding pipes / forks

First I'd like to ask why are forks needed in pipes? I'm fairly new to this, but to me I don't yet see why I can't just run one process to the left of the | then have the next part go after using the result as an input. I know forks are used, but I'm not getting why or where I would need them.

Thanks so much for answering this question even though it's probably a stupid question.

Upvotes: 0

Views: 72

Answers (2)

Slava
Slava

Reputation: 44258

fork() or variants is not only necessary to execute programs that connected by pipes, but to execute any program. The reason for that - exec..() functions family replace current process with one that loaded by exec. So for your shell program to continue after child program terminates you have to call fork()

For pipes, pipe buffer is relatively small, but amount of data that programs communicate through pipe could be large. For that to work efficiently both programs have to run in parallel.

Upvotes: 1

Read Advanced Linux Programming & intro(2); it -the ALP book- has several chapters explaining this. And perhaps study the source code of some free software shell. Use also strace(1)

A pipe(7) has a certain (small) capacity PIPE_BUF (a few kilobytes). When that pipe is filled, the writing process is blocked. When the reading process has read everything, the pipe becomes empty, and the reading process is blocked. So the writing process gets a chance to run and write inside it.

So you need both processes to run simultaneously (and they could exchange a huge amount of data - eg gigabytes in a few seconds). And  fork is the only way to make new processes.

Upvotes: 1

Related Questions