在Linux上编写C语言,实现从键盘输入时间片长度、任务个数、每一个任务的到达时间及服务时间,然后构造相应的进程并按时间片轮转调度算法对所有进程进行调度,进程运行情况可以输出到终端
时间: 2024-02-29 18:52:28 浏览: 174
这是一个比较复杂的问题,需要涉及到进程创建、时间片轮转调度算法的实现、以及进程运行情况的输出等多个方面。下面是一个简单的参考实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define MAX_TASKS 10 //最大任务数
#define MAX_BUF_SIZE 1024 //最大缓冲区大小
#define TIME_QUANTUM 1000 //默认时间片长度
struct task {
int pid; //进程ID
int arrival_time; //到达时间
int service_time; //服务时间
int remaining_time; //剩余服务时间
int waited_time; //等待时间
};
struct task tasks[MAX_TASKS];
int num_tasks = 0;
int time_quantum = TIME_QUANTUM;
int current_task = -1;
int timer_interrupted = 0;
int total_wait_time = 0;
int total_run_time = 0;
void handle_timer_interrupt(int sig) {
timer_interrupted = 1;
}
int create_task(int arrival_time, int service_time) {
if (num_tasks >= MAX_TASKS) {
printf("Too many tasks.\n");
return -1;
}
int pid = fork();
if (pid == -1) {
printf("Error creating task.\n");
return -1;
} else if (pid == 0) { //子进程
printf("Task %d created.\n", getpid());
sleep(arrival_time);
int i = 0;
while (i < service_time) { //模拟任务执行
printf("Task %d running (%d/%d).\n", getpid(), i + 1, service_time);
usleep(time_quantum);
i += time_quantum;
}
printf("Task %d completed.\n", getpid());
exit(0);
} else { //父进程
tasks[num_tasks].pid = pid;
tasks[num_tasks].arrival_time = arrival_time;
tasks[num_tasks].service_time = service_time;
tasks[num_tasks].remaining_time = service_time;
tasks[num_tasks].waited_time = 0;
num_tasks++;
return 0;
}
}
void schedule_task() {
if (current_task != -1) {
if (tasks[current_task].remaining_time == 0) { //当前任务已完成
printf("Task %d finished.\n", tasks[current_task].pid);
total_wait_time += tasks[current_task].waited_time;
total_run_time += tasks[current_task].service_time;
current_task = -1;
} else if (timer_interrupted) { //时间片用完,需要切换任务
tasks[current_task].remaining_time -= time_quantum;
tasks[current_task].waited_time += time_quantum;
printf("Task %d interrupted.\n", tasks[current_task].pid);
current_task = (current_task + 1) % num_tasks;
timer_interrupted = 0;
}
} else { //当前没有任务在执行,需要选取一个任务开始执行
current_task = 0;
}
}
int main(int argc, char** argv) {
//读取输入参数
char buf[MAX_BUF_SIZE];
printf("Enter time quantum (ms): ");
fgets(buf, MAX_BUF_SIZE, stdin);
time_quantum = atoi(buf);
printf("Enter number of tasks: ");
fgets(buf, MAX_BUF_SIZE, stdin);
num_tasks = atoi(buf);
for (int i = 0; i < num_tasks; i++) {
printf("Enter arrival time and service time for task %d: ", i + 1);
fgets(buf, MAX_BUF_SIZE, stdin);
int arrival_time, service_time;
sscanf(buf, "%d %d", &arrival_time, &service_time);
create_task(arrival_time, service_time);
}
//设置定时器,用于实现时间片轮转调度算法
struct sigaction sa;
sa.sa_handler = handle_timer_interrupt;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, NULL);
struct itimerval timer;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = time_quantum * 1000;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = time_quantum * 1000;
setitimer(ITIMER_REAL, &timer, NULL);
//执行调度
while (1) {
schedule_task();
if (current_task == -1 && total_run_time == 0) { //所有任务都已完成
printf("All tasks completed.\n");
break;
}
}
//输出运行情况统计信息
float avg_wait_time = (float)total_wait_time / num_tasks;
float avg_run_time = (float)total_run_time / num_tasks;
printf("Total wait time: %d ms\n", total_wait_time);
printf("Total run time: %d ms\n", total_run_time);
printf("Average wait time: %.2f ms\n", avg_wait_time);
printf("Average run time: %.2f ms\n", avg_run_time);
return 0;
}
```
注意:上述代码仅供参考,可能存在一些问题,如进程创建失败、定时器无法正常工作等。在实际应用中,需要进行更完善的错误处理和测试。
阅读全文