FINAL PROJECT | Báo cáo thực hành môn Hệ điều hành

Prior to adding the history variable to the array, we must declare it and write the function for it. Therefore, when the users enter the commands as a string value, we will use stcpy function to add the value of that command to the array history. Tài liệu giúp bạn tham khảo, củng cố kiến thức và ôn tập đạt kết quả cao.

Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
1
H và tên: Trn Ngc Xuân Mai
Mã s sinh viên: 21522322
Lp: CS4323.O12.CTTT
OPERATING SYSTEM
FINAL PROJECT HOMEWORK
PROBLEM 1
Implement Section 3: Creating a history feature
Solution:
-To solve this problem, we must establish an array to hold user instructions that they have
submitted and to trigger the command again whenever a user enters "!!"
-Prior to adding the history variable to the array, we must declare it and write the function
for it. Therefore, when the users enter the commands as a string value, we will use stcpy
function to add the value of that command to the array history.
-To determine if the history value of the array is empty or not, we must send it a signal.
To do this, we will write a check function.
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
2
C Script:
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
3
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
4
Explanation:
Step 1: Create the add command in the array function and declare the array's variable.
- Using the fgets function, which will read and save the command as an input variable, I
define an input variable and store the command. My fgets function will retain up to 100
characters in addition to the newline character ("\n") since I used MAX_LINE 100.
Furthermore, my array may have a maximum of 20 variables called HISTORY_LINE 20.
Step 2: Respond to the user's "!!" entry.
- When a user enters "!!" as input, the software begins to search the array to see whether
there are any commands there. whether it finds none, it displays "No commands in
history."
- The software will choose the most recent command from the history array and assign it
to the input array if there are any commands in there.
- The software will end the loop if the user types "exit" as a command while it is
executing.
- The application will carry out the command if the user types "ls-l" as the command.
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
5
Testing output:
Explanation:
-I'll test my software to see whether it runs correctly after executing the C script.
-First off, since I haven't entered any instructions yet, the application will say "No
commands in the history" if we input "!!".
- To list every directory in the current path, I then input the ls command. At this point,
the software stores the ls command in a variable located inside the history array.
- After that, I entered another command, echo XuanMai, and the output printed
"XuanMai." Next, I entered "!!" to test the program, and it printed the current command,
echo XuanMai, in the history array.
- The software will show "Exec failed" if we enter a string of random characters without
any sense, but it will also record that random text in the history array.
-To make the program exist, enter exit.
PROBLEM 2
Implement Section 4: Redirecting Input and Output
Solution:
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
6
-We must put in place a method to recognize ">" for input direction inside user
instructions and ">" for output redirection in order to solve this issue.
-It will need modifying the current code structure to incorporate input and output
redirection into the shell.
-This entails maintaining file descriptors using methods like dup2 for redirection,
processing the input, and finding redirection operators.
-The creation or opening of files related to redirection is handled by fileoperations, such
open.
-To solve this issue, it will be necessary to import a few specialized libraries in order to
write code that can handle the characters ">" and "\" for the redirection process and run
the command that is used to reroute user-inputted commands.
C Script:
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
7
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
8
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
9
Explanation: To do this assignment, we must add two flags: redirect_input and
redirect_output for the signal.
- The software will examine the args array for the symbol <and> in order to determine
whether there is an input or output redirection. The files will be saved if redirection is
discovered.
- If input redirection is set in the child process, dup2 is used to open the input file and
redirect the input. The output file is opened and output is redirected to the files if output
redirection is selected.
- The command is then carried out by the child process using execvp. An error message is
generated if execvp fails.
- To wait for the child process to end, the parent process must wait until the pid is called.
- Up until the user types the "exit." Command, the loop keeps going and the steps are
repeated. The software will terminate when users leave.
Output Redirection “>:
- The application carefully examines every token to identify the ">" output redirection
sign.
- Upon detection, output redirection is deemed necessary and the redirect_output flag is
raised. After that, the token is void, therefore the '>' is removed from the instruction. The
loop finishes since just one redirection symbol is predicted.
- The code searches for the next token (tokens[j + 1]) after identifying the ">" operator in
the command, assuming that this is the filename to which the output has to be redirected.
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
10
- The file is opened in write-only (O_WRONLY) mode, created if it doesn't already exist
(O_CREAT), and truncated to zero length (O_TRUNC) using the open system function.
0644 is the file permission setting.
- I copied the file descriptor fd onto the standard output file descriptor using the
dup2(fd,STDOUT_FILENO) function.
Input Redirection “<”:
- Input redirection handling uses a similar method to output redirection. Every token is
analyzed by the software to check for the input redirection sign "\".
- The redirect_input flag is set to 1 upon recognition, indicating that input redirection is
necessary. Then, the token is set to NULL, removing the command's '<'. Upon realizing
that just one redirection symbol is expected, the loop is closed.
- The child process can then optionally perform redirection by using the flags
redirect_input and redirect_output, depending on their values.12
- After identifying the operator "<", the code searches tokens[j + 1] for the subsequent
token, thinking that this is the filename for input redirection. The file is then opened in
read-only mode using the open system function.
-The function dup2(fd, STDIN_FILENO) redirects standard input from the given file by
copying the file descriptor fd onto the standard input file descriptor.
- The file descriptor is then closed by the code using close(fd), as it is no longer required
following redirection.
- After the file settings are set, the user command is executed by the system, which takes
into account the kind of redirection depending on the signal (">" or "<") entered by the
user.
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
11
Testing output:
Explanation:
- The file that exists in the current direction is displayed in the console when the user
types "ls"
-When the user types "ls > out.txt" as I demonstrate in the video, the names of all the
files in the current direction will display in the out.txt file.
-Because the user enters random characters, "Exec failed" is the only message that is
stored and shown.
-The file in.txt will be sorted and shown in the terminal when the user enters cat <
in.txt.
- The application will end when the user types "exit".
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
12
Input file:
Output file:
Báo cáo thc hành môn H điều hành - Ging viên: Trn Hoàng Lc.
13
This is a link to a video illustrating the output of sections 2, 3, and 4:
https://drive.google.com/drive/folders/1I8OF-OR5HqySFehbj5k9Fb-
91JSUpSyf?usp=drive_link
| 1/13

