Add README and supporting materials

This commit is contained in:
AndrewTrieu
2023-02-05 16:53:31 +02:00
committed by Andrew Trieu
parent 6f5c7f67b4
commit 673ce20f3a
161 changed files with 960 additions and 0 deletions

View File

@@ -0,0 +1,187 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
//const int MAXJOB=50; // maximum number of jobs
#define MAXJOB 50
//data structure of jobs
typedef struct node
{
int number; // job number
int reach_time;// arrival time
int need_time;// execution time
int privilege;// priority
float excellent;// response ratio
int start_time;// start time
int wait_time;// waiting time
int visited;// if the job is accessed
bool isreached;// if the job has arrived
} job;
job jobs[MAXJOB];//job sequence
int quantity;//number of jobs
//initialize job sequence
void initial_jobs()
{
int i;
for(i=0;i<MAXJOB;i++)
{
jobs[i].number=0;
jobs[i].reach_time=0;
jobs[i].privilege=0;
jobs[i].excellent=0;
jobs[i].start_time=0;
jobs[i].wait_time=0;
jobs[i].visited=0;
jobs[i].isreached=false;
}
quantity=0;
}
//reset all job information
void reset_jinfo()
{
int i;
for(i=0;i<MAXJOB;i++)
{
jobs[i].start_time=0;
jobs[i].wait_time=0;
jobs[i].visited=0;
}
}
// Find the shortest job that has reached but not yet been executed. If not return -1
int findminjob(job jobs[],int count)
{
int minjob=-1;//=jobs[0].need_time;
int minloc=-1;
for(int i=0;i<count;i++)
{
if(minloc==-1){
if( jobs[i].isreached==true && jobs[i].visited==0){
minjob=jobs[i].need_time;
minloc=i;
}
}
else if(minjob>jobs[i].need_time&&jobs[i].visited==0&&jobs[i].isreached==true)
{
minjob=jobs[i].need_time;
minloc=i;
}
}
return minloc;
}
//Find the job that arrived ealiest
int findrearlyjob(job jobs[],int count)
{
int rearlyloc=-1;
int rearlyjob=-1;
for(int i=0;i<count;i++)
{
if(rearlyloc==-1){
if(jobs[i].visited==0){
rearlyloc=i;
rearlyjob=jobs[i].reach_time;
}
}
else if(rearlyjob>jobs[i].reach_time&&jobs[i].visited==0)
{
rearlyjob=jobs[i].reach_time;
rearlyloc=i;
}
}
return rearlyloc;
}
//read all job data
void readJobdata()
{
FILE *fp;
char fname[20];
int i;
//input the name of test file
printf("please input job data file name\n");
scanf("%s",fname);
if((fp=fopen(fname,"r"))==NULL)
{
printf("error, open file failed, please check filename:\n");
}
else
{
//read job information sequentially
while(!feof(fp))
{
if(fscanf(fp,"%d %d %d %d",&jobs[quantity].number,&jobs[quantity].reach_time,&jobs[quantity].need_time,&jobs[quantity].privilege)==4)
quantity++;
}
//print job information
printf("output the origin job data\n");
printf("---------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tneedtime\tprivilege\n");
for(i=0;i<quantity;i++)
{
printf("\t%-8d\t%-8d\t%-8d\t%-8d\n",jobs[i].number,jobs[i].reach_time,jobs[i].need_time,jobs[i].privilege);
}
}
}
//FCFS
void FCFS()
{
int i;
int current_time=0;
int loc;
int total_waitime=0;
int total_roundtime=0;
loc=findrearlyjob(jobs,quantity);
//print job stream
printf("\n\nFCFS job stream\n");
printf("------------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n");
current_time=jobs[loc].reach_time;
// Each loop finds the first arriving job and prints information about it
for(i=0;i<quantity;i++)
{
if(jobs[loc].reach_time>current_time)
{
jobs[loc].start_time=jobs[loc].reach_time;
current_time=jobs[loc].reach_time;
}
else
{
jobs[loc].start_time=current_time;
}
jobs[loc].wait_time=current_time-jobs[loc].reach_time;
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n",loc+1,jobs[loc].reach_time,jobs[loc].start_time,jobs[loc].wait_time,
jobs[loc].wait_time+jobs[loc].need_time);
jobs[loc].visited=1;
current_time+=jobs[loc].need_time;
total_waitime+=jobs[loc].wait_time;
total_roundtime=total_roundtime+jobs[loc].wait_time+jobs[loc].need_time;
// Get the first arriving job among the remaining jobs
loc=findrearlyjob(jobs,quantity);
}
printf("total waiting time:%-8d total turnaround time:%-8d\n", total_waitime, total_roundtime);
printf("average waiting time: %4.2f average turnaround time: %4.2f\n", (float)total_waitime/(quantity),(float)total_roundtime/(quantity));
}
// Shortest-Job-First Scheduling algorithm.
void SFJschdulejob(job jobs[],int count)
{
}
// Highest Response Ratio Next
//response ratio=turnaround time/execution time
void HRRFschdulejob()
{
}
// Priority scheduling algorithm
void HPF(job jobs[])
{
}
void main()
{
initial_jobs();
readJobdata();
FCFS();
reset_jinfo();
SFJschdulejob(jobs, quantity);
system("pause");
//return 0;
}

