Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

C program that replicates the shell's pipe functionality. It reads input from a file, executes two commands and directs the output to another file.

NotificationsYou must be signed in to change notification settings

waltergcc/42-pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pipex is a small command-line program that emulates the functionality of the shell's pipe feature. It reads input from a file, runs two commands as separate child processes, and then sends the output of the first command as input to the second command. The output is then written to another file. Pipex is implemented using the C programming language and various system calls, such as fork, pipe, dup2, and execve.

Concepts

  1. Subprocesses: Let's work with different processes. One for the first command to be executed, and one to the second.For this, we will use thefork() function that creates a child process.
  2. Pipes: To connect the processes, let's use pipes. Pipes are bufffers that allow communication between processes. To create a pipe, we use thepipe() function.
  3. Redirection: To redirect the input and output of the processes, we will use thedup2() function.It receives two file descriptors as a parameter, and causes the file descriptor received as a second parameter to be a copy of the file descriptor received as the first parameter. Thus, we can redirect the input and exit of the processes to the pipes.
  4. Execution of commands: To execute the commands, we will use theexecve() function. It receives as parameters the path of the executable file, a string array with the command arguments. With this, in the command being called, the process that called it is replaced by the command process.

How it works

  1. The program receives as parameters the name of theinput file, thefirst command, thesecond command, and the name of theoutput file.
  2. The program creates a pipe (read-endfd[3] | write-endfd[4]).
  3. The program creates a child process.
    • It open the input filefd[5].
    • It redirects the stdoutfd[1] to the pipefd[4].
    • Then redirects the input filefd[5] to the stdinfd[0].
    • Close the read-end of the pipefd[3].
    • Execute the first command.
    • The output of the first command is written to the pipe because the previous redirection.
    • The child process exits.
  4. The program wait for the child process to finish.
  5. The program call the parent process.
    • It open the output filefd[5].
    • It redirects the stdinfd[0] to the pipefd[3], that is, the output buffer of the first command.
    • Then redirects the output filefd[5] to the stdoutfd[1].
    • Close the write-end of the pipefd[4].
    • Execute the second command.
    • The output of the second command is written to the output because the previous redirection.
    • The parent process exits.
  6. If you don't have any errors, the program ends successfully.

How to use

To compile this program, run the following commands:

make

Then, to run the program, run the following command:

./pipex file1 cmd1 cmd2 file2

Memory leaks test

For test memory leaks, run:

valgrind --leak-check=full --trace-children=yes ./pipex file1 cmd1 cmd2 file2

Grade: 100 / 100

Used tests

About

C program that replicates the shell's pipe functionality. It reads input from a file, executes two commands and directs the output to another file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp