1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <sys/resource.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/wait.h> #include <time.h>
void cpu_intensive_work(int worker_id, int priority, int duration_seconds) { pid_t pid = getpid(); clock_t start_time = clock(); clock_t work_duration = duration_seconds * CLOCKS_PER_SEC; printf("CPU密集型工作进程 %d 启动 (PID: %d, 优先级: %d)\n", worker_id, pid, priority); if (setpriority(PRIO_PROCESS, 0, priority) != 0) { printf("工作进程 %d 设置优先级失败: %s\n", worker_id, strerror(errno)); } volatile long sum = 0; long iterations = 0; while ((clock() - start_time) < work_duration) { sum += iterations; iterations++; if (iterations % 10000000 == 0) { double elapsed = (double)(clock() - start_time) / CLOCKS_PER_SEC; printf("工作进程 %d: 迭代 %ld 次, 耗时 %.2f 秒\n", worker_id, iterations, elapsed); } } double total_time = (double)(clock() - start_time) / CLOCKS_PER_SEC; printf("工作进程 %d 完成:\n", worker_id); printf(" 总迭代次数: %ld\n", iterations); printf(" 总耗时: %.2f 秒\n", total_time); printf(" 平均迭代速度: %.0f 次/秒\n", iterations / total_time); exit(0); }
int demo_priority_scheduling() { pid_t workers[3]; int priorities[] = {-10, 0, 10}; const char *priority_names[] = {"高优先级", "中优先级", "低优先级"}; int worker_count = 3; int work_duration = 10; printf("=== 优先级调度效果演示 ===\n"); printf("工作时长: %d 秒\n", work_duration); printf("优先级设置: 高(-10), 中(0), 低(10)\n\n"); uid_t uid = getuid(); printf("权限检查:\n"); if (uid == 0) { printf(" ✓ 具有root权限,可以设置所有优先级\n"); } else { printf(" ✗ 没有root权限,可能无法设置负优先级\n"); } printf("\n"); printf("创建 %d 个工作进程:\n", worker_count); for (int i = 0; i < worker_count; i++) { workers[i] = fork(); if (workers[i] == 0) { cpu_intensive_work(i + 1, priorities[i], work_duration); } else if (workers[i] > 0) { printf(" %s工作进程 %d: PID=%d, 优先级=%d\n", priority_names[i], i + 1, workers[i], priorities[i]); } else { perror("创建工作进程失败"); for (int j = 0; j < i; j++) { kill(workers[j], SIGKILL); } return -1; } } printf("\n所有进程初始优先级:\n"); for (int i = 0; i < worker_count; i++) { errno = 0; int prio = getpriority(PRIO_PROCESS, workers[i]); if (prio != -1 || errno == 0) { printf(" 进程 %d (PID=%d): 优先级=%d\n", i + 1, workers[i], prio); } else { printf(" 进程 %d (PID=%d): 获取优先级失败 (%s)\n", i + 1, workers[i], strerror(errno)); } } printf("\n等待工作进程完成 (约 %d 秒):\n", work_duration); int status; pid_t finished_pid; int finished_count = 0; time_t start_wait = time(NULL); while (finished_count < worker_count) { finished_pid = wait(&status); if (finished_pid > 0) { finished_count++; time_t elapsed = time(NULL) - start_wait; printf(" 工作进程 (PID=%d) 已完成,耗时 %ld 秒,退出状态: %d\n", finished_pid, elapsed, WEXITSTATUS(status)); } else if (finished_pid == -1) { if (errno == EINTR) { continue; } else { break; } } } printf("\n=== 调度效果分析 ===\n"); printf("高优先级进程应该获得更多的CPU时间\n"); printf("低优先级进程应该获得较少的CPU时间\n"); printf("实际效果取决于系统负载和调度器实现\n"); printf("\n=== 优先级使用建议 ===\n"); printf("1. 优先级范围: -20 (最高) 到 19 (最低)\n"); printf("2. root权限可以设置负优先级\n"); printf("3. 普通用户只能设置0到19的优先级\n"); printf("4. 过高的优先级可能影响系统稳定性\n"); printf("5. 合理使用优先级平衡系统资源\n"); return 0; }
int main() { return demo_priority_scheduling(); }
|