This project will let you discover in detail a UNIX mechanism that you already know
by using it in your program.
open, close, read, write, malloc, free, perror, strerror, access, dup,
dup2, execve, exit, fork, pipe, unlink, wait, waitpid, ft_printf
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.
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
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 |
|___________| |___________|
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
can check the path with X_OK to the command
returns a specific error code the program exited with if an error occoured
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.
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
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