• 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
  • 相关阅读:
    OPENCV图像变换-1
    OPENCV形态学操作1
    OPENCV基本滤波算法
    OSX下编译安装opencv3.1.0与opencv_contrib_master
    iOS8学习笔记-构建多视图应用程序
    iOS8学习笔记2--autolayout
    iOS学习笔记1--在xcode6以上的版本中不使用storyboard以及部分控件使用
    Objective-c学习笔记3
    objective-c学习笔记2
    objective-c学习笔记
  • 原文地址:https://www.cnblogs.com/JasonCow/p/14799867.html
Copyright © 2020-2023  润新知