You are on page 1of 17

Họ và tên: NGUYỄN TOÀN TIẾN CƯỜNG

MSSV: 21521906
Lớp: CS4323.O11.CTTT

OPERATING SYSTEM
Final Project – Homework
Video demo:
https://drive.google.com/drive/folders/122hiCLnrvMsN8iun
XlkKUiXHW3A3tBd2?usp=sharing
Source code and txt:
https://drive.google.com/drive/folders/1HkHx7CMY7Opiqw_3p
KORWzho90SS21k2?usp=sharing
Problem 1:
Implement Section 3: Creating a history feature
The solution to this problem closely resembles a classwork problem we
previously encountered. In this instance, we are required to implement an array
variable to store the user's previous commands, alongside executing the commands
entered by the user in the shell interface. This will enable us to execute a specific
command again when the user inputs "!!".
After declaring a history variable, we will create a function that adds the
user's command to the history array. Since the command is entered as a string, we
can consider using the strcpy function to assign the command value to the history
array. It is important to handle cases where the history array is empty.
Furthermore, we need to modify the code responsible for handling user input
in order to incorporate the functionality of executing the most recent command
when the user inputs "!!".
Code explaination:
Due to the previous explanation of the shell interface creation process during
the classwork, I will now provide an additional explanation of the accompanying
code.

To begin with, an array was declared to store variables of type char.


Following that, a function was implemented with the purpose of adding a new
command to the command history, which is maintained in the previously declared
history array. This function operates by shifting the existing commands down by
one position to create room for the new command. Subsequently, the new
command is copied to the first row of the array, effectively updating the history
with the most recent command.
The provided code addresses the scenario when the user inputs the "!!"
symbol. In the case where there is no command stored in the history array, it will
display the message "No command in history." Conversely, if there are commands
in the history array, the code will execute the most recent command using the
execvp function, following the same execution process as outlined in the classwork
assignment. This code will add the command input by the user to history so it can
be executed in the future.
Explain the result:

Based on the results and conclusions you have provided, it appears that you
have successfully implemented the additional function for creating a history
feature in the shell. The behavior you described confirms that the code correctly
handles the "!!" command by executing the most recent command from the history
array. Additionally, the code appropriately handles cases where there are no
commands in the history or when an invalid command is entered. Based on your
observations and conclusions, it seems that you have accomplished the task
correctly. Well done!

PROBLEM 2
Implement Section 4: Redirtecting Input and Output
To enable input and output redirection in the existing shell code, it is
necessary to expand the code structure. This entails implementing a mechanism to
detect the '>' symbol for output redirection and the '<' symbol for input redirection
within user commands. The process involves parsing the user input, identifying the
redirection operators, and managing file descriptors using functions like 'dup2' for
redirection purposes. Additionally, file operations, such as 'open', are used to
handle the creation or opening of files associated with the redirection process.
The task also requires importing the required libraries and implementing the
code accordingly to handle the '>' and '<' symbols, allowing for the execution of
commands that utilize input and output files specified by the user.
Code
Code Explanation:
To handle input and output redirection, two flags, namely "redirect_input"
and "redirect_output," are introduced. These flags are utilized to indicate whether
input or output redirection is necessary. For output redirection (">"), the code
checks if the current token corresponds to the output redirection symbol. If a match
is found, the "redirect_output" flag is set to 1, indicating the need for output
redirection. The token containing the ">" symbol is then set to NULL to remove it
from the command. The loop is terminated at this point since only one redirection
symbol is expected. The same approach is applied for input redirection ("<"). If the
current token matches the input redirection symbol, the "redirect_input" flag is set
to 1 to indicate input redirection is required. The token containing the "<" symbol
is set to NULL.
=) By employing these flags, the program can identify whether input or
output redirection is necessary without immediately manipulating the command
stored in the tokens array. Later, in the child process, the values of the
"redirect_input" and "redirect_output" flags can be used to conditionally perform
the appropriate redirection.
To redirect the output:
If the ">" operator is detected in the command, the code proceeds to look for
the next token (tokens[j + 1]), assuming it is the filename to which the output
should be redirected.
The open system call is used to open the file in write-only mode
(O_WRONLY), creating it if it doesn't exist (O_CREAT), and truncating it to zero
length (O_TRUNC). The file permissions are set to 0644.
The dup2(fd, STDOUT_FILENO) function duplicates the file descriptor fd
to the standard output file descriptor (STDOUT_FILENO), effectively redirecting
the standard output to the specified file.
Finally, the close(fd) function is called to close the file descriptor, as it is no
longer needed after redirection.
To redirect the input:
If the "<" operator is detected, the code proceeds to look for the next token
(tokens[j + 1]), assuming it is the filename from which the input should be
redirected.
The open system call is used to open the file in read-only mode
(O_RDONLY).
The dup2(fd, STDIN_FILENO) function duplicates the file descriptor fd to
the standard input file descriptor (STDIN_FILENO), redirecting the standard input
from the specified file.
Similarly, the close(fd) function is called to close the file descriptor, as it is
no longer needed after redirection.
=) Once the file has been set up for redirection, the system will execute the user
command with the appropriate input/output redirection based on the user's input
signal (">" or "<").
Explain the result.
Redirecting output

When you used the command "ls > out.txt", the output of the "ls" command
was successfully redirected to the "out.txt" file. This happened because you used
the ">" symbol, which instructed the shell to redirect the output to the specified
file. As a result, the output of the "ls" command was written to the "out.txt" file
instead of being displayed in the terminal, matching your expected behavior.

In the example where you used the command "echo hello > out.txt", the word
"hello" was successfully printed to the "out.txt" file. This outcome indicates that
you have correctly implemented the redirection functionality. By utilizing the ">"
symbol, you redirected the output of the "echo" command to the specified file,
resulting in the word "hello" being written to the "out.txt" file, as you expected.
Redirecting input
To begin with, I possess a file called "in.txt" that comprises numbers, similar
to the picture mentioned earlier.
Afterward, I utilized the command "sort < in.txt" which executed the "sort"
command using the input values from the "in.txt" file. By using the "<" symbol, I
successfully redirected the input to be sourced from the specified file. As
demonstrated in the resulting output, the numbers from the "in.txt" file are
displayed in a sorted list. This outcome confirms that I have effectively redirected
the input and output as intended.
=) Based on the aforementioned tests, it can be concluded that I have accurately
implemented the input and output redirection, successfully fulfilling the
requirements of this question.

You might also like