-1

I have some questions about the whole concept of Shells in the C language. For this semester we have to learn C but the teaching skills of the people in my uni aren't that great. So for the first assignment we have to create some shells. Don't get me wrong here, I love programming, I'm NOT asking you to complete my assignment, I just would like some help getting started because I'm kind of lost here.

The first thing I have to do is create a Shell in C language (Different from linux shell scripts I think) and run it on a Ubuntu virtual machine. It's in the very early stages of C programming and we are being taught about the parent-child processes. I quote "This shell, will accept simple single commands. Specifically, it

a) will read the title of a program from the terminal,

b) will create a new process to run the program that just got read and

c) will wait for the process to end.

Once the process is over it will read and execute the next command. For example, the shell will be able to run commands like "ls" ". This confuses me, as I don't understand what a shell is and what form it actually has, and reading about on the internet doesn't really help me. So could you please show me how this should look like? I don't understand how it will run or how it will execute commands like ls. We have been instructed to use fork and strtok for the commands.

Thanks in advance!

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
George M
  • 23
  • 2
  • A shell is basically a command line interpreter. – Jabberwocky Apr 22 '15 at 10:19
  • Yes, I realise that but it doesn't help me implement the code. Is it written,compiled and ran like a normal program? How do I make it run "ls"-like commands? I haven't understood the basic concepts of a shell. – George M Apr 22 '15 at 10:22
  • It's a normal program. Basically it reads a line int o a buffer (e.g. with the `fgets` function) and then it will create a process corresponding to the content of the line. Reading [this SO topic](http://stackoverflow.com/questions/5883462/linux-createprocess) should give you some clues. – Jabberwocky Apr 22 '15 at 10:25
  • 1
    A shell is a program that can read commands. Then, it executes these commands. `ls` is one of those commands. So basically, your task is to build a program that works like the a basic linux command line. This is written, compiled and run like a normal C-program. But when it runs, will ask the user for input and "execute" this input to do what the user tells it to do – Slizzered Apr 22 '15 at 10:26
  • Thanks, that was helpful! For the "ls" example, would I basically give the ls or any other command as an argument when running the program, then find this command with strtok() and exec() it? – George M Apr 22 '15 at 10:43
  • Yes, in case of `ls` that will work. However, in a later lecture they might tell you to implement a mechanism similar to the pipe operator in `bash`, so you can forward the output of a command (e.g. `ls`) to another command. So the user could enter something like `ls | grep "..."` into your shell. – Slizzered Apr 22 '15 at 10:58

1 Answers1

0

Shell is a C program. So yes it is written, compiled and run like a standard c program.

Also it is important to note that commands like ls , wc are also programs. So when we say "run command ls", we actually mean " run the program whose name is ls".

Now this is the fun part: There is a function called exec( ) which helps a program run another program. ( there is an entire family of exec functions)

What shell does is this:

  1. Shell uses fork ( ) syscall to create a child process.
  2. Once child process is created, then, shell uses exec ( ) function to make the child process run the desired command (desired program) ( ex: ls, wc).
  3. Shell uses wait ( ) syscalls for the child ( and hence the command ) to complete.

What I described above is a very brief description. And each of the above 3 steps may have one or more sub-steps.

You can learn about fork( ) and exec ( ) and wait ( ) using the manual pages provided in linux.

Please type below command in your terminal to learn about these:

man 2 fork
man 3 exec
man 2 wait

These may clear some of your questions..

sps
  • 2,720
  • 2
  • 19
  • 38
  • Thank you very much, this has really helped me understand! One last question, the shell that I have to make is supposed to execute commands like ls, which is located in bin/ls iirc. So if it had to execute , say, five of these supposedly unknown to me commands(because the commands that will be run is up to the user), how will I pinpoint and execute them? Through if statements? I guess that this is impractical, and surely bad practice, as the commands are many for me to check each one. – George M Apr 22 '15 at 11:30