Preview text:

Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc.
Họ và tên: Trần Ngọc Xuân Mai Mã số sinh viên: 21522322 Lớp: CS4323.O12.CTTT OPERATING SYSTEM
FINAL PROJECT – HOMEWORK PROBLEM 1
Implement Section 3: Creating a history feature Solution:
-To solve this problem, we must establish an array to hold user instructions that they have
submitted and to trigger the command again whenever a user enters "!!"
-Prior to adding the history variable to the array, we must declare it and write the function
for it. Therefore, when the users enter the commands as a string value, we will use stcpy
function to add the value of that command to the array history.
-To determine if the history value of the array is empty or not, we must send it a signal.
To do this, we will write a check function. 1
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. C Script: 2
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. 3
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. Explanation:
Step 1: Create the add command in the array function and declare the array's variable.
- Using the fgets function, which will read and save the command as an input variable, I
define an input variable and store the command. My fgets function will retain up to 100
characters in addition to the newline character ("\n") since I used MAX_LINE 100.
Furthermore, my array may have a maximum of 20 variables called HISTORY_LINE 20.
Step 2: Respond to the user's "!!" entry.
- When a user enters "!!" as input, the software begins to search the array to see whether
there are any commands there. whether it finds none, it displays "No commands in history."
- The software will choose the most recent command from the history array and assign it
to the input array if there are any commands in there.
- The software will end the loop if the user types "exit" as a command while it is executing.
- The application will carry out the command if the user types "ls-l" as the command. 4
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. Testing output: Explanation:
-I'll test my software to see whether it runs correctly after executing the C script.
-First off, since I haven't entered any instructions yet, the application will say "No
commands in the history" if we input "!!".
- To list every directory in the current path, I then input the “ls” command. At this point,
the software stores the ls command in a variable located inside the history array.
- After that, I entered another command, “echo XuanMai”, and the output printed
"XuanMai." Next, I entered "!!" to test the program, and it printed the current command,
echo XuanMai, in the history array.
- The software will show "Exec failed" if we enter a string of random characters without
any sense, but it will also record that random text in the history array.
-To make the program exist, enter “exit”. PROBLEM 2
Implement Section 4: Redirecting Input and Output Solution: 5
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc.
-We must put in place a method to recognize ">" for input direction inside user
instructions and ">" for output redirection in order to solve this issue.
-It will need modifying the current code structure to incorporate input and output redirection into the shell.
-This entails maintaining file descriptors using methods like dup2 for redirection,
processing the input, and finding redirection operators.
-The creation or opening of files related to redirection is handled by fileoperations, such open.
-To solve this issue, it will be necessary to import a few specialized libraries in order to
write code that can handle the characters ">" and "\" for the redirection process and run
the command that is used to reroute user-inputted commands. C Script: 6
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. 7
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. 8
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc.
Explanation: To do this assignment, we must add two flags: redirect_input and
redirect_output for the signal.
- The software will examine the args array for the symbol in order to determine
whether there is an input or output redirection. The files will be saved if redirection is discovered.
- If input redirection is set in the child process, dup2 is used to open the input file and
redirect the input. The output file is opened and output is redirected to the files if output redirection is selected.
- The command is then carried out by the child process using execvp. An error message is generated if execvp fails.
- To wait for the child process to end, the parent process must wait until the pid is called.
- Up until the user types the "exit." Command, the loop keeps going and the steps are
repeated. The software will terminate when users leave.
Output Redirection “>”:
- The application carefully examines every token to identify the ">" output redirection sign.
- Upon detection, output redirection is deemed necessary and the redirect_output flag is
raised. After that, the token is void, therefore the '>' is removed from the instruction. The
loop finishes since just one redirection symbol is predicted.
- The code searches for the next token (tokens[j + 1]) after identifying the ">" operator in
the command, assuming that this is the filename to which the output has to be redirected. 9
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc.
- The file is opened in write-only (O_WRONLY) mode, created if it doesn't already exist
(O_CREAT), and truncated to zero length (O_TRUNC) using the open system function.
0644 is the file permission setting.
- I copied the file descriptor fd onto the standard output file descriptor using the
dup2(fd,STDOUT_FILENO) function.
Input Redirection “<”:
- Input redirection handling uses a similar method to output redirection. Every token is
analyzed by the software to check for the input redirection sign "\".
- The redirect_input flag is set to 1 upon recognition, indicating that input redirection is
necessary. Then, the token is set to NULL, removing the command's '<'. Upon realizing
that just one redirection symbol is expected, the loop is closed.
- The child process can then optionally perform redirection by using the flags
redirect_input and redirect_output, depending on their values.12
- After identifying the operator "<", the code searches tokens[j + 1] for the subsequent
token, thinking that this is the filename for input redirection. The file is then opened in
read-only mode using the open system function.
-The function dup2(fd, STDIN_FILENO) redirects standard input from the given file by
copying the file descriptor fd onto the standard input file descriptor.
- The file descriptor is then closed by the code using close(fd), as it is no longer required following redirection.
- After the file settings are set, the user command is executed by the system, which takes
into account the kind of redirection depending on the signal (">" or "<") entered by the user. 10
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. Testing output: Explanation:
- The file that exists in the current direction is displayed in the console when the user types "ls"
-When the user types "ls > out.txt" as I demonstrate in the video, the names of all the
files in the current direction will display in the out.txt file.
-Because the user enters random characters, "Exec failed" is the only message that is stored and shown.
-The file in.txt will be sorted and shown in the terminal when the user enters “cat < in.txt”.
- The application will end when the user types "exit". 11
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc. Input file: Output file: 12
Báo cáo thực hành môn Hệ điều hành - Giảng viên: Trần Hoàng Lộc.
This is a link to a video illustrating the output of sections 2, 3, and 4:
https://drive.google.com/drive/folders/1I8OF-OR5HqySFehbj5k9Fb-
91JSUpSyf?usp=drive_link 13