FCFS first Come first service

Source: Internet
Author: User

/*
First come first serve FCFS

Requirements:
1> with struct definition program: program number, priority number (arrival time), time required for CPU operation

2> Input Process sequence

3> sort output by priority number
*/
#include <stdio.h>
#include <string.h>
#define N 100
typedef struct TIME
{
int reachtime; Priority number (program arrival time)
int runningtime; CPU Operation time
}time;

typedef struct INFORMATION
{
Long ID; Program number

Time pay;
}inf;

int n;
void Readpay (INF p[]);
void Desortbythefirst (INF p[]);

void main ()//main function
{
INF P[n];
struct time;
int sum=0;
int i;
Long number;
printf ("Please enter the number of programs: (<100)");
scanf ("%d", &n);
Readpay (P);
for (i=0;i<n;i++)
{
Sum=sum+p[i].pay.reachtime+p[i].pay.runningtime;
}
printf ("\n\n\t\t\t#### #总周转时间为:%d milliseconds #####\n\t\n", sum);
printf ("\t\t\t#### #按优先级高低排序输出 #####\n\n");
Desortbythefirst (P);


}

void Readpay (INF p[])//1. Input program Data
{
int i;
for (i=0;i<n;i++)
{
printf ("\n\t Please enter%d program number:", i+1);
scanf ("%ld", &p[i].id);
printf ("Please enter the arrival time for%d programs:", i+1);
scanf ("\t%d", &p[i].pay.reachtime);
printf ("Please enter the CPU time for%d programs:", i+1);
scanf ("\t%d", &p[i].pay.runningtime);

printf ("\t\n%10ld%d\t%d\t\n turnaround time:%d\n", P[i].id,p[i].pay.reachtime,p[i].pay.runningtime,p[i].pay.reachtime+p[i]. Pay.runningtime);

}
}

void Desortbythefirst (INF p[])//2. Sort output by priority level
{
int i,j,k;
int all;
int a1,a2;
INF temp;

for (i=0;i<n-1;i++)
{
K=i;
for (j=i+1;j<=n;j++)
{
A1=p[i].pay.reachtime+p[i].pay.runningtime;
A2=p[j].pay.reachtime+p[j].pay.runningtime;
if (A1<A2)
{
TEMP=P[J];
P[j]=p[i];
P[i]=temp;
}
}
printf ("\n\t by priority rank: \ n");
printf ("\n\t No. \ t program number \ t arrival time \ t run time \t\n");
for (k=0;k<n;k++)
{

All=p[k].pay.reachtime+p[k].pay.runningtime;
printf ("\t%d%10ld\t%d\t\t%d\t\n", K+1,p[k].id,p[k].pay.reachtime,all);
}

}


}

#####################################################

Operation Result:

FCFS first Come first service

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.