简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM