Learning Goal: I’m working on a c programming question and need an explanation and answer to help me learn.
1. In an infinite loop, read the standard input
2. Extract strings using whitespaces
3. For each string, check if you have internal or external commands
4. In case of internal commands, execute the corresponding function
4. In case of external commands, fork the shell process, replace the existing code segment by the code of the external program & wait for the end of the child process
Possible steps to prepare the processes before forking: connect SIGTSTP or SIGINT to a user-supplied handler before entrering onfinite loop, open pipe, redirect i/o with dup2…
Other consideration: to make easier your work, you can assume that the entry for |, >, >>, echo.txt is forbidden for example). Only exception is | can be followed by ./
./ Looks for an external command in the working directory only.
For other external commands, you can use PATH directory or working directory to find the program.in the case of reading from a file, the shell process must not die before the end of the child processes. Moreover, n gives the signal to bash to execute a command and wait the end of this command before executing the next one (as when you read from the standard input, pressing enter writes n and asks bash to execute the command before waiting for the next one at the end of the current command).
So in all cases, you have an infinite loop, except that reading EOF sends a signal to quit the shell (while from the standard input, CTR-C/CTR-Z must send the signal to quit the shell).
To use multiple pipes, you must detect the number of pipes, it will allow you to get the number of children to create. Open the pipes, fork and then in each child, find a way to close the unused file descriptors. Remind that you shell process remains executed as it is the parent process (eg 3 pipes leads to 5 processes).