Explain what the return value of fork-shell programming, Programming Languages

Assignment Help:

Simple Shell

In this LAB, you will explore and extend a simple Unix shell interpreter. In doing so, you will learn the basics of system calls for creating and managing processes.
STEP 1: Compile the shell
chmod +x b.sh
make
make test # Use in Step 5 to test your changes to the Program
./shell

STEP 2: Try using the shell

Note: You need to specify the absolute paths of commands. Some commands to try:
/bin/ls
/bin/ls ..
cd /
/bin/pwd
/bin/bash
exit
./shell (Note: You need to be in the smp1 directory.)
./shell& (Note: You need to be in the smp1 directory.)
./b.sh (Note: You need to be in the smp1 directory.)
/bin/kill -s KILL nnnn (Where nnnn is a process ID.)
"./" means the current directory

STEP 3: Study the implementation of the shell

In preparation for the questions in Step 4, please explore the source code for the shell contained in 'shell.c'. You needn't understand every detail of the implementation, but try to familiarize yourself with the structure of the code, what it's doing, and the various library functions involved. Please use the 'man' command or Internet to browse the manual pages describing functions with which you are unfamiliar.
STEP 4: Questions
1. Why is it necessary to implement a change directory 'cd' command in the shell? Could it be implemented by an external program instead?
2. Explain how our sample shell implements the change directory command.
3. What would happen if this program did not use the fork function, but just used execv directly? (Try it!)

Try temporarily changing the code 'pid_from_fork = fork();'
to 'pid_from_fork = 0;'
4. Explain what the return value of fork() means and how this program uses it.
5. What would happen if fork() were called prior to chdir(), and chdir() invoked within the forked child process? (Try it!)

Try temporarily changing the code for 'cd' to use fork:
if (fork() == 0) {
if (chdir(exec_argv[1]))
/* Error: change directory failed */
fprintf(stderr, "cd: failed to chdir %s\n", exec_argv[1]);
exit(EXIT_SUCCESS);
}
6. Can you run multiple versions of ./b.sh in the background? What happens to their output?
7. Can you execute a second instance of our shell from within our shell program (use './shell')? Which shell receives your input?
8. What happens if you type CTRL-C while the countdown script ./b.sh is running? What if ./b.sh is running in the background?
9. Can a shell kill itself? Can a shell within a shell kill the parent shell?

./shell
./shell
/bin/kill -s KILL NNN (Where NNN is the parent's PID.)
10. What happens to background processes when you exit from the shell? Do they continue to run? Can you see them with the 'ps' command?

./shell
./b.sh&
exit
ps

STEP 5: Modify the Program

Please make the following modifications to the given file shell.c. As in previous lab, In this assignment there are some built-in test cases, which are described along with the feature requests below.
In addition to running the tests as listed individually, you can run "make test" to attempt all tests on your modified code.
1. Modify this Program so that you can use 'ls' instead of '/bin/ls' (i.e. the shell searches the path for the command to execute.)

Test: ./shell -test path
2. Modify this Program so that the command prompt includes a counter that increments for each command executed (starting with 1). Your program should use the following prompt format:

"Shell(pid=%1)%2> " %1=process pid %2=counter
(You will need to change this into a correct printf format)
Do not increment the counter if no command is supplied to execute.
Test: ./shell -test counter
3. Modify this Program so that '!NN' re-executes the n'th command entered. You can assume that NN will only be tested with values 1 through 9, no more than 9 values will be entered.

Shell(...)1> ls
Shell(...)2> !1 # re-executes ls
Shell(...)3> !2 # re-executes ls
Shell(...)4> !4 # prints "Not valid" to stderr
Test: ./shell -test rerun
4. Modify the Program so that it uses waitpid instead of wait.
5. Create a new builtin command 'sub' that forks the program to create a new subshell. The parent shell should run the imtheparent( ) function just as if we were running an external command (like 'ls').

./shell
Shell(.n1..)1> sub
Shell(.n2..)1> exit # Exits sub shell
Shell(.n1..)1> exit # Exits back to 'real' shell
6. Create a new global variable to prevent a subshell from invoking a subshell invoking a subshell (i.e., more than 3 levels deep):

./shell
Shell(.n1..)1> sub
Shell(.n2..)1> sub
Shell(.n3..)1> sub # prints "Too deep!" to stderr
Test: ./shell -test sub


Related Discussions:- Explain what the return value of fork-shell programming

Write a function to draw backbone of the protein, Write a function drawCA(a...

Write a function drawCA(aname, coords) that uses the plot3 command to draw the C α backbone of the protein. When calling this function, you should pass in the aname and coords var

Write use case description for the process, The following is the process an...

The following is the process an account holder of eBroker, a discount online brokerage firm, uses to sell stocks.  The user first logs in by entering the username and password.

Shell script to print count & sum of digits of given number, Normal 0 ...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Develop a context diagram and use case diagram, Develop (i) a context diagr...

Develop (i) a context diagram and (ii) a use case diagram to model the following functionalities of NetFlix. Netflix is an online video/DVD renting company. A customer desiring

Real-time driver model , AMCC PowerPC 440 EPx Sequoia Evaluation Kit, Embed...

AMCC PowerPC 440 EPx Sequoia Evaluation Kit, Embedded Linux Development Kit (ELDK), and Xenomai A. Description of the Project The project's objective is to design a set of

Define a prolog predicate that asserts list, Define a Prolog predicate flat...

Define a Prolog predicate flatten(List, FlattenedList)  that asserts List  is any nested list of atoms and  FlattenedList  is the same list with the nesting removed. The atom [] sh

Shell script, program for pyramid in shell script

program for pyramid in shell script

Shell script to print pyramid, Normal 0 false false false ...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Robot factory game, A deterministic finite automaton (DFA) is an abstract m...

A deterministic finite automaton (DFA) is an abstract machine that reads input from a serial (nonreversible) stream and changes between a finite number of  states according to the

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd