Browse Source

week6

master
RinRi 1 year ago
parent
commit
89d7b9667f
10 changed files with 228 additions and 0 deletions
  1. +51
    -0
      week06/ex1.c
  2. BIN
      week06/ex1.out
  3. +4
    -0
      week06/ex1.sh
  4. +63
    -0
      week06/ex2.c
  5. BIN
      week06/ex2.out
  6. +4
    -0
      week06/ex2.sh
  7. +3
    -0
      week06/ex2.txt
  8. +99
    -0
      week06/ex3.c
  9. BIN
      week06/ex3.out
  10. +4
    -0
      week06/ex3.sh

+ 51
- 0
week06/ex1.c View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int id, at, bt, et, tat, wt;
} Process;

int proc_cmp(const void *a, const void *b) {
return ((Process*)a)->at - ((Process*)b)->at;
}

int proc_cmp_id(const void *a, const void *b) {
return ((Process*)a)->id - ((Process*)b)->id;
}

int main() {
int n;
printf("Number of processes: ");
scanf("%d", &n);
Process p[n];

printf("Arrival time and Burst time:\n");
for (int i = 0; i < n; ++i) {
scanf("%d %d", &p[i].at, &p[i].bt);
p[i].id = i;
}

qsort(p, n, sizeof(Process), proc_cmp);

int curt = 0;
printf("ID\tAT\tBT\tET\tTAT\tWT\n");
double avg_tat = 0, avg_wt = 0;
for (int i = 0; i < n; ++i) {
p[i].et = (curt > p[i].at ? curt : p[i].at) + p[i].bt;
curt = p[i].et;

p[i].tat = p[i].et - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
avg_tat += p[i].tat;
avg_wt += p[i].wt;
}

qsort(p, n, sizeof(Process), proc_cmp_id);
for (int i = 0; i < n; ++i) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].et, p[i].tat, p[i].wt);
}

avg_tat /= n;
avg_wt /= n;
printf("avg:\t\t\t\t%lg\t%lg\n", avg_tat, avg_wt);
}

BIN
week06/ex1.out View File


+ 4
- 0
week06/ex1.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex1.c -o ex1.out
./ex1.out

+ 63
- 0
week06/ex2.c View File

@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef struct {
int id, at, bt, et, tat, wt;
} Process;

int proc_cmp(const void *a, const void *b) {
return ((Process*)a)->bt - ((Process*)b)->bt;
}

int proc_cmp_id(const void *a, const void *b) {
return ((Process*)a)->id - ((Process*)b)->id;
}

int main() {
int n, mn = INT_MAX, curt = 0, jd = 0;
printf("Number of processes: ");
scanf("%d", &n);
Process p[n];

printf("Arrival time and Burst time:\n");
for (int i = 0; i < n; ++i) {
scanf("%d %d", &p[i].at, &p[i].bt);
p[i].id = i;
p[i].et = -1;
mn = (mn < p[i].at ? mn : p[i].at);
}

qsort(p, n, sizeof(Process), proc_cmp);

printf("ID\tAT\tBT\tET\tTAT\tWT\n");

double avg_tat = 0, avg_wt = 0;
while (jd < n) {
mn = INT_MAX;
for (int i = 0; i < n; ++i) {
if (p[i].et == -1)
mn = (mn < p[i].at ? mn : p[i].at);
if (p[i].at <= (mn > curt ? mn : curt) && p[i].et == -1) {
jd++;
p[i].et = (curt > p[i].at ? curt : p[i].at) + p[i].bt;
curt = p[i].et;
p[i].tat = p[i].et - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
avg_tat += p[i].tat;
avg_wt += p[i].wt;
break;
}
}
curt = (curt > mn ? curt : mn);
}

qsort(p, n, sizeof(Process), proc_cmp_id);
for (int i = 0; i < n; ++i) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].et, p[i].tat, p[i].wt);
}

avg_tat /= n;
avg_wt /= n;
printf("avg:\t\t\t\t%lg\t%lg\n", avg_tat, avg_wt);
}

BIN
week06/ex2.out View File


+ 4
- 0
week06/ex2.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex2.c -o ex2.out
./ex2.out

+ 3
- 0
week06/ex2.txt View File

@@ -0,0 +1,3 @@
The shortest job first algorithm is definitely more fair compared to FCFS algorithm.
The reason is obvious: the FCFS algorithm does care about only one thing - arival time,
which doesn't mean a lot to 'fairness' of a scheduler algorithm. As fairness, I mean decreasing the wait time and TAT.

+ 99
- 0
week06/ex3.c View File

@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef struct {
int id, at, bt, et, tat, wt, rt, used;
} Process;

int proc_cmp(const void *a, const void *b) {
return ((Process*)a)->at - ((Process*)b)->at;
}

int proc_cmp_id(const void *a, const void *b) {
return ((Process*)a)->id - ((Process*)b)->id;
}

int deq(int q[], int *sz) {
int res = q[0];
for (int i = 0; i < *sz-1; ++i) {
q[i] = q[i+1];
}
(*sz)--;
return res;
}

int main() {
int n, mn = INT_MAX, q, qsz = 0;
printf("Quantum: ");
scanf("%d", &q);
printf("Number of processes: ");
scanf("%d", &n);
Process p[n];
int ready[n];

printf("Arrival time and Burst time:\n");
for (int i = 0; i < n; ++i) {
scanf("%d %d", &p[i].at, &p[i].bt);
p[i].id = i;
p[i].et = -1;
p[i].used = 0;
p[i].rt = p[i].bt;
ready[i] = 0;
}

qsort(p, n, sizeof(Process), proc_cmp);

printf("ID\tAT\tBT\tET\tTAT\tWT\n");

double avg_tat = 0, avg_wt = 0;
int curt = 0, jd = 0;
ready[qsz++] = 0;
p[0].used = 1;
while (qsz > 0) {
int id = deq(ready, &qsz);
if (p[id].rt != 0) {
int ex = (p[id].rt < q ? p[id].rt : q);
p[id].rt -= ex;
p[id].et = curt + ex;
curt = p[id].et;
p[id].tat = p[id].et - p[id].at;
p[id].wt = p[id].tat - p[id].bt;
if (p[id].rt == 0) {
avg_tat += p[id].tat;
avg_wt += p[id].wt;
jd++;
}
}
for (int i = 0; i < n; ++i) {
if (curt >= p[i].at) {
ready[qsz++] = i;
p[i].used = 1;
}
}

if (p[id].rt != 0) {
ready[qsz++] = id;
}

if (qsz == 0) {
for (int i = 0; i < n; ++i) {
if (p[i].used != 0) {
ready[qsz++] = i;
p[i].used = 1;
curt = p[i].at;
break;
}
}
}
}

qsort(p, n, sizeof(Process), proc_cmp_id);
for (int i = 0; i < n; ++i) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].et, p[i].tat, p[i].wt);
}

avg_tat /= n;
avg_wt /= n;
printf("avg:\t\t\t\t%lg\t%lg\n", avg_tat, avg_wt);
}

BIN
week06/ex3.out View File


+ 4
- 0
week06/ex3.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex3.c -o ex3.out
./ex3.out

Loading…
Cancel
Save