Discussion Section for February 20th, 2014: Mini-Shell and Fork


In Discussion Section

Implement a mini-shell that does the following:

You can assume everything will be lower case (eg: exit will always be exactly exit), all commands will not have any extra whitespace, and all arguments to a command will be seperated by exactly one space.

Compiling and Running

To compile and run your shell, run the following commands from a Terminal on a Linux machine:

$ make
$ ./mshell

Example

Consider the following example run of your shell:

$(pid=16341): ls
index.html Makefile mshell mshell.c
$(pid=16341): pwd
/home/chen208/cs241_TA/ds4
$(pid=16341): cd ..
$(pid=16341): ls
ds4 ds4.zip mp0-rel mp1 mp1_sol mp1_sol.tar mp1.tar mp1.zip
$(pid=16341): pwd
/home/chen208/cs241_TA
$(pid=16341): echo hello world CS241
hello world CS241
$(pid=16341): exit

Extra: Fork_fan and Fork_chain

Your task is to create a fork_fan and a fork_chain using the fork() call, this is to give you a better understanding of how fork works.

Fork_fan

Suppose you have created a fork fan of size 5, this means that your program should have one parent process, and this parent process should create four child processes. In order to check the correctness, you can have your child process sleep for some short time(10 seconds) while you are checking. After 10 seconds, your program should exit gracefully. You can check that your program created a fan correctly by using ps while the child is sleeping. See the following example:

ps -ef|grep mshell
chen208 11709 12029 0 22:18 pts/10 00:00:00 ./mshell
chen208 11710 11709 0 22:18 pts/10 00:00:00 ./mshell
chen208 11711 11709 0 22:18 pts/10 00:00:00 ./mshell
chen208 11712 11709 0 22:18 pts/10 00:00:00 ./mshell
chen208 11713 11709 0 22:18 pts/10 00:00:00 ./mshell
chen208 11718 2042 0 22:18 pts/32 00:00:00 grep mshell

The second and third field represents each process's' pid and its parent's pid. In the above example, process 11709 is the parent process, and processes 11710, 11711, 11712, 11713 are its children.

Fork_chain

Suppose you have created a fork chain of size 5, this means that your program should have five processes chained together. In order to check the correctness, you can have your child process sleep for some short time(10 seconds) while you are checking. After 10 seconds, your program should exit gracefully. You can check that your program created a fan correctly by using ps while the child is sleeping. See the following example:

ps -ef|grep mshell
chen208 15499 12029 0 22:28 pts/10 00:00:00 ./mshell
chen208 15500 15499 0 22:28 pts/10 00:00:00 ./mshell
chen208 15501 15500 0 22:28 pts/10 00:00:00 ./mshell
chen208 15502 15501 0 22:28 pts/10 00:00:00 ./mshell
chen208 15503 15502 0 22:28 pts/10 00:00:00 ./mshell
chen208 15506 2042 0 22:28 pts/32 00:00:00 grep mshell

The second and third field represents each process's pid and its parent's pid. In the above example, process 15499 is the root of the chain, it is the parent of process 15500. Process 15500 is the parent of process 15501. Process 15501 is the parent of process 15502. Process 15502 is the parent of process 15503. Thus forming a chain.