FinalProject (Submit)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

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: Mai Đăng Minh


Mã số sinh viên: 21522340
Lớp: CS4323.CTTT

HỆ ĐIỀU HÀNH
FINAL PROJECT

CHECKLIST
2.5. BÀI TẬP THỰC HÀNH
Homework

Problem Problem
1 2
Trình bày cách làm
Chụp hình minh chứng

Giải thích kết quả

Tự chấm điểm: 8

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.

HOMEWORK
6.2.1. Probem 1 (4 scores) Implement Section 3: Creating a history feature
Trả lời...
Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
 These are the necessary header files for various system calls and functions used in
the program, such as process handling, input/output, string manipulation, and
memory allocation.

#define MAX_LINE 100


#define HIST_SIZE 20
 These macros define the maximum length of a command line (MAX_LINE) and
the size of the command history array (HIST_SIZE).

char history[HIST_SIZE][MAX_LINE];
 This two-dimensional array (history) is used to store command history. Each row
represents a command, and there is a total of HIST_SIZE rows.

void addToHistory(const char *command) {


// ...
}
 This function adds a command to the command history array. It shifts existing
commands down and inserts the new command at the beginning.
int main(void) {

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.

// ...
}
 The main function initializes variables and enters a loop (while (should_run)) to
repeatedly prompt the user for input and execute commands until the user enters
"exit".

while (should_run) {
printf("21522340>");
fflush(stdout);

fgets(args, sizeof(args), stdin);


args[strcspn(args, "\n")] = '\0';
}
 The program prints a command prompt, reads the user's input using fgets, and
removes the newline character at the end.

if (strcmp(args, "exit") == 0) {
should_run = 0;
}
 If the user enters "exit," the loop exit condition is set, and the program will
terminate.

else if (strcmp(args, "!!") == 0) {


// ...
}
 If the user enters "!!," the most recent command from history is executed.
if (history[0][0] == '\0') {
printf("No commands in history. \n");
continue;
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.

}
 If the history is empty, it prints a message and continues to the next iteration of the
loop

pid_t pid = fork();


if (pid == 0) {
// ...
}
if (pid > 0) {
wait(NULL);
}
 A child process is forked, and the previous command is tokenized and executed
using execvp. The parent process waits for the child process to complete.
else {
// ...
}
 If the user enters a command other than "exit" or "!!," it is added to the history,
and the command is executed similarly to the previous case.

char *token;
char *tokens[MAX_LINE / 2 + 1];
int i = 0;
token = strtok(args, " ");
while (token != NULL) {
tokens[i++] = token;
token = strtok(NULL, " ");
}
tokens[i] = NULL;
execvp(tokens[0], tokens);
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.

perror("execvp");
_exit(1);
 The user's input or the command from history is tokenized into an array of strings
(tokens), and then execvp is used to replace the current process image with a new
one based on the specified command.
Result:

Explain result:
-21522340>^C: pressed Ctrl+C, which is a signal to interrupt the currently running
process. The shell responds with ^C, and I tried to execute something after that.
-execvp: No such file or directory: This error message indicates that the system couldn't
find the executable file I tried to run. It typically happens when there's a typo in the
command or the file I’m trying to execute doesn't exist in the specified location.
-21522340>!!: The double exclamation mark (!!) is a shortcut in many shells to repeat the
last executed command. I tried to repeat the previous command, but it resulted in the
same error.
-ls: This command lists the files and directories in the current directory. It shows a list of
items: finalproject1, finalproject3, lab5_1, lab5_2.1, lab5_2.2, lab5_3, and SJF.
-21522340>echo Minh Mike: This command prints the string "Minh Mike" to the
terminal.

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.

-asdfghjkl: It appears that I tried to run a command of a random name “asdfghjkl”, but it
resulted in the same execvp: No such file or directory error. This suggests that there is no
executable with that name in the system path.
-!!: I attempted to repeat the last failed command, which was “asdfghjkl”, resulting in the
same error.
-^C: Again, I pressed Ctrl+C, interrupting the execution of the current process.
Link video : https://drive.google.com/file/d/1YY0T2fBn8Cbgq0YH-
Pg6fkdaHXGkfchQ/view?usp=sharing

6.2.2. Problem 2 (4 scores) Choose either Section 4: Redirtecting Input and Output
or Section 5: Communication via a pipe to implement. All of your work should be
done by individual. Your report for each question should cover the following
requirements:
a. What is your solution for the problem?
b. The proof that your solution work correctly? Video demonstration is highly
recommended.
c. Explain your result? Please remember to write your explanation in the report.
You do not have to explain in the video.
Trả lời...
Code:

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.

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.

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.

Result

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.

File in.txt:

File out.txt:

Explain:
When entering the command "ls > out.txt," it creates a file named "out.txt" and stores the
output of the "ls" command in this file. Similarly, if an "in.txt" file is created with text
content and the command "cat < in.txt" is used, it prints the text values from the "in.txt"
file. When a list of integers is placed in "in.txt," and the command "sort < in.txt" is
executed, the values in the file are sorted, and the sorted list of integers is printed. Using
the command "!!" subsequently prints the last entered command along with its output. In

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.

summary, the described commands showcase the functionality of input and output
redirection, command repetition with "!!," and the indication of execution failure for
invalid commands.
Link video:
https://drive.google.com/file/d/1pVjpCfn7NTJecox2_IspnfE4pZDVBIuu/view?usp=sha
ring
Link source code:
https://drive.google.com/file/d/1noDWSMxIdss5t_YOPzaRHx_esO5d-
RPE/view?usp=sharing

13

You might also like