GithubHelp home page GithubHelp logo

pipex's Introduction

📖 PIPEX

This project will let you discover in detail a UNIX mechanism that you already know by using it in your program.

GitHub code size in bytes Code language count GitHub top language GitHub last commit

Allowed functions

open, close, read, write, malloc, free, perror, strerror, access, dup,

dup2, execve, exit, fork, pipe, unlink, wait, waitpid, ft_printf

env

When used in a pipeline, env is used to pass specific environment variables to a command or process in the pipeline, allowing it to access those values and use them in its execution.

pipe()

A pipe is a system call that creates a unidirectional communication link between two process

system call creates a pair of file descriptors, a read-end and a write-end, that are connected to each other so that data written to the write-end of the pipe is read from the read-end of the pipe.

First process output on the stdout becomes the second process input on stdin

First process -> infile, second process -> outfile

Pipeline

        |---------|
        | PIPE(); |
        |_________|
         /       \
        /         \
      fd0         fd1
        |---------|
        | FORK(); |
        |_________|
          /     \
         /       \
        /         \
|--------|       |-------|
| PARENT |       | CHILD |
|________|       |_______|
  /    \           /    \
fd0    fd1        fd0   fd1

fork()

It's creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process).

After a new child process is created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.

Negative Value: creation of a child process was unsuccessful.

Zero: Returned to the newly created child process.

Positive value: Returned to parent or caller. The value contains process ID of newly created child process.

If you call fork() once:

|-----------|
|  PARENT   |
|___________|
	|
	|
|-----------|
|  Child_1  |
|___________|

If you call fork() twice:

|-----------|
|  PARENT   |___________
|___________|           |
    |                   |
    |                   |
|-----------|       |-----------|
|  Child_1  |       |  Child_2  |
|___________|       |___________|
	|
	|
|-----------|
|  Child_3  |       
|___________|

If you call fork() 3 times:

|-----------|       |-----------|
|  Child_4  |<------|  PARENT   |___________
|___________|       |___________|           |
                    	|                   |
                    	|                   |
|-----------|       |-----------|       |-----------|       |-----------|
|  Child_5  |<------|  Child_1  |       |  Child_2  |------>|  Child_6  |
|___________|       |___________|       |___________|       |___________|
                    	|
                    	|
                    |-----------|
                    |  Child_3  |
                    |___________|
                    	|
                    	|
                    |-----------|
                    |  Child_7  |
                    |___________|

If you call fork() 4 times:

                                        |-----------|
                                        |  Child_8  |
                                        |___________|
                                               |
                                               |
|-----------|       |-----------|       |-----------|        |-----------|       |-----------|
|  Child_12 |<------|  Child_4  |<------|  PARENT   |------->|  Child_2  |------>|  Child_6  |
|___________|       |___________|       |___________|        |___________|       |___________|
                                                |                      |                  |
                                                |                      |                  |
|-----------|       |-----------|       |-----------|          |-----------|       |-----------|
|  Child_13 |<------|  Child_5  |<------|  Child_1  |_____     |  Child_10 |       |  Child_14 |
|___________|       |___________|       |___________|     |    |___________|       |___________|
                                          |               |
                                          |          |-----------|
            |-----------|       |-----------|        |  Child_9  |
            |  Child_11 |<------|  Child_3  |        |___________|
            |___________|       |___________|
                                        |
                                        |
                                  |-----------|      |-----------|
                                  |  Child_7  |----->|  Child_15 |
                                  |___________|      |___________|

dup2()

is a system call in the C programming language that is used to duplicate a file descriptor. The call takes two arguments: an existing file descriptor (the original) and a new file descriptor number (the copy).

The new file descriptor is a copy of the original file descriptor, and it shares the same properties and underlying file table entry. Any changes made to the new file descriptor are reflected in the original file descriptor, and vice versa.

If it was open previously, it's closed before being reused and it is running automatically

Trying to use dup() and close() instead, could cause problems like, reuse of the fd between 2 proces

access()

can check the path with X_OK to the command

sterror()

returns a specific error code the program exited with if an error occoured

execve

The call takes three arguments: the pathname of the program to be executed, an array of arguments for the program, and an array of environment variables for the program.

The execve() function replaces the current process image with the new process image, so that the new program runs in the same process as the calling program.

It is used when you want to run a program in place of the current process, without creating a new process. This is useful when you want to start a new program from a C program, or when you want to replace the current shell with a different shell.

wait() & waitpid()

Both can be used for waiting child processes and with waitpid you can wait for a specific one and can use -1 to wait untill all process finsh

Error handling

Wrong amount of args

Empty arguments or with only spaces in the argument

Invalid infile or outfile

No cat if the first command invalid

Invalid commands

Accepting commands with path as well, while checking if it's in /bin or /usr/bin

Wrong path check for the previous point

Correct returns as permission denied or no such file/folder etc.

Dircet path check

check for exit command

fork error

if first arg invalid and second is cat

checking for script files

commands not depending on the prev command, after an invalid command should work in bonus

Bonus

heredoc

handling multiple commands

🛠️Usage🛠️

./pipex infile "cmd" "cmd1" outfile
./pipex_bonus infile "cmd" "cmd1" "cmd2" "cmd3" "cmd4" outfile
./pipex_bonus here_doc stop "cmd" "cmd1" outfile

pipex's People

Contributors

zstenger93 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.