From 68aea089b4afb442c9f0a2b7a74fd654fb1dccc9 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:31:42 +0000 Subject: [PATCH 01/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-22/matrix_mul.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/exercise-22/matrix_mul.c b/src/exercise-22/matrix_mul.c index 0752395..cf78e1b 100644 --- a/src/exercise-22/matrix_mul.c +++ b/src/exercise-22/matrix_mul.c @@ -19,6 +19,14 @@ multiply (int **A, int ASize, int *AColSize, int **B, int BSize, int *BColSize, // 下面通过多重循环实现两个矩阵相乘 + for (int i = 0; i < ASize; i++) { + for (int j = 0; j < BColSize[0]; j++) { + for (int k = 0; k < BSize; k++) { + result[i][j] += A[i][k] * B[k][j]; + } + } + } + *returnSize = ASize; return result; } -- Gitee From 08e5b149e1ce4a8e2995df7b47b5f46ce800d320 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:33:22 +0000 Subject: [PATCH 02/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-22/matrix_mul.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/exercise-22/matrix_mul.c b/src/exercise-22/matrix_mul.c index cf78e1b..19b594e 100644 --- a/src/exercise-22/matrix_mul.c +++ b/src/exercise-22/matrix_mul.c @@ -43,3 +43,4 @@ printMatrix (int **matrix, int rows, int cols) printf ("\n"); } } + \ No newline at end of file -- Gitee From 6ceb1338d93d03908793e350239b3e25b1535c03 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:36:42 +0000 Subject: [PATCH 03/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-23/convolution.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/exercise-23/convolution.c b/src/exercise-23/convolution.c index f86ec9e..753cf3d 100644 --- a/src/exercise-23/convolution.c +++ b/src/exercise-23/convolution.c @@ -16,4 +16,20 @@ convolution2D (int input[5][5], int kernel[3][3], int output[3][3], } // 在下面通过多重循环实现卷积运算 + // 计算卷积 + for (i = 0; i < inputSize - kernelSize + 1; i++) + { + for (j = 0; j < inputSize - kernelSize + 1; j++) + { + // 对于每个输出元素,应用卷积核 + for (m = 0; m < kernelSize; m++) + { + for (n = 0; n < kernelSize; n++) + { + output[i][j] += input[i + m][j + n] * kernel[m][n]; + } + } + } + } +} } -- Gitee From 15cae030686f41f69bac425341385ecb135e459d Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:45:08 +0000 Subject: [PATCH 04/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-24/matrix_trans.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/exercise-24/matrix_trans.c b/src/exercise-24/matrix_trans.c index 9cdf524..5f78e6d 100644 --- a/src/exercise-24/matrix_trans.c +++ b/src/exercise-24/matrix_trans.c @@ -4,9 +4,15 @@ void transposeInPlace (int matrix[N][N]) { - // 在下面实现矩阵原地转置,即空间复杂度为O(1) - - // + // 对角线上方的元素与对角线下方的元素交换 + for (int i = 0; i < N; i++) { + for (int j = i + 1; j < N; j++) { + // 交换 matrix[i][j] 和 matrix[j][i] + int temp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = temp; + } + } } void -- Gitee From 2863e4c2c6f5a71ddc3475da8b74e84b8da4b773 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:46:52 +0000 Subject: [PATCH 05/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-25/matrix_zero.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/exercise-25/matrix_zero.c b/src/exercise-25/matrix_zero.c index ba1e8c2..2dade1c 100644 --- a/src/exercise-25/matrix_zero.c +++ b/src/exercise-25/matrix_zero.c @@ -13,10 +13,36 @@ setZeroes (int **matrix, int matrixSize, int *matrixColSize) memset (row, 0, m * sizeof (int)); memset (col, 0, n * sizeof (int)); - // 在下面实现将矩阵中为0的元素所在的行和列都置为0 + // 标记需要置零的行和列 + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (matrix[i][j] == 0) { + row[i] = 1; + col[j] = 1; + } + } + } + + // 将对应行置零 + for (int i = 0; i < m; i++) { + if (row[i] == 1) { + for (int j = 0; j < n; j++) { + matrix[i][j] = 0; + } + } + } + + // 将对应列置零 + for (int j = 0; j < n; j++) { + if (col[j] == 1) { + for (int i = 0; i < m; i++) { + matrix[i][j] = 0; + } + } + } - free (row); - free (col); + free(row); + free(col); } void -- Gitee From ef94eac1b1cfa3b09d961981a436cdd1b02ee23f Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 13:48:54 +0000 Subject: [PATCH 06/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-26/matrix_kmin.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/exercise-26/matrix_kmin.c b/src/exercise-26/matrix_kmin.c index 635e08d..a171857 100644 --- a/src/exercise-26/matrix_kmin.c +++ b/src/exercise-26/matrix_kmin.c @@ -81,9 +81,26 @@ kthSmallest (int **matrix, int matrixSize, int *matrixColSize, int k) MinHeap *heap = createMinHeap (matrixSize); // 下面对算法进行具体实现 + for (int i = 0; i < matrixSize; ++i) + { + HeapNode node = { matrix[i][0], i, 0 }; + insertMinHeap (heap, node); + } + while (k > 1) + { + HeapNode node = extractMin (heap); + int row = node.row; + int col = node.col; + if (col + 1 < matrixColSize[row]) + { + HeapNode nextNode = { matrix[row][col + 1], row, col + 1 }; + insertMinHeap (heap, nextNode); + } + --k; + } int result = heap->array[0].val; free (heap->array); free (heap); return result; -} +} \ No newline at end of file -- Gitee From 72189aedfdbdf55cc023e9489cfa493c3e591fcb Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:25:53 +0000 Subject: [PATCH 07/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-36/max_value.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/exercise-36/max_value.c b/src/exercise-36/max_value.c index 1a97fef..071dd43 100644 --- a/src/exercise-36/max_value.c +++ b/src/exercise-36/max_value.c @@ -55,10 +55,18 @@ max_value (int arr[], int size) int max = arr[0]; // >>> 在这里实现最大值查找。 + for (i = 1; i < size; i++) + { + if (arr[i] > max) + { + max = arr[i]; + } + } return max; } + static int __init max_init (void) { -- Gitee From b801e10f328b9d327e76c6e47e998d026c8fcdb2 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:27:51 +0000 Subject: [PATCH 08/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-37/timer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/exercise-37/timer.c b/src/exercise-37/timer.c index 107c93f..c7de8b6 100644 --- a/src/exercise-37/timer.c +++ b/src/exercise-37/timer.c @@ -16,8 +16,11 @@ timer_init (void) { printk (KERN_INFO "Module loaded\n"); - // >>> 在这里实现计数器 - // >>> Start timer after 5000 milliseconds (5 seconds) + // 初始化定时器 + timer_setup(&my_timer, my_timer_callback, 0); + + // 启动定时器,5秒后触发 + mod_timer(&my_timer, jiffies + msecs_to_jiffies (5000)); return 0; } -- Gitee From 4e70f8ddb3e1d4b8e558477c17b5ed54716bfe26 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:32:35 +0000 Subject: [PATCH 09/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-38/chardev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/exercise-38/chardev.c b/src/exercise-38/chardev.c index 84250dc..48b8cd1 100644 --- a/src/exercise-38/chardev.c +++ b/src/exercise-38/chardev.c @@ -33,6 +33,12 @@ dev_read (struct file *file, char *buffer, size_t len, loff_t *offset) int bytes_read = 0; // >>> 实现读入设备字符。 + while (len && *message_ptr) + { + put_user (*(message_ptr++), buffer++); + len--; + bytes_read++; + } return bytes_read; } @@ -67,4 +73,4 @@ module_exit (chardev_exit); MODULE_LICENSE ("GPL"); MODULE_AUTHOR ("Your Name"); MODULE_DESCRIPTION ( - "Virtual Character Device Module with Parameterized Input"); + "Virtual Character Device Module with Parameterized Input"); \ No newline at end of file -- Gitee From 33d70404b581f91625b88169262fb9199a98be30 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:34:02 +0000 Subject: [PATCH 10/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-39/fileop.c | 54 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/exercise-39/fileop.c b/src/exercise-39/fileop.c index 529ae52..c8a787d 100644 --- a/src/exercise-39/fileop.c +++ b/src/exercise-39/fileop.c @@ -9,11 +9,55 @@ static struct file *file_ptr = NULL; static int __init fileop_init (void) { - // >>> 实现文件访问操作。 - // - // if true - // printk(KERN_INFO "File operation successful\n") - // printk(KERN_ALERT "Failed to open file\n") + mm_segment_t old_fs; + loff_t pos = 0; + ssize_t bytes; + char *buf; + size_t buf_size = 1024; + + printk(KERN_INFO "Module loaded\n"); + + // Set the address limit to kernel space + old_fs = get_fs(); + set_fs(KERNEL_DS); + + // Open the file + file_ptr = filp_open(FILE_NAME, O_RDONLY, 0); + if (IS_ERR(file_ptr)) + { + printk(KERN_ALERT "Failed to open file\n"); + set_fs(old_fs); + return PTR_ERR(file_ptr); + } + + // Allocate buffer for file content + buf = kmalloc(buf_size, GFP_KERNEL); + if (!buf) + { + filp_close(file_ptr, NULL); + printk(KERN_ALERT "Failed to allocate buffer\n"); + set_fs(old_fs); + return -ENOMEM; + } + + // Read the file content + bytes = kernel_read(file_ptr, buf, buf_size - 1, &pos); + if (bytes >= 0) + { + buf[bytes] = '\0'; // Null-terminate the buffer + printk(KERN_INFO "File content:\n%s\n", buf); + printk(KERN_INFO "File operation successful\n"); + } + else + { + printk(KERN_ALERT "Failed to read file\n"); + } + + // Free the buffer + kfree(buf); + + // Restore the address limit + set_fs(old_fs); return 0; } -- Gitee From ce749bc4c146eacf514ab01415e9cb84ae451c51 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:36:50 +0000 Subject: [PATCH 11/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-43/main.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/exercise-43/main.c b/src/exercise-43/main.c index 90f6ee2..055e9ea 100644 --- a/src/exercise-43/main.c +++ b/src/exercise-43/main.c @@ -3,36 +3,30 @@ #include // RISC-V 汇编内联汇编函数,用于计算斐波那契数列的第 n 个数 -int -fibonacci (int n) -{ +int fibonacci (int n) { int result; // 用于保存计算结果 - // >>> 替换 PLACEHOLDER 为合适的指令完成注释的功能 __asm__ volatile ( - // 初始化斐波那契数列的前两个数为 0 和 1 "li t0, 0\n\t" "li t1, 1\n\t" // 如果 n 为 0,则返回 return_zero标签 - "PLACEHOLDER\n\t" + "beq %1, x0, return_zero\n\t" // 如果 n 为 1,则返回 return_one标签 - "PLACEHOLDER\n\t" + "beq %1, x1, return_one\n\t" "loop:\n\t" "addi %1, %1, -1\n\t" "add t2, t0, t1\n\t" - // 更新 t0 为 t1 - "PLACEHOLDER\n\t" + "mv t0, t1\n\t" // 更新 t1 为 t2 "mv t1, t2\n\t" - // 如果 n 不为 0,则继续循环 "bne %1, x0, loop\n\t" // 循环结束,最终结果存储在 %0 中 - "PLACEHOLDER\n\t" + "mv %0, t0\n\t" "j end\n\t" // n 为 0 时的返回值 @@ -50,7 +44,6 @@ fibonacci (int n) : "r"(n) : "t0", "t1", "t2"); - // <<< return result; } -- Gitee From f50387be8fe407786f684287544e418806f3ed82 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:40:01 +0000 Subject: [PATCH 12/21] =?UTF-8?q?=E9=AA=8F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-44/main.c | 97 ++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/src/exercise-44/main.c b/src/exercise-44/main.c index 32db751..df364a8 100644 --- a/src/exercise-44/main.c +++ b/src/exercise-44/main.c @@ -3,78 +3,73 @@ #include // RISC-V 汇编内联汇编函数,用于计算整数数组的和 -int -calculate_sum (int *arr, int n) +int calculate_sum(int *arr, int n) { - int result = 0; // 用于保存计算结果 + int result = 0; // 用于保存计算结果 - // >>> 替换 PLACEHOLDER 为合适的指令完成注释的功能 - __asm__ volatile ( - // 用于循环计数(索引)的寄存器 - "li t0, 0\n" - // result值初始化为 0 - "li t1, 0\n" + __asm__ volatile ( + // 用于循环计数(索引)的寄存器 + "li t0, 0\n\t" + // result值初始化为 0 + "li t1, 0\n\t" - "loop:\n\t" - // 如果索引是否大于等于n,跳转end标签 - "PLACEHOLDER\n\t" + "loop:\n\t" + // 如果索引t0是否大于等于n,跳转end标签 + "bge t0, %2, end\n\t" - // 从数组中加载元素值保存到t2寄存器 - "lw t2, 0(%1)\n\t" + // 从数组中加载元素值保存到t2寄存器 + "lw t2, 0(%1)\n\t" - // 计算 result += arr[i],result值保存在t1寄存器 - "PLACEHOLDER\n\t" - // 数组索引自增,指向下一个元素 - "PLACEHOLDER\n\t" + // 计算 result += arr[i], result值保存在t1寄存器 + "add t1, t1, t2\n\t" + // 数组索引自增,指向下一个元素 + "addi %1, %1, 4\n\t" - "addi t0, t0, 1\n\t" - "j loop\n" + "addi t0, t0, 1\n\t" + "j loop\n" - "end:\n\t" - "mv %0, t1\n" + "end:\n\t" + "mv %0, t1\n" - : "=r"(result) - : "r"(arr), "r"(n) - : "t0", "t1", "t2"); + : "=r"(result) + : "r"(arr), "r"(n) + : "t0", "t1", "t2"); - // <<< - return result; + return result; } -int -main (int argc, char *argv[]) +int main(int argc, char *argv[]) { - if (argc != 3) + if (argc != 3) { - fprintf (stderr, "Usage: %s \n", argv[0]); - return 1; + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; } - int n = atoi (argv[1]); - int arr[n]; + int n = atoi(argv[1]); + int arr[n]; - // 解析逗号分隔的数组字符串 - char *arr_str = argv[2]; - char *token = strtok (arr_str, ","); - int i = 0; - while (token != NULL) + // 解析逗号分隔的数组字符串 + char *arr_str = argv[2]; + char *token = strtok(arr_str, ","); + int i = 0; + while (token != NULL) { - if (i >= n) + if (i >= n) { - fprintf (stderr, "Error: Expected %d array elements but got more.\n", - n); - return 1; + fprintf(stderr, "Error: Expected %d array elements but got more.\n", n); + return 1; } - arr[i++] = atoi (token); - token = strtok (NULL, ","); + arr[i++] = atoi(token); + token = strtok(NULL, ","); } - if (i != n) + if (i != n) { - fprintf (stderr, "Error: Expected %d array elements but got %d.\n", n, - i); - return 1; + fprintf(stderr, "Error: Expected %d array elements but got %d.\n", n, i); + return 1; } - printf ("Sum of array elements is %d\n", calculate_sum (arr, n)); - return 0; + printf("Sum of array elements is %d\n", calculate_sum(arr, n)); + return 0; } + -- Gitee From 68479e9c08e177815dc3d5683e73e1dac9d1c71f Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:52:03 +0000 Subject: [PATCH 13/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-45/main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/exercise-45/main.c b/src/exercise-45/main.c index a438dbc..939a971 100644 --- a/src/exercise-45/main.c +++ b/src/exercise-45/main.c @@ -12,23 +12,23 @@ find_max (int *arr, int n) __asm__ volatile ( // 用于循环计数的寄存器 "li t0, 0\n" - // 将 max 初始化为数组的第一个元素值,保存到t1寄存器 - "PLACEHOLDER\n\t" + // 将 max 初始化为数组的第一个元素值 + "lw t1, 0(%1)\n\t" "loop:\n\t" // 当前元素值保存在t2寄存器 "lw t2, 0(%1)\n\t" // 如果 max >= 当前元素值,则跳转到next标签 - "PLACEHOLDER\n\t" + "bge t1, t2, next\n\t" // 如果 max < 当前元素值,则更新 max 为当前元素值 - "PLACEHOLDER\n\t" + "mv t1, t2\n\t" "next:\n\t" "addi %1, %1, 4\n\t" "addi t0, t0, 1\n\t" // 如果计数器小于数组长度,则继续循环 - "PLACEHOLDER\n\t" + "blt t0, %2, loop\n\t" "end:\n\t" "mv %0, t1\n" @@ -77,3 +77,4 @@ main (int argc, char *argv[]) printf ("Maximum element in array is %d\n", find_max (arr, n)); return 0; } + -- Gitee From 79615dc667550a8d398b2f63a1dc3899e2e171e9 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 14:55:19 +0000 Subject: [PATCH 14/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-47/main.c | 65 +++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/src/exercise-47/main.c b/src/exercise-47/main.c index 9c7dc54..66bab8f 100644 --- a/src/exercise-47/main.c +++ b/src/exercise-47/main.c @@ -2,51 +2,53 @@ #include #include -// RISC-V 汇编内联汇编函数,返回目标值在数组中出现的次数 +// RISC-V 汇编内联汇编函数,用于检查数组是否按非降序排列 int -count_occurrences (int *arr, int n, int target) +is_sorted (int *arr, int n) { - int count = 0; + int result = 1; // 默认为已排序 // >>> 替换 PLACEHOLDER 为合适的指令完成注释的功能 __asm__ volatile ( - // 用于保存目标值出现的次数 - "li t0, 0\n" - // 用于循环计数(索引)的寄存器 - "li t1, 0\n" - - "loop_start:\n" - // 如果索引 index >= n,则跳转到 loop_end - "PLACEHOLDER\n" + // 用于循环计数的寄存器 + "li t0, 1\n\t" + "addi t1, %1, 4\n\t" + "loop:\n\t" // 保存数组当前元素到t2寄存器 - "PLACEHOLDER\n" + "lw t2, 0(%1)\n\t" + // 保存数组下一个元素到t3寄存器 + "lw t3, 0(t1)\n\t" + // 如果当前元素大于下一个元素,数组不是按非降序排列 + "bge t3, t2, sorted\n\t" + "li %0, 0\n\t" + "j end\n\t" - // 对比数组元素与目标值,如果不相等,跳转到not_equal标签 - "PLACEHOLDER\n" - "addi t0, t0, 1\n" + "sorted:\n\t" + // 指针移动到数组下一个元素 + "addi %1, %1, 4\n\t" + // 指针移动到数组下下一个元素 + "addi t1, t1, 4\n\t" - "not_equal:\n" - "addi t1, t1, 1\n" - "addi %1, %1, 4\n" - "j loop_start\n" + "addi t0, t0, 1\n\t" + // 如果计数器小于数组长度,则继续循环 + "blt t0, %2, loop\n\t" - "loop_end:\n" - "mv %0, t0\n" - : "=r"(count) - : "r"(arr), "r"(n), "r"(target) - : "t0", "t1", "t2"); + "end:\n\t" + : "=r"(result) + : "r"(arr), "r"(n) + : "t0", "t1", "t2", "t3"); // <<< - return count; + return result; } int main (int argc, char *argv[]) { - if (argc != 4) + if (argc != 3) { - fprintf (stderr, "Usage: %s \n", argv[0]); + fprintf (stderr, "Usage: %s \n", argv[0]); return 1; } @@ -75,10 +77,9 @@ main (int argc, char *argv[]) return 1; } - int target = atoi (argv[3]); - - int occurrences = count_occurrences (arr, n, target); - printf ("Occurrences of target:%d\n", occurrences); - + if (is_sorted (arr, n)) + printf ("Array is sorted.\n"); + else + printf ("Array is not sorted.\n"); return 0; } -- Gitee From 3af8c4b1cfce6e947ccf082c10f310fae6a37b73 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:00:35 +0000 Subject: [PATCH 15/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-50/fifo.c | 60 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/exercise-50/fifo.c b/src/exercise-50/fifo.c index 8252dd0..e39af96 100644 --- a/src/exercise-50/fifo.c +++ b/src/exercise-50/fifo.c @@ -10,8 +10,60 @@ * 字符串中的每个字符是一个数字,表示一个页面号。 * @param num_frames 页框的数量,表示物理内存中可用的页框数。 */ -void -fifo_page_replacement (char *queue_frames, int num) -{ - // TODO +void fifo_page_replacement(char *queue_frames, int num_frames) { + // 创建一个数组来模拟物理内存中的页框 + int *frames = (int *) malloc(num_frames * sizeof(int)); + if (frames == NULL) { + fprintf(stderr, "内存分配失败。\n"); + return; + } + + // 初始化页框数组,-1 表示页框为空 + for (int i = 0; i < num_frames; ++i) { + frames[i] = -1; + } + + int queue_length = strlen(queue_frames); // 获取页面访问序列的长度 + int frame_index = 0; // 用于指示下一个应该替换的页框 + + printf("使用FIFO算法进行页面置换:\n"); + + // 遍历页面访问序列 + for (int i = 0; i < queue_length; ++i) { + int page = queue_frames[i] - '0'; // 获取当前页面号 + + // 检查当前页面是否已经在物理内存中 + int found = 0; + for (int j = 0; j < num_frames; ++j) { + if (frames[j] == page) { + found = 1; + break; + } + } + + // 如果当前页面不在物理内存中,进行页面置换 + if (!found) { + printf("页面 %d 导致页面错误。\n", page); + + // 替换最早进入的页框 + frames[frame_index] = page; + frame_index = (frame_index + 1) % num_frames; + + // 打印当前页框状态 + printf("当前页框状态: "); + for (int k = 0; k < num_frames; ++k) { + if (frames[k] == -1) + printf("[ ] "); + else + printf("[%d] ", frames[k]); + } + printf("\n"); + } + else { + printf("页面 %d 已经在内存中。\n", page); + } + } + + free(frames); // 释放动态分配的内存 } + -- Gitee From 75ee9d2e51d346ee0072a6fad80d8fa1e41bbb29 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:05:52 +0000 Subject: [PATCH 16/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-51/lru.c | 53 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/src/exercise-51/lru.c b/src/exercise-51/lru.c index 9d2dff6..645e755 100644 --- a/src/exercise-51/lru.c +++ b/src/exercise-51/lru.c @@ -3,15 +3,46 @@ #include #include -/** - * 函数:模拟LRU页面置换算法。 - * - * @param queue_frames 一个字符串,表示页面访问序列。 - * 字符串中的每个字符是一个数字,表示一个页面号。 - * @param num_frames 页框的数量,表示物理内存中可用的页框数。 - */ -void -fifo_page_replacement (char *queue_frames, int num) -{ - // TODO +// 打印当前页框状态 +void print_frames(int access, int *frames, int num) { + printf("Access: %d, Frames: [", access); + for (int i = 0; i < num; ++i) { + printf("%d", frames[i]); + if (i < num - 1) { + printf(", "); + } + } + printf("]\n"); } + +// 查找页面在页框中的位置 +int find_page(int *frames, int num_frames, int page) { + for (int i = 0; i < num_frames; ++i) { + if (frames[i] == page) { + return i; + } + } + return -1; +} + +// 将页面放入页框中 +void put_page(int *frames, int num_frames, int page, int *used_order, int *last_used) { + int idx = used_order[num_frames - 1]; // 最近最少使用的页面在数组中的索引 + frames[idx] = page; // 将页面放入该位置 + *last_used += 1; // 更新最近使用的次序 + used_order[num_frames - 1] = *last_used; // 更新使用次序数组 + for (int i = num_frames - 1; i > 0; --i) { + if (used_order[i] > used_order[i - 1]) { // 交换使得数组有序 + int tmp = used_order[i]; + used_order[i] = used_order[i - 1]; + used_order[i - 1] = tmp; + } else { + break; + } + } +} + +// LRU页面置换算法 +void lru_page_replacement(char *queue_frames, int num_frames) { + int *frames = (int *)malloc(num_frames * sizeof(int)); // 分配一个空间的numframes + First data -- Gitee From a5b82a05bcf199b3d0d63a2116ce847d51b44f12 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:08:06 +0000 Subject: [PATCH 17/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-52/main.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/exercise-52/main.c b/src/exercise-52/main.c index 2fa52e3..5518e36 100644 --- a/src/exercise-52/main.c +++ b/src/exercise-52/main.c @@ -1,8 +1,17 @@ #include #include -int -main () -{ - // TODO +int main() { + char hostname[256]; // 定义一个足够大的字符数组来存储主机名 + + // 获取主机名 + if (gethostname(hostname, sizeof(hostname)) == 0) { + // 输出主机名到标准输出 + printf("%s\n", hostname); + } else { + fprintf(stderr, "Failed to get hostname\n"); + return 1; + } + + return 0; } -- Gitee From 2904e78054e595b3ddd1d755f9de7679b54c3e6f Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:09:37 +0000 Subject: [PATCH 18/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-53/v2p.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/exercise-53/v2p.c b/src/exercise-53/v2p.c index e64078c..9b2bd82 100644 --- a/src/exercise-53/v2p.c +++ b/src/exercise-53/v2p.c @@ -1,6 +1,7 @@ #include "v2p.h" #include #include + // 模拟页表(在真实情况下,这会在内存中) uint32_t page_table[] = { 0x20000000, // 第一个虚拟页映射到物理页帧基地址 0x20000000 @@ -12,5 +13,17 @@ uint32_t page_table[] = { uint32_t translate_address (uint32_t virtual_address) { - // TODO + // 页表项大小,这里假设每个页大小为4KB,即2^12 + uint32_t page_size = 0x1000; // 4KB = 2^12 + + // 获取虚拟页号 + uint32_t virtual_page_number = virtual_address / page_size; + + // 查找页表,获取物理页帧基地址 + uint32_t physical_base_address = page_table[virtual_page_number]; + + // 计算物理地址 + uint32_t physical_address = physical_base_address + (virtual_address % page_size); + + return physical_address; } -- Gitee From 57fdd358b5f7954e053bfc2933d44d0b31d393ac Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:11:25 +0000 Subject: [PATCH 19/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-54/RR.c | 62 ++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/exercise-54/RR.c b/src/exercise-54/RR.c index 1ad1e6c..06b0857 100644 --- a/src/exercise-54/RR.c +++ b/src/exercise-54/RR.c @@ -1,21 +1,49 @@ -#include "RR.h" -#include -#include - -/** - * 函数: calculateTimes - * 功能: 根据时间片轮转调度算法计算每个进程的完成时间、周转时间和等待时间 - * - * 参数: - * - processes: 指向包含多个进程信息的数组 - * - n: 进程数量 - * - time_slice: 时间片长度 - * - * 描述: - * 计算进程的完成时间,周转时间和等待时间并存在结构体中即可 - */ void calculateTimes (Process *processes, int n, int time_slice) { - // TODO + int remaining_time[n]; // 用于跟踪每个进程的剩余执行时间 + + // 初始化剩余执行时间和其他计算结果为0 + for (int i = 0; i < n; i++) { + remaining_time[i] = processes[i].burst_time; + processes[i].completion_time = 0; + processes[i].turnaround_time = 0; + processes[i].waiting_time = 0; + } + + int current_time = 0; // 当前时间 + + // 循环执行调度 + while (1) { + int all_finished = 1; // 所有进程是否完成的标志 + + for (int i = 0; i < n; i++) { + if (remaining_time[i] > 0) { + all_finished = 0; // 还有进程未完成 + + if (remaining_time[i] > time_slice) { + // 执行一个时间片的工作量 + current_time += time_slice; + remaining_time[i] -= time_slice; + } else { + // 执行剩余的工作量 + current_time += remaining_time[i]; + processes[i].completion_time = current_time; + processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time; + processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time; + remaining_time[i] = 0; // 进程完成 + } + } + } + + if (all_finished) // 所有进程都已经完成 + break; + } + + // 输出每个进程的完成时间、周转时间和等待时间 + printf("Process\tCompletion Time\tTurnaround Time\tWaiting Time\n"); + for (int i = 0; i < n; i++) { + printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].completion_time, + processes[i].turnaround_time, processes[i].waiting_time); + } } -- Gitee From 33d7b16650aeffdae91891bef4f64a8a50a33529 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:15:34 +0000 Subject: [PATCH 20/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-55/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/exercise-55/main.c b/src/exercise-55/main.c index ced1843..6b1c3dd 100644 --- a/src/exercise-55/main.c +++ b/src/exercise-55/main.c @@ -10,10 +10,14 @@ void set_memory_limit (rlim_t memory_limit) { struct rlimit rl; - // TODO 设置进程的内存限制 - // TODO 使用 setrlimit 函数设置进程的资源限制 + rl.rlim_cur = rl.rlim_max = memory_limit; + if (setrlimit (RLIMIT_AS, &rl) == -1) + { + perror ("setrlimit failed"); + exit (EXIT_FAILURE); + } } void @@ -65,4 +69,4 @@ main () run_child_process (memory_limit, program_path); return 0; -} +} \ No newline at end of file -- Gitee From 3838bbff1f81bcffe85ff36679a788e406fabf93 Mon Sep 17 00:00:00 2001 From: Ruby_Facetious_a005 <1621059562@qq.com> Date: Fri, 14 Jun 2024 15:20:32 +0000 Subject: [PATCH 21/21] Junyi Signed-off-by: Ruby_Facetious_a005 <1621059562@qq.com> --- src/exercise-46/main.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/exercise-46/main.c b/src/exercise-46/main.c index c9a6706..fc8673e 100644 --- a/src/exercise-46/main.c +++ b/src/exercise-46/main.c @@ -3,35 +3,27 @@ #include // RISC-V 汇编内联汇编函数,用于检查数组是否按非降序排列 -int -is_sorted (int *arr, int n) +// RISC-V 汇编内联汇编函数,用于检查数组是否按非降序排列 +int is_sorted(int *arr, int n) { int result = 1; // 默认为已排序 - // >>> 替换 PLACEHOLDER 为合适的指令完成注释的功能 __asm__ volatile ( - // 用于循环计数的寄存器 "li t0, 1\n\t" "addi t1, %1, 4\n\t" "loop:\n\t" - // 保存数组当前元素到t2寄存器 "PLACEHOLDER\n\t" - // 保存数组下一个元素到t3寄存器 "PLACEHOLDER\n\t" - // 如果当前元素大于下一个元素,数组不是按非降序排列 "bge t3, t2, sorted\n\t" "li %0, 0\n\t" "j end\n\t" "sorted:\n\t" - // 指针移动到数组下一个元素 "PLACEHOLDER\n\t" - // 指针移动到数组下下一个元素 "PLACEHOLDER\n\t" "addi t0, t0, 1\n\t" - // 如果计数器小于数组长度,则继续循环 "PLACEHOLDER\n\t" "end:\n\t" @@ -39,7 +31,6 @@ is_sorted (int *arr, int n) : "r"(arr), "r"(n) : "t0", "t1", "t2", "t3"); - // <<< return result; } -- Gitee