View File

@@ -0,0 +1,118 @@
//#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define VM_PAGE 7 /*Number of virtual pages*/
#define PM_PAGE 4 /* Number of memory blocks allocated to the job */
#define TOTAL_INSERT 18 /*Number of virtual page replacements*/
typedef struct
{
int vmn;
int pmn;
int exist;
int time;
}vpage_item;
vpage_item page_table[VM_PAGE];
vpage_item* ppage_bitmap[PM_PAGE];
int vpage_arr[TOTAL_INSERT] = { 1,2,3,4,2,6,2,1,2,3,7,6,3,2,1,2,3,6 }; // The access order virtual pages
void init_data() //initialize data
{
for (int i = 0; i<VM_PAGE; i++)
{
page_table[i].vmn = i + 1;
page_table[i].pmn = -1;
page_table[i].exist = 0;
page_table[i].time = -1;
}
for (int i = 0; i<PM_PAGE; i++) /*initialize the physical page map*/
{
ppage_bitmap[i] = NULL;
}
}
void FIFO()/*FIFO page replacement algorithem*/
{
int k = 0;
int i;
int sum = 0;
int missing_page_count = 0;
int current_time = 0;
bool isleft = true; /* Whether there are remaining physical blocks */
while (sum < TOTAL_INSERT)
{
if (page_table[vpage_arr[sum] - 1].exist == 0)
{
missing_page_count++;
if (k < 4)
{
if (ppage_bitmap[k] == NULL) /*find a free block*/
{
ppage_bitmap[k] = &page_table[vpage_arr[sum] - 1];
ppage_bitmap[k]->exist = 1;
ppage_bitmap[k]->pmn = k;
ppage_bitmap[k]->time = current_time;
k++;
}
}
else
{
int temp = ppage_bitmap[0]->time;
int j = 0;
for (i = 0; i < PM_PAGE; i++)
{
if (ppage_bitmap[i]->time < temp)
{
temp = ppage_bitmap[i]->time;
j = i;
}
}
ppage_bitmap[j]->exist = 0;
ppage_bitmap[j] = &page_table[vpage_arr[sum] - 1]; /*update page table */
ppage_bitmap[j]->exist = 1;
ppage_bitmap[j]->pmn = j;
ppage_bitmap[j]->time = current_time;
}
}
current_time++;
sum++;
}
printf("The number of page faults of FIFO is:%d\t Page fault rate:%f\t The number of replacement:%d\tReplacement rate:%f\n", missing_page_count, missing_page_count / (float)TOTAL_INSERT, missing_page_count - 4, (missing_page_count - 4) / (float)TOTAL_INSERT);
}
void LRU()
{
}
void OPT()
{
}
int main()
{
int a;
do
{
printf("Please choose page replacement algorithm1.FIFO\t2.LRU\t3.OPT\t0. quit\n");
scanf_s("%d", &a);
switch (a)
{
case 1:
init_data();
FIFO();
break;
case 2:
init_data();
LRU();
break;
case 3:
init_data();
OPT();
break;
}
} while (a != 0);
return 0;
}