作业调度算法


前言

使用C语言设计采用先来先服务算法、运行时间最短者优先算法和最高响应比优先调度算法模拟设计作业调度程序。

一、

二、使用步骤

1.引入库

代码如下(示例):

#include <stdio.h>
#include <stdlib.h>

struct Job {
    char name[20];
    float arrival_time;
    float completion_time;
};

void firstComeFirstServe(struct Job jobs[], int n) {
    float current_time = 0;
    printf("先来先服务\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
    for (int i = 0; i < n; i++) {
        if (jobs[i].arrival_time > current_time) {
            current_time = jobs[i].arrival_time;
        }
        float start_time = current_time;
        float end_time = start_time + jobs[i].completion_time;
        float turnaround_time = end_time - jobs[i].arrival_time;
        float turnaround_ratio = turnaround_time / jobs[i].completion_time;

        printf("\t %s   \t%.2f   \t%.2f   \t%.2f    \t%.2f\n",
            jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);

        current_time = end_time;
    }
    printf("-----------------------------\n");
}

void shortestJobFirst(struct Job jobs[], int n) {
    float current_time = 0;
    int* completed = (int*)malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        completed[i] = 0;
    }
    printf("短作业优先\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
    while (1) {
        int shortest_job = -1;
        float shortest_time = -1;

        for (int i = 0; i < n; i++) {
            if (jobs[i].arrival_time <= current_time && !completed[i] && (shortest_job == -1 || jobs[i].completion_time < shortest_time)) {
                shortest_job = i;
                shortest_time = jobs[i].completion_time;
            }
        }

        if (shortest_job != -1) {
            int i = shortest_job;
            completed[i] = 1;
            float start_time = current_time;
            float end_time = start_time + jobs[i].completion_time;
            float turnaround_time = end_time - jobs[i].arrival_time;
            float turnaround_ratio = turnaround_time / jobs[i].completion_time;

            printf("\t %s   \t%.2f   \t%.2f   \t%.2f    \t%.2f\n",
                jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);

            current_time = end_time;
        }
        else {
            int all_completed = 1;
            for (int i = 0; i < n; i++) {
                if (!completed[i]) {
                    all_completed = 0;
                    break;
                }
            }
            if (all_completed) {
                break;
            }
            else {
                current_time++;
            }
        }
    }
    printf("-----------------------------\n");
  
}

void highestResponseRatioNext(struct Job jobs[], int n) {
    float current_time = 0;
    int* completed = (int*)malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        completed[i] = 0;
    }
    printf("最高响应比\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
    while (1) {
        int highest_response_job = -1;
        float highest_response_ratio = -1;

        for (int i = 0; i < n; i++) {
            if (!completed[i] && jobs[i].arrival_time <= current_time) {
                float waiting_time = current_time - jobs[i].arrival_time;
                float response_ratio = (waiting_time + jobs[i].completion_time) / jobs[i].completion_time;
                if (highest_response_job == -1 || response_ratio > highest_response_ratio) {
                    highest_response_job = i;
                    highest_response_ratio = response_ratio;
                }
            }
        }

        if (highest_response_job != -1) {
            int i = highest_response_job;
            completed[i] = 1;
            float start_time = current_time;
            float end_time = start_time + jobs[i].completion_time;
            float turnaround_time = end_time - jobs[i].arrival_time;
            float turnaround_ratio = turnaround_time / jobs[i].completion_time;

            printf("\t %s   \t%.2f   \t%.2f   \t%.2f    \t%.2f\n",
                jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);

            current_time = end_time;
        }
        else {
            int all_completed = 1;
            for (int i = 0; i < n; i++) {
                if (!completed[i]) {
                    all_completed = 0;
                    break;
                }
            }
            if (all_completed) {
                break;
            }
            else {
                current_time++;
            }
        }
    }
    free(completed);
    printf("-----------------------------\n");
}

int main() {
    int n;
    printf("输入作业数量: ");
    scanf_s("%d", &n);

    struct Job* jobs = (struct Job*)malloc(n * sizeof(struct Job));
    if (jobs == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    for (int i = 0; i < n; i++) {
        printf("输入作业名, 抵达时间, 运行时间 %d: ", i + 1);
        scanf_s("%s %f %f", jobs[i].name, (unsigned)_countof(jobs[i].name), &jobs[i].arrival_time, &jobs[i].completion_time);
    }

    int choice;
    printf("-----------------------------\n");
    while (1) {
        printf("选择算法:\n");
        printf("1. 先来先服务\n");
        printf("2. 短作业优先\n");
        printf("3.最高响应比\n");
        printf("0. 重新输入\n");
        printf("Enter 'end' to exit\n");
        printf("-----------------------------\n");
        scanf_s("%d", &choice);
       
        switch (choice) {
        case 1:
            firstComeFirstServe(jobs, n);
            break;
        case 2:
            shortestJobFirst(jobs, n);
            break;
        case 3:
            highestResponseRatioNext(jobs, n);
            break;
        case 0:
            main(); // Restart data input
            break;
        default:
            free(jobs);
            return 0; // Exit if invalid choice
        }
    }
   
    free(jobs);
    return 0;
}

3总结

作业管理是用户与操作系统的接口。作业调度的主要功能是检查系统是否能满足用户作业的资源要求以及按照一定的算法选取作业。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值