This program implements a simple version of a command pipeline using fork and pipe system calls. The program creates two child processes to execute two different commands and redirects their input and output through a pipe. The main function:
git clone git@github.com:JohannaGcd/42_pipex.git && cd 42_pipex && make
./pipex infile "ls -l" "wc -l" outfile
Should behave as:
< infile ls -l | wc -l > outfile
-
Process Management:
- Forking Processes: Using
fork()to create child processes and managing process IDs. - Waiting for Processes: Using
waitpid()to wait for child processes to finish and get their exit statuses.
- Forking Processes: Using
-
Inter-Process Communication (IPC):
- Pipes: Using
pipe()to create a pipe for communication between processes anddup2()to redirect standard input and output.
- Pipes: Using
-
File Descriptors and Redirection:
- File Operations: Using
open()to open files and handle file descriptors. - Redirection: Redirecting input and output streams using
dup2().
- File Operations: Using
-
Error Handling:
- Error Reporting: Using
perror()to report errors and handle them gracefully.
- Error Reporting: Using
-
Command Execution:
- Executing Commands: Using
execve()to execute commands within child processes. - Retrieving Command Paths: Determining executable paths using environment variables (
PATH) and handling command execution errors.
- Executing Commands: Using
-
Memory Management:
- Dynamic Memory: Allocating and freeing memory for command arguments and paths using functions like
ft_split()andfree().
- Dynamic Memory: Allocating and freeing memory for command arguments and paths using functions like
main(): Initializes pipes and processes, forks two child processes, and waits for their completion.execute_child(): Prepares and executes commands in child processes, handling input/output redirection and command execution.open_file(): Opens input or output files based on the child process.redirect_fds(): Redirects file descriptors for input and output.close_pipes(): Closes unused pipe file descriptors.set_in_and_out(): Sets up input and output redirection for child processes.retrieve_cmds(): Retrieves the full path of commands to be executed.retrieve_env_path(): Retrieves the PATH environment variable.get_cmd(): Splits a command string into arguments.get_env_path(): Retrieves the value of the PATH environment variable.get_cmd_path(): Constructs the full path for an executable command.