CS计算机代考程序代写 Carleton University School of Computer Science

Carleton University School of Computer Science
COMP 3000 (WINTER 2021) OPERATING SYSTEMS ASSIGNMENT 1 (SOLUTIONS)
Questions – [18]
The following questions (where applicable) will be based on the original 3000shell.c in Tutorial 2:
1. [1]Manytasksrequiretherootprivilege,suchasinstallingasoftwarepackage,butusuallyyou cannot log in as root for security reasons. Instead, the sudo command is a convenient way of executing commands as root (by default, unless another user is specified). After you run “export X=7”, you will be able see it using the command “env”. But if you “sudo env” you won’t. Why [0.5/1]? What simple argument can you add to “sudo env” to see “X=7” [0.5/1]?
The internal command export only tells the shell to make the environment variable available to subsequently executed commands (by passing them to child processes). This is still under the same user. The sudo command (external) actually acts as a login program (similar to 3000userlogin) to temporarily switch to another user where the environment of the invoking user is not preserved by default. sudo -E env will preserve the environment variables. (man sudo)
2. [2] Think about the concepts of standard input/output/error and (pseudo)terminal. Try the three commands: , echo “Hi there?” > /dev/stdin, echo “Hi
. What do you see [0.5/2]? Why does it happen [1.5/2]? Hint: You only need to explain using the commands mentioned/shown in the lectures/tutorials.
What you see: Even though output to different devices files, the message is seen on the same terminal, which you’re looking at.
Why: By examining the three device files with ls -l or ll, we can see they point to (are a symbolic link to) /proc/self/fd/1, /proc/self/fd/0, and respectively. Further using the same command we can see they all point to the same , hence explaining why the message went to the “same place”.
Using the command stty, we see that the current terminal is the same /dev/pts/?, meaning that “same place” is our current terminal.
3. [4]Whenyourunaninteractiveprogram(e.g.,nanoortop)inthebackgroundwith“&”in3000shell, you may see characters typed lost every so often, or even end up with an unresponsive terminal.
echo “Hi there?” > /dev/stdout
there?” > /dev/stderr
/proc/self/fd/2
/dev/pts/?
Explain why this happens in 1-2 sentences [1/4].
In addition to hitting Ctrl-C (which harshly terminates both programs altogether), there is a smarter way of recovering from this, so that the interactive program becomes usable and you can exit to 3000shell normally: to send a signal to 3000shell. Which signal [1/4]? Explain why sending this signal solves the problem [2/4]. Hint: read the part of the code for signal handling carefully.
Why: Because after fork() the file descriptors (e.g., stdout/stdin) are still shared. Both the parent and the child are trying to read/write hence interfering with each other.
Which signal: SIGCHLD
Why: This is like a hack, as this signal is not designed for this purpose. What you need is to make the parent wait on the child as it normally does without the “&” being specified. From

the code, there are two places the parent can wait (line 205 and line 234). Look at the function signal_handler(), if a signal SIGCHLD is received, line 205 will be executed, which puts the parent back to waiting.
4. [4]Fornowthecommandpromptof3000shellisveryprimitive.Implementthesamepromptasthat of bash, specifically:
– Instead of “student $”, it should display “$USER@$HOSTNAME:$PWD$”
– For example, when USER=student, HOSTNAME= compxxxx and the current directory is ~/comp3000, it should display “student@compxxxx:~/comp3000” or “student@compxxxx: /home/student/comp3000”
– It is optional to replace the home directory (e.g., /home/student) with a “~”
– Note that $HOSTNAME is a Bash variable (not inherited), so to see the expected result above you need to run export HOSTNAME before 3000shell.
See code in the next questions’ solution. You only need to add 3 lines of code and change 1 line.
5. [4]Youmayhavenoticedthat3000shelldoesnotsupporttheinternalcommand“cd”.Implementa simplified version of “cd”.
– The current directory should be changed to what follows “cd”, e.g., cd / enters the root directory, cd .. goes back to the parent directory, and cd /var/log enters /var/log.
– Verify your implementation using the command “ls” to list files in the current directory.
– “cd” with no argument should enter the home directory of the user.
– To get full marks, your prompt must reflect the current directory changed by “cd”. For example:
student@compxxxx:/var$ cd student@compxxxx:/home/student$ cd / student@compxxxx:/$
– Note: “simplified” is in the sense that our cd does not support everything a regular cd does, e.g., you are not required to support options like -L, or shorthand like “-“ (previous directory) and “~”. Hint: you may need two functions setenv() and chdir() (and other functions depending on your way of implementing it).
See an implementation here. Note that if you don’t rely on the environment variable $PWD (hence not using ) but can still achieve the same functionality, it’s also acceptable.
6. [3]Implementsupportforstandardinputredirection,withtheoperator“<”.Thiswouldbesimilar(if not the same) to the already implemented output redirection. - As with output redirection (command >output.txt), 3000shell will support: command open(). See the code here.
setenv()/
getenv()
2