The execution module is the heart of Minishell, responsible for coordinating process creation, data flow between commands, and handling external binaries.
When a command is submitted, the execution engine performs the following:
- Redirection Handling: It processes redirections (
<,>,<<,>>) first to set up the appropriate file descriptors for input and output. - Built-in Check: It checks if the command is a built-in. If so, it executes it directly in the current process.
- External Command Execution: If the command is not a built-in, it forks a new process and uses
execveto search for and execute the binary using thePATHenvironment variable.
Pipelines (|) allow the output of one command to be used as the input for another.
- Minishell creates a
pipebetween each command in a pipeline. - The
stdoutof the preceding command is redirected to the write-end of the pipe. - The
stdinof the succeeding command is redirected to the read-end of the pipe. - All processes in a pipeline are forked and executed concurrently.
- Input Redirection (
<): Reading input from a file instead ofstdin. - Output Redirection (
>): Directing output to a file instead ofstdout(overwrites). - Append Redirection (
>>): Directing output to a file instead ofstdout(appends). - Heredoc (
<<): Providing input directly in the shell until a specified delimiter is reached.
Minishell carefully manages child processes:
- It waits for all commands in a pipeline to finish using
waitpid. - It captures and updates the exit status (accessible via
$?). - It properly closes file descriptors after use to prevent leaks and exhausted resources.