zoukankan      html  css  js  c++  java
  • CPT104-lab11-Shortest-Job-First (SJF) scheduling algorithm

    结构体自定义排序函数,调用stdlib.h库的系统快速排序qsort

     1 //sjf non-preemptive with same arrival time
     2 
     3 #include<stdio.h>
     4 #include<stdlib.h>
     5 #define N 5010
     6 
     7 struct process {
     8     int pid;
     9     int bt;
    10     int wt;
    11     int tat;
    12 };
    13 
    14 int cmp(const void* a, const void* b) {
    15     struct process c = *(struct process*)a;
    16     struct process d = *(struct process*)b;
    17     return c.bt < d.bt ? 0 : 1;
    18 }
    19 
    20 void main() {
    21     puts("Enter total number of processes");
    22     int n;
    23     scanf("%d", &n);
    24     puts("Enter process-id and burst-time for each process
    ");
    25     struct process p[N];
    26     for (int i = 0; i < n; i++) scanf("%d%d", &p[i].pid, &p[i].bt);
    27     puts("Processes after SJF scheduling...
    ");
    28     qsort(p, n, sizeof(p[0]), cmp);
    29     p[0].wt = 0;
    30     for (int i = 1; i < n; i++) p[i].wt += p[i - 1].bt + p[i - 1].wt;
    31     for (int i = 0; i < n; i++) p[i].tat = p[i].bt + p[i].wt;
    32     for (int i = 0; i < n; i++) {
    33         printf("Process %d
    ", p[i].pid);
    34         printf("- burst time = %d, waiting time = %d, turnaround time = %d
    
    ", p[i].bt, p[i].wt, p[i].tat);
    35     }
    36     int twt = 0, ttat = p[0].bt;
    37     for (int i = 1; i < n; i++) {
    38         twt = twt + p[i].wt;
    39         ttat = ttat + p[i].tat;
    40     }
    41     float awt = (float)twt / n;
    42     float atat = (float)ttat / n;
    43     printf("Average waiting time = %4.2f
    ", awt);
    44     printf("Average turnaround time = %4.2f
    ", atat);
    45 }
    ~~Jason_liu O(∩_∩)O
  • 相关阅读:
    创建windows服务
    Scrum演练(2)
    数据结构链表创建与输出
    C++ XML解析之tinyXML
    解决VS2010启动速度死慢的问题
    标准C++中string类的用法总结
    模版参数编译时检查方法,利用typedef
    static的作用
    数据结构实现链表的反转
    字节对齐
  • 原文地址:https://www.cnblogs.com/JasonCow/p/14799867.html
Copyright © 2011-2022 走看看