Shortest job Priority scheduling

Source: Internet
Author: User

#include <stdio.h>
#define TIME INT
#define M 100
typedef struct process{
Char name[m];//process Name
int priority;//Precedence Number
int reachtime;//Arrival time
int needtime;//takes time
int usedtime;//used Time
int completetime;//Finish Time
int time1;//in turnaround time
int time2;//with right turnaround time
int state;//Process State
int ptime;//time Slice size
}PCB; Process Control block
int n;
PCB Pcb[m];
int ptime;
void print (int n) {
int i;
printf ("############################### #最短作业优先进程调度 ############################\n");
printf ("--------------------------------------------------------------------------------\ n");
printf ("Process name takes time to arrive time to finish time turnaround time with right turnaround time \ n");
for (i=0;i<n;i++) {
printf ("%s\t%d\t \ t%d\t\t%d\t\t%d\t%d\n", pcb[i].name,pcb[i].needtime,pcb[i].reachtime,pcb[i].completetime,pcb[i ].TIME1,PCB[I].TIME2);

}
printf ("--------------------------------------------------------------------------------\ n");
}


void Sjsort (int n) {
int i;
Int J;
int a=0;
int b;
PCB temp;
Judge
for (b=0;b<n;b++) {

A=a+pcb[b].needtime;
if (a>=pcb[n-1].reachtime) {

printf ("%d\n", b+1);
Break
}

}



for (i=b+1;i<n;i++) {
for (j=i;j<n;j++) {
if (pcb[i].needtime>pcb[j].needtime) {
TEMP=PCB[J];
Pcb[j]=pcb[i];
Pcb[i]=temp;
}

}
}

}





void Main () {
int i;
Int J;
float avertime1=0.00;
float avertime2=0.00;

PCB temp;
printf ("Please enter the number of processes:");
scanf ("%d", &n);
for (i=0;i<n;i++) {
printf ("\ n Please enter the process name:");
scanf ("%s", &pcb[i].name);

printf ("Please enter the time required:");
scanf ("%d", &pcb[i].needtime);
printf ("Please enter the time of Arrival:");
scanf ("%d", &pcb[i].reachtime);
}

Sjsort (n);

print (n);
for (i=0;i<n;i++) {
while (pcb[i].needtime!=0) {
printf ("\ n please press any key to continue ... \ n");
printf ("\ n");
Fflush (stdin);
GetChar ();

printf ("Currently running the program is: \ n");

printf ("--------------------------------------------------------------------------------\ n");
printf ("Process name takes time to arrive time to finish time turnaround time with right turnaround time \ n");
if (i==0) {
pcb[i].completetime=pcb[i].needtime+pcb[i].reachtime;//Calculate Completion time
}
else{
pcb[i].completetime=pcb[i-1].completetime+pcb[i].needtime;//Calculate Completion time
}
pcb[i].time1=pcb[i].completetime-pcb[i].reachtime;//Calculate turnaround time
pcb[i].time2=pcb[i].time1/pcb[i].needtime;//calculating the turnaround time of the belt weight

printf ("%s\t%d\t \ t%d\t\t%d\t\t%d\t%d\n", pcb[i].name,pcb[i].needtime,pcb[i].reachtime,pcb[i].completetime,pcb[i ].TIME1,PCB[I].TIME2);

printf ("--------------------------------------------------------------------------------\ n");

pcb[i].needtime=0;
printf ("\ n");
print (n);

}


}
for (i=0;i<n;i++) {
avertime1=avertime1+pcb[i].time1;
avertime2=avertime2+pcb[i].time2;

}
printf ("Average turnaround time for this process is:");
printf ("%.2f\n", avertime1/n);
printf ("The average belt turnaround time for the process is:");
printf ("%.2f\n", avertime2/n);

printf ("Hint: the shortest job priority process is scheduled to end!\n");


}

Shortest job Priority scheduling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.