syscalls: yes i work on an 80 line dumb terminal

This commit is contained in:
John 2022-02-21 19:40:52 -06:00
parent b304263af0
commit c57170a241

View File

@ -7,34 +7,39 @@
#include <unistd.h> // fork(), getpid(), getppid(), execl(), exit() #include <unistd.h> // fork(), getpid(), getppid(), execl(), exit()
#include <stdlib.h> // EXIT_SUCCESS #include <stdlib.h> // EXIT_SUCCESS
#include <stdio.h> // printf() #include <stdio.h> // printf()
// tabs are 3 spaces, because I find it more aesthetically pleasing than x=[1-8]|x!=3 spaces // indents are 3 spaces, because I find it aesthetically pleasing.
int main(int argc, char **argv) { int main(int argc, char **argv) {
// First we fork, and assign the child's pid to a variable // First we fork, and assign the child's pid to a variable
int child = fork(); int child = fork(); //! fork()
// If we have a child, then we're the parent, and must wait for the child to execute /* If we have a child, then we're the parent,
and must wait for the child to execute */
if (child) { if (child) {
// We get our own pid // We get our own pid
int pid = getpid(); int pid = getpid(); //! getpid()
printf("[PARENT]: pid = %d, child = %d\n", pid, child); printf("[PARENT]: pid = %d, child = %d\n", pid, child);
// Wait for the child to exit, and acquire its status // Wait for the child to exit, and acquire its status
int status = 0; int status = 0;
int exited_child = wait(&status); int exited_child = wait(&status); //! wait(...)
printf("[PARENT]: child %d exited with status %d.\n", exited_child, status); printf("[PARENT]: child %d exited. Status: %d.\n", exited_child, status);
// exit // exit
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS); //! exit(...)
} }
// If we have no child, we are the child, and are assigned the task of executing // If we have no child, we are the child, and must execute
else { else {
// Get our pid, get parent's pid // Get our pid, get parent's pid
int pid = getpid(), parent_pid = getppid(); int pid = getpid(), parent = getppid(); //! get[p]pid()
// Print that information // Print that information
printf("[CHILD ]: pid = %d, parent_pid = %d\n", pid, parent_pid); printf("[CHILD ]: pid = %d, parent_pid = %d\n", pid, parent);
// Hand control over to ls, with the arguments "-la" /* Prepare to hand over control over to ls,
with the arguments "-la" */
char *command = "/bin/ls", *argv1 = "-la"; char *command = "/bin/ls", *argv1 = "-la";
printf("[CHILD ]: execl(%s, %s, NULL);\n", command, argv1); printf("[CHILD ]: execl(%s, %s, NULL);\n", command, argv1);
// execl takes the command, the arguments, and a null-pointer terminator argument /* execl takes the command, the arguments,
// argv[0] is the path to the command, argv[1] is the first argument and a null-pointer terminator argument.
// personally I prefer execv()/execve() for their relative ease of use argv[0] is the path to the command
execl(command, command, argv1, (char*)NULL); argv[1] is the first argument
personally I prefer execv()/execve(),
for their relative ease of use */
execl(command, command, argv1, (char*)NULL); //! execl(...)
} }
} }