 Hi students, in this video we will see how to write a very simple Linux shell in C programming language. So here I have written this my underscore shell dot C program. Let's open it in visual code. So this is the my underscore shell dot C and Let's go over the code line by line. So here is the main program. First of all, what do we want our my shell program to do? We want our my shell program to first display some prompt to the user and then take command from the user as an input and then it should execute that command and Again show another prompt to the user to take in another command and repeat so we have this while loop in the code and What it does is first it prints a prompt. So here it is dollar followed by a space Then it takes in input So it will take all the characters up to the new line character and store it in the line variable and then We are using a get care so that it waits for the user to enter this new line character We want this line variable to have that string which ends with a new line character So we just add this new line character at the end of the line string and then we tokenize this line And what does this tokenize do this tokenize will separate this line string into different words So let's say user enters leaf 5 then tokenize will separate of this leaf and 5 as separate strings in a string array so tokens is a care star star and Let's see this tokenize implementation so it takes this care star as an input and it goes over every character of the line and It checks if the character is a white space character if it is not a white space character then it adds that character to the token and If it is a white space character, then whatever token it has stored till now it Stores it in the token number position in the tokens array and Increases the token number by 1 and then resets the token index to 0 so that one can Start taking the new token again so this is how it separates out word-by-words and creates this tokens array and Finally, it returns this token array Let's continue Now here is where the shell implementation begins if token 0 is null Which means that user has directly pressed enter without entering anything then we want this program to continue and Just again display a new prompt to the user So if token 0 is not null that means user has entered some command So we first use the fork system call to create a new child process So what does the fork do fork will create a new child process? This new child process will have the exact same memory image as the parent child process But what will be different is the return value of fork in parent and child in parent The fork will return the PID of the child as return value and in child fork will return 0 as return value So here we have different conditions based on FC So if FC is negative which means that fork has failed due to some reason then we just print in the STD error Unable to create child process. Otherwise if FC is 0, which means child will execute this part of code We are using this exec vp system call Now what does this exec vp system call do exec vp system call is just a variant of the exec system call it takes in an executable name as the first argument and All the arguments that are to be supplied to that executable as second argument in an array form and Then it searches for this executable in the Linux system And if it finds the executable then it will reinitialize the memory image of child process with the code of this executable And in case this exec vp fails and it is unable to find this Executable in the Linux system then the control will reach here and the child will print command execution failed and then it will Exit with return value of one and What does parent do? parent uses the wait system call to wait for the child to exit and once the child exits Parent reaps the child and clears all the memory which was used by the child So here we just free the memory which was allocated to their tokens array and This is where this loop wordy ends and then it repeats So it first shows the prompt it takes in some input from the user it creates a new child process and In the child it uses this exec vp system call to replace child's code with the executables code and Then the child will execute that executable and if it fails due to some reason then child will print command execution failed and parent simply waits for the child to exit and then it reaps the child and It repeats again and again. So let's compile this my underscore shell dot C and Run the arrow doubt So you can see that it is showing us the prompt and let's enter some command. Let's say echo hello world So here it printed hello world So what happened here is parent forked a new child and that exact vp system call replace the child's code with the code given in the executable of echo command and then the child executed that code to print out hello world Finally the child exit it and the parent who was waiting for child to exit Reaps the child and then it again shows a new prompt so that we can enter other commands So let's run a few more commands. Alice. So here it shows us the list of all the files Let's see sleep 3 so it will sleep for 3 seconds and then again show a new prompt Let's try to execute CD workspace It says command execution failed and why is that so because it couldn't find an executable for CD because CD is not implemented as an executable in Linux system rather the shell program has to Implement the CD itself. So what we can do is we can check if token 0 is CD And if that is so then we can use the csdir system call to change the directory to implement the CD command And we can exit this shell using control plus C. So that's it for this video. Thanks and have a nice day