This is an elementary implementation of a shell for my personal learning. Feel free to come along for the journey!
-
What are
stdin,stdout,stderr? standard input, standard output and standard error respectively. These are usually called I/O Streams, but are essentially files (at least, in the Unix-sense of the word) that the program reads or writes from. They exist so that the program can simply read or write bytes from a file wihout having to worrying if its reading from a keyboard or writing to a shell, etc. -
What does
mallocandreallocdo? Why is this different from initializing a variable? From man7:
void *malloc(size_t size);
void *realloc(void *_Nullable p, size_t size);
void free(void *_Nullable p);malloc allocates size bytes and returns a pointer to the allocated memory. The memory is allocated from the heap in the process context, aka the dynamic memory, during runtime. The lifetime of this newly allocated memory is not fixed and must be free-ed explicitly by the programmer at runtime.
In comparison, local variables, such as variables initialized within a function, have memory automatically allocated from the stack in the process context. However, if these variables are declared static or are initialized in the global scope, then they will reside in the static memory portion of the process context. The static memory is allocated when the program starts and shares the program lifetime. On the other hand, the stack storage is automatic, being created and destroyed on function calls.
realloc returns a pointer to a newly allocated memory with the new size. The contents of the new object will be the same as the old object until min(size_old, size_new).
- What does
forkandexecdo?
pid_t fork(void);
int execvp(const char *file, char *const argv[]);fork creates a clone of the calling process. The fork function returns the unique process ID of the child process to the parent process but returns 0 to the child process, assuming the fork was successful. Then, the child process is able to use the exec function to replace its current process image with a new process image. On the other hand, the parent process uses wait or in this case, waitpid to wait for the specific child process to return a status code. When the child process finishes its execution or terminates, the parent process receives the status and continues its own execution, in this case, waiting for the next command from the user.
- history
- print directory