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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/wait.h> #include <signal.h>
/** * 后台作业进程 */ void background_job(int job_id) { printf("后台作业 %d 启动 (PID: %d)\n", job_id, getpid()); // 创建独立的进程组 if (setpgid(0, 0) == 0) { printf("后台作业 %d 成功创建进程组\n", job_id); } else { printf("后台作业 %d 创建进程组失败: %s\n", job_id, strerror(errno)); } // 模拟长时间运行的作业 for (int i = 0; i < 20; i++) { printf("后台作业 %d 运行中... (%d/20)\n", job_id, i + 1); sleep(1); // 检查是否收到终止信号 if (i == 10) { // 模拟作业暂停和恢复 printf("后台作业 %d 暂停工作\n", job_id); sleep(2); printf("后台作业 %d 恢复工作\n", job_id); } } printf("后台作业 %d 完成\n", job_id); exit(0); }
/** * 作业控制管理器 */ typedef struct { pid_t pid; int job_id; int status; // 0:运行, 1:暂停, 2:完成 char command[256]; } job_t;
/** * 显示作业列表 */ void show_job_list(job_t *jobs, int count) { printf("\n=== 作业列表 ===\n"); printf("%-5s %-8s %-10s %s\n", "作业ID", "进程ID", "状态", "命令"); printf("----------------------------------------\n"); for (int i = 0; i < count; i++) { const char *status_str; switch (jobs[i].status) { case 0: status_str = "运行"; break; case 1: status_str = "暂停"; break; case 2: status_str = "完成"; break; default: status_str = "未知"; break; } printf("%-5d %-8d %-10s %s\n", jobs[i].job_id, jobs[i].pid, status_str, jobs[i].command); } printf("\n"); }
/** * 演示作业控制 */ int demo_job_control() { job_t jobs[3]; int job_count = 0; printf("=== 作业控制演示 ===\n"); // 创建后台作业 printf("1. 创建后台作业:\n"); for (int i = 0; i < 3; i++) { pid_t job_pid = fork(); if (job_pid == 0) { background_job(i + 1); } else if (job_pid > 0) { // 父进程记录作业信息 jobs[job_count].pid = job_pid; jobs[job_count].job_id = i + 1; jobs[job_count].status = 0; // 运行中 snprintf(jobs[job_count].command, sizeof(jobs[job_count].command), "background_job_%d", i + 1); job_count++; printf(" 创建后台作业 %d: PID=%d\n", i + 1, job_pid); // 将子进程加入独立进程组 if (setpgid(job_pid, job_pid) == 0) { printf(" 作业 %d 成功创建独立进程组\n", i + 1); } } else { perror("创建后台作业失败"); } } // 显示初始作业列表 show_job_list(jobs, job_count); // 演示向特定作业发送信号 printf("2. 向作业发送信号:\n"); if (job_count > 0) { printf(" 向作业 1 (PID=%d) 发送 SIGUSR1 信号\n", jobs[0].pid); if (kill(jobs[0].pid, SIGUSR1) == 0) { printf(" ✓ 信号发送成功\n"); } else { printf(" ✗ 信号发送失败: %s\n", strerror(errno)); } } // 等待一段时间 printf("\n3. 等待作业运行...\n"); sleep(5); // 显示当前作业状态 show_job_list(jobs, job_count); // 演示作业控制操作 printf("4. 作业控制操作:\n"); // 暂停一个作业 if (job_count > 1) { printf(" 暂停作业 2 (PID=%d)\n", jobs[1].pid); if (kill(-getpgid(jobs[1].pid), SIGSTOP) == 0) { // 发送给整个进程组 jobs[1].status = 1; // 暂停 printf(" ✓ 作业 2 已暂停\n"); } else { printf(" ✗ 暂停作业 2 失败: %s\n", strerror(errno)); } } // 显示更新后的作业列表 show_job_list(jobs, job_count); // 恢复暂停的作业 if (job_count > 1) { printf(" 恢复作业 2 (PID=%d)\n", jobs[1].pid); if (kill(-getpgid(jobs[1].pid), SIGCONT) == 0) { jobs[1].status = 0; // 运行中 printf(" ✓ 作业 2 已恢复\n"); } else { printf(" ✗ 恢复作业 2 失败: %s\n", strerror(errno)); } } // 等待所有作业完成 printf("\n5. 等待所有作业完成:\n"); int completed_jobs = 0; while (completed_jobs < job_count) { int status; pid_t finished_pid = waitpid(-1, &status, WNOHANG); if (finished_pid > 0) { // 找到完成的作业并更新状态 for (int i = 0; i < job_count; i++) { if (jobs[i].pid == finished_pid) { jobs[i].status = 2; // 完成 printf(" 作业 %d (PID=%d) 已完成\n", jobs[i].job_id, finished_pid); completed_jobs++; break; } } } else if (finished_pid == 0) { // 没有作业完成,短暂等待 usleep(100000); // 100ms } else { // 错误 if (errno != ECHILD) { perror("等待作业完成时出错"); } break; } } // 显示最终作业列表 show_job_list(jobs, job_count); printf("作业控制演示完成\n"); return 0; }
int main() { return demo_job_control(); }
|