0% found this document useful (0 votes)
57 views1 page

Project 1 Operating Sys

The document discusses questions about implementing a basic shell program. It covers topics like how the cd command works, using fork vs execv, running multiple instances of the shell program, and how background processes are handled.

Uploaded by

Dylan Greytak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views1 page

Project 1 Operating Sys

The document discusses questions about implementing a basic shell program. It covers topics like how the cd command works, using fork vs execv, running multiple instances of the shell program, and how background processes are handled.

Uploaded by

Dylan Greytak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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?
Cd is necessary to change the working directory of the OS. It could not be
implemented by an external program.
2. Explain how our sample shell implements the change directory command.
The shell calls the chdir function on the argument following ‘cd’. If there is no
such directory, it prints out the error.
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;'
The program executes the command and crashes.
4. Explain what the return value of fork() means and how this program uses it.
Fork returns a negative value if the creation of a process was unsuccessful. It
returns a zero to the new child process. It also returns a positive value to the
parent or caller with the process ID.
In this program, if it is less than 0, it prints the error. If it is equal to zero,
it returns the child and exits from main. If it is positive, the parent will
continue around the loop.
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); }
The program keeps creating child processes when cd is called and does not allow
exit of program.
6. Can you run multiple versions of ./[Link] in the background? What happens to
their output?
Yes you can run multiple versions in the background. Their output stays the same.
7. Can you execute a second instance of our shell from within our shell program
(use './shell')? Which shell receives your input?
Yes you can another instance within the program. The new instance receives the
input.
8. What happens if you type CTRL-C while the countdown script ./[Link] is running?
What if ./[Link] is running in the background?
The shell exits.
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 the parent's PID.)
A shell can kill itself and its parent shell.
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 ./[Link]& exit ps
They do continue to run and you can see them with the command.

You might also like