diff --git a/src/exercise-22/matrix_mul.c b/src/exercise-22/matrix_mul.c index 0752395ce7d9d15499ee37c49401d98b8fb86ccb..19b594e730aa1180d7b0132172a11f3f2feaa5a7 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; } @@ -35,3 +43,4 @@ printMatrix (int **matrix, int rows, int cols) printf ("\n"); } } + \ No newline at end of file diff --git a/src/exercise-23/convolution.c b/src/exercise-23/convolution.c index f86ec9e925c69da3fb1893ce743122c5785b4dd0..753cf3dc4f3f73ad9c23676b6716899b1079ca1f 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]; + } + } + } + } +} } diff --git a/src/exercise-24/matrix_trans.c b/src/exercise-24/matrix_trans.c index 9cdf524559a987366e169a378622337cf3df2210..5f78e6de960ddf5a9922e41911020c359c565775 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 diff --git a/src/exercise-25/matrix_zero.c b/src/exercise-25/matrix_zero.c index ba1e8c2fcea32c88d435ecdf0ac941a97f578c02..2dade1c3a46a76ada05a9faa14731ae189f6f419 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 diff --git a/src/exercise-26/matrix_kmin.c b/src/exercise-26/matrix_kmin.c index 635e08d4e30ac133c25da55accd5c7d2ecc41660..a1718574ab07b351716d89c845e2f420f9905886 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 diff --git a/src/exercise-36/max_value.c b/src/exercise-36/max_value.c index 1a97fefc4d040a833b626b207444a67bc22519de..071dd43af84adf8d33dfbae5313bbf06a3787963 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) { diff --git a/src/exercise-37/timer.c b/src/exercise-37/timer.c index 107c93f39ea6b730948318e376cbb59a3d9bf63a..c7de8b6ae08b6bf83d710f1732026463fc931e00 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; } diff --git a/src/exercise-38/chardev.c b/src/exercise-38/chardev.c index 84250dc1b9282abc884cd987e19371b52678e142..48b8cd1819c03c7eb985d2e49d4a590aed2c141f 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 diff --git a/src/exercise-39/fileop.c b/src/exercise-39/fileop.c index 529ae524203589286a824c32c49350563b62dcfd..c8a787df77674dbbe617cd4a5d26ffb0a5d55b7f 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; } diff --git a/src/exercise-43/main.c b/src/exercise-43/main.c index 90f6ee2f2f4a85e5d23e96901e1751dccbc27504..055e9ea2004109110074e7a05ac2193750b7492c 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; } diff --git a/src/exercise-44/main.c b/src/exercise-44/main.c index 32db751b2691ab7c579b1f4b782d8a0b3341e4f5..df364a8ca990aa5c960bbf6701fc7c11ee5390c2 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; } + diff --git a/src/exercise-45/main.c b/src/exercise-45/main.c index a438dbc06863fae1907b2a42a939898f7084095e..939a971c3fd16aa64a3432c9a77cb01db81395eb 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; } + diff --git a/src/exercise-46/main.c b/src/exercise-46/main.c index c9a6706d2d08c12a3189605ded3db5fcd17d2f21..fc8673e6325dadf8d9e4e1be49ec2923f17748b5 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; } diff --git a/src/exercise-47/main.c b/src/exercise-47/main.c index 9c7dc542167b53ea8d200df337993d0af093047a..66bab8f2e05869a71749881d4cd6544c81c6ea34 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; } diff --git a/src/exercise-50/fifo.c b/src/exercise-50/fifo.c index 8252dd02f03c590e6a1aa633e427a10ffe1be992..e39af96689ddb9d7fcc79ea6bffbf20243f1a070 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); // 释放动态分配的内存 } + diff --git a/src/exercise-51/lru.c b/src/exercise-51/lru.c index 9d2dff6adaab01bde2f5c46adb066b590b146ed7..645e7557375204661ae0359d937c6a91e0f1a659 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 diff --git a/src/exercise-52/main.c b/src/exercise-52/main.c index 2fa52e3cf6759586570cb0d5cb62e339a1051035..5518e364f09e0c8c049b80eb840467f144cd839d 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; } diff --git a/src/exercise-53/v2p.c b/src/exercise-53/v2p.c index e64078c271a33bbd7674d39aecea372a983eac51..9b2bd821c94fea8d3af5b61e08866f0d8e823fb6 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; } diff --git a/src/exercise-54/RR.c b/src/exercise-54/RR.c index 1ad1e6ce3d69d661925b72f074bae1f2cd4210a6..06b08572c2d1d9165a90d85ced7c39ab93723f8f 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); + } } diff --git a/src/exercise-55/main.c b/src/exercise-55/main.c index ced18430d3643c3250d8e2754d2e6a570a15edf3..6b1c3dd051564145219ad45725fbd82cb525d697 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