├── manual.c ├── 1.1._proc_sys_kernel_pid_max ├── AUTHORS ├── 1.0.getppid.c ├── 2.0.av.c ├── 2.1.Read_Line.c ├── 3.execve.c ├── 4.fork.c ├── generate-authors.sh ├── 2.2.command_line_to_av.c ├── 4.1.fork.c ├── 5.wait.c ├── man_1_simple_shell ├── 6.fork+wait+execve.c └── README.md /manual.c: -------------------------------------------------------------------------------- 1 | hola 2 | -------------------------------------------------------------------------------- /1.1._proc_sys_kernel_pid_max: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat /proc/sys/kernel/pid_max 3 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This file lists all individuals having contributed content to the repository. 2 | # For how it is generated, see 'generate-authors.sh'. 3 | 4 | dalejoro 5 | jdrestre 6 | RicardoBarretoR 7 | -------------------------------------------------------------------------------- /1.0.getppid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - PID 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | pid_t my_pid; 12 | 13 | my_pid = getpid(); 14 | printf("hijo %u\n", my_pid); 15 | my_pid = getppid(); 16 | printf("padre %u\n", my_pid); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /2.0.av.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main-prototype 5 | *@ac:argument 1 6 | *@av:argument 2 7 | *Return: 0 8 | */ 9 | int main(int ac, char **av) 10 | { 11 | int i = 0; 12 | 13 | while (av[i]) 14 | { 15 | printf("%s ", av[i]); 16 | i++; 17 | } 18 | printf("\n"); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /2.1.Read_Line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main-prototype 5 | *Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char *buffer = malloc(1024); 10 | size_t len = 1024; 11 | 12 | while (1) 13 | { 14 | printf("$ "); 15 | getline(&buffer, &len, stdin); 16 | prinf("%s", buffer); 17 | } 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /3.execve.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - execve example 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *argv[] = {"/bin/ls", "-l", "/usr/", NULL}; 12 | 13 | printf("Before execve\n"); 14 | if (execve(argv[0], argv, NULL) == -1) 15 | { 16 | perror("Error:"); 17 | } 18 | printf("After execve\n"); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /4.fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - fork example 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | pid_t my_pid; 12 | pid_t pid; 13 | 14 | printf("Before fork\n"); 15 | pid = fork(); 16 | if (pid == -1) 17 | { 18 | perror("Error:"); 19 | return (1); 20 | } 21 | printf("After fork\n"); 22 | my_pid = getpid(); 23 | printf("My pid is %u\n", my_pid); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /generate-authors.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | cd "$(dirname "$(readlink -f "$BASH_SOURCE")")" 5 | 6 | # see also ".mailmap" for how email addresses and names are deduplicated 7 | 8 | { 9 | cat <<-'EOH' 10 | # This file lists all individuals having contributed content to the repository. 11 | # For how it is generated, see 'generate-authors.sh'. 12 | EOH 13 | echo 14 | git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf 15 | } > AUTHORS 16 | -------------------------------------------------------------------------------- /2.2.command_line_to_av.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main-prototype 6 | *Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char *buffer = malloc(1024); 11 | size_t len = 1024; 12 | char *token; 13 | 14 | while (1) 15 | { 16 | printf("$ "); 17 | getline(&buffer, &len, stdin); 18 | printf("buffer getline: %s", buffer); 19 | 20 | token = strtok(buffer, " "); 21 | while (token) 22 | { 23 | printf("buffer strtok: %s\n", token); 24 | token = strtok(NULL, " "); 25 | } 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /4.1.fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - fork example 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | pid_t my_pid; 12 | pid_t child_pid; 13 | 14 | child_pid = fork(); 15 | if (child_pid == -1) 16 | { 17 | perror("Error:"); 18 | return (1); 19 | } 20 | my_pid = getpid(); 21 | printf("My pid is %u\n", my_pid); 22 | if (child_pid == 0) 23 | { 24 | printf("(%u) Nooooooooo!\n", my_pid); 25 | } 26 | else 27 | { 28 | printf("(%u) %u, I am your father\n", my_pid, child_pid); 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /5.wait.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - fork & wait example 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | pid_t child_pid; 14 | int status; 15 | 16 | child_pid = fork(); 17 | if (child_pid == -1) 18 | { 19 | perror("Error:"); 20 | return (1); 21 | } 22 | if (child_pid == 0) 23 | { 24 | printf("Wait for me, wait for me\n"); 25 | sleep(3); 26 | } 27 | else 28 | { 29 | wait(&status); 30 | printf("Oh, it's all better now\n"); 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /man_1_simple_shell: -------------------------------------------------------------------------------- 1 | .TH man 1 "28 Nov 2019" "0.32" "our own simple_shell man page" 2 | .SH NAME 3 | .B our own simple_shell 4 | - We try to build our own simple shell. 5 | .SH SYNOPSIS 6 | .SH Descripción y definición de Shell 7 | .SH 8 | .SH 9 | .SH 10 | .SH 11 | .SH DESCRIPTION 12 | we use the following files to build our own simple shell: 13 | - FILE 1 14 | - FILE 2 15 | - ETC 16 | .SH RETURN VALUE 17 | Nuestra Shell debe retornar la ejecución en salida estándar del comando digitado por el usuario 18 | Si el comando no existe debe decir que "No existe" 19 | .SH 20 | .SH EXAMPLES TEST 21 | we compare with printf function original our own _printf function with next examples: 22 | .SH 23 | .SH Examples 24 | .SH 25 | .SH 26 | .SH 27 | .SH 28 | .SH Results 29 | .SH 30 | .SH 31 | .SH 32 | .SH 33 | .SH ISSUES 34 | We have a issue in check # 35 | .SH AUTHORS 36 | Ricardo Barreto, Diego Rojas and Juan David Restrepo Z -------------------------------------------------------------------------------- /6.fork+wait+execve.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - exercise: fork + wait + execve 8 | * 9 | * Return: Always 0 10 | */ 11 | int main(void) 12 | { 13 | pid_t my_pid; 14 | pid_t child_pid = 1; 15 | int i = 0; 16 | int status; 17 | char *argv[] = {"bin/ls", "-l", "tmp/", NULL}; 18 | 19 | my_pid = getpid(); 20 | while (i <= 4 && (child_pid != 0)) 21 | { 22 | child_pid = fork(); 23 | if (child_pid == -1) 24 | { 25 | printf("error"); 26 | return (1); 27 | } 28 | wait(&status); 29 | i++; 30 | } 31 | if (child_pid == 0) 32 | { 33 | printf("--------------------------------\n\n"); 34 | printf("ID HIJO: %u\n\nID PADRE: %u\n\n", getpid(), getppid()); 35 | printf("--------------------------------\n\n"); 36 | 37 | } 38 | else 39 | { 40 | printf("%u SOY EL PADRE Y MI ID ES: %u\n", my_pid, child_pid); 41 | } 42 | if (execve(argv[0], argv, NULL) == -1) 43 | { 44 | /*perror("ERROR:");*/ 45 | } 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ++ Everything you need to know to start coding your own shell (Low-level programming & Algorithm) 2 | 3 | ** Exercises 4 | - File 1.0.getppid.c (Write a program that prints the PID of the parent process. Run your program several times within the same shell. It should be the same. Does echo $$ print the same value? Why?) 5 | - File 1.1._proc_sys_kernel_pid_max - Write a shell script that prints the maximum value a process ID can be. 6 | - File 2.0.av.c - (Write a program that prints all the arguments, without using ac.) 7 | - File 2.1.Read_Line.c - (getline) Write a program that prints "$ ", wait for the user to enter a command, prints it on the next line. 8 | - File 2.2.command_line_to_av.c - (strtok) Write a function that splits a string and returns an array of each word of the string. 9 | - File 3.execve.c - Executing a program. The system call execve allows a process to execute another program (man 2 execve). Note that this system call does load the new program into the current process memory in place of the previous program: on success execve does not return to continue the rest of the previous program. 10 | - File 4.fork.c - The system call fork (man 2 fork) creates a new child process, almost identical to the parent (the process that calls fork). Once fork successfully returns, two processes continue to run the same program, but with different stacks, datas and heaps. 11 | - File 4.1.fork.c - Using the return value of fork, it is possible to know if the current process is the father or the child: fork will return 0 to the child, and the PID of the child to the father. 12 | - File 5.wait.c - The wait system call (man 2 wait) suspends execution of the calling process until one of its children terminates. 13 | - File 6.fork+wait+execve.c - Exercise: fork + wait + execve 14 | Write a program that executes the command ls -l /tmp in 5 different child processes. Each child should be created by the same process (the father). Wait for a child to exit before creating a new child. 15 | - Files generate-authors.sh and AUTHORS. 16 | . File man_1_simple_shell - Manual for our simple shell --------------------------------------------------------------------------------