Operating System Scheduling Algorithm Implementation (FCFS, and sjf) simulation-(0)

Source: Internet
Author: User

This program is a simulation of operating system experiments. We hope to provide valuable suggestions for its shortcomings.
# Include
Float t, d;/* define two global variables */
Struct/* defines a struct array, including process information */
{
Int id;
Float arrivetime;
Float servertime;
Float starttime;
Float finishtime;
Float Turnovertime;
Float bili;
Int flags;
} Pro [4];/* define the initialized struct array */
Create ()/* assign values to the struct array, input arrival, service time */
{Int I;
Float;
For (I = 0; I <4; I ++)
{Pro [I]. id = I + 1;
Printf ("input the arrivetime of % d Progress:", I + 1);/* time when the user inputs the process, initially zero */
Scanf ("% f", & );
Pro [I]. arrivetime =;
Printf ("input the servertime of % d Progress:", I + 1 );
Scanf ("% f", & );
Pro [I]. servertime =;
Pro [I]. starttime = 0;
Pro [I]. finishtime = 0;
Pro [I]. Turnovertime = 0;
Pro [I]. flags = 0;/* start default flag position 0 */
}
}
Int Serach_fcfs ()/* defines the first arrival time of unexecuted processes in FCFS */
{
Int I, j, s = 0;/* Find an unexecuted PROCESS IN THE struct array */
For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 0)
{
T = pro [I]. arrivetime;
S = 1;
}
If (s = 1)
Break;
}
For (I = 0; I <4; I ++)/* Find the process with the minimum unexecuted arrival time in the array */
{
If (pro [I]. arrivetimet = pro [I]. arrivetime;
}/* Returns the subscript of the array with the minimum arrival time */
For (I = 0; I <4; I ++)
{
If (pro [I]. arrivetime = t & pro [I]. flags = 0)
Return I;
}
}
Int Search_sjf ()/* defines the first arrival time of unexecuted processes in SJF */
{
Int I, x = 0, a = 0, B = 0;/* determine whether the process is the first to be executed */
Float g;
For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 1)
{G = pro [I]. finishtime;
X = 1;
}
}
If (x = 0)/* the first process executed is FCFS */
{
T = pro [0]. arrivetime;
For (I = 0; I <4; I ++)
{
If (pro [I]. arrivetime {t = pro [I]. arrivetime;
A = I;
}
}
Return ;}
Else
{
For (I = 0; I <4; I ++)
{If (pro [I]. finishtime> g)
G = pro [I]. finishtime;
}
For (I = 0; I <4; I ++)
{If (pro [I]. flags = 0 & pro [I]. arrivetime <= g)
{T = pro [I]. servertime;
A = I;
B = 1;}/* determine whether a process has arrived before the previous process is completed */
}
If (B = 1)/* when a process arrives, press SJF */
{For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 0 & pro [I]. arrivetime <= g & pro [I]. servertime {t = pro [I]. servertime;
A = I ;}
If (pro [I]. flags = 0 & pro [I]. arrivetime <= g & pro [I]. servertime = t)
{
If (pro [I]. arrivetime {t = pro [I]. servertime;
A = I;
}
}
}
Return ;}
Else {/* Otherwise, press FCFS */
For (I = 0; I <4; I ++)
{If (pro [I]. flags = 0)
T = pro [I]. arrivetime;
}
For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 0 & pro [I]. arrivetime {t = pro [I]. arrivetime;
A = I;
}
}
Return ;}
}
}
Begin (int s)/* defines the modification of related data after the execution process */
{
Int I, g = 0;
For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 0)
Continue;
Else
{
G = 1;
Break;
}
}
If (g = 0)/* execute when processing the first unexecuted process */
{
Pro [s]. starttime = pro [s]. arrivetime;
Pro [s]. finishtime = pro [s]. servertime + pro [s]. arrivetime;
Pro [s]. Turnovertime = pro [s]. servertime;
Pro [s]. flags = 1;
G = 2;
}
If (g = 1)/* execute when the process is not the first unexecuted process */
{
Pro [s]. flags = 1;
For (I = 0; I <4; I ++)
{
If (pro [I]. flags = 1)
D = pro [I]. finishtime;
}
For (I = 0; I <4; I ++)/* Find the completion time of the last executed process */
{
If (pro [I]. finishtime> d & pro [I]. flags = 1)
D = pro [I]. finishtime;
}
If (pro [s]. arrivetimepro [s]. starttime = d;
Else
Pro [s]. starttime = pro [s]. arrivetime;
Pro [s]. finishtime = pro [s]. starttime + pro [s]. servertime;
Pro [s]. Turnovertime = pro [s]. finishtime-pro [s]. arrivetime;
}
Pro [s]. bili = pro [s]. Turnovertime/pro [s]. servertime;
}
Printresult (int j)/* define the print function */
{
Printf ("% dt", pro [j]. id );
Printf ("% 5. 2ft", pro [j]. arrivetime );
Printf ("% 5. 2ft", pro [j]. servertime );
Printf ("% 5. 2ft", pro [j]. starttime );
Printf ("% 5. 2ft", pro [j]. finishtime );
Printf ("% 5. 2ft", pro [j]. Turnovertime );
Printf ("% 5.2fn", pro [j]. bili );
}
Main ()
{Int I, B, k, a, g = 0, c = 0;
Int d [4];
Clrscr ();
Printf ("t | ----------------------------------------------- | n ");
Printf ("t | \ | // | n ");
Printf ("t | \\~~ /// | N ");
Printf ("t | (~ @@~ ) | N ");
Printf ("t + ------- oOOo --------- (_) ----------- oOOo ----- + | n ");
Printf ("t | ******************************* | n ");
Printf ("t | Operate System Experiment | n ");
Printf ("t | Author: XiongGuoQing | n ");
Printf ("t | Run Environment: Win-TC | n ");
Printf ("t | --------------------------------------------- | n ");
Printf ("t | 1. FCFS ************************* | n ");
Printf ("t | 2. SFJ ************************* | n ");
Printf ("t | 0. EXIT ************************* | n ");
Printf ("t | --------------------------------------------- | n ");
Printf ("t | Bigbone 2005.5.19 | n ");
Printf ("t | + -------------------------------------------- + | n ");
Printf ("t | ----------------------------------------------- | n ");
For (I = 0; I ++)
{If (c)
Break;
Printf ("************************************* * **************************** n ");
Printf ("if a = 1, FCFS,; t ifa = 2, SJF; t if a = 0, EXIT; telse RETURN INPUTn ");
Printf ("************************************* * **************************** n ");
Printf ("Please input a: n ");
Scanf ("% d", & );
Switch ()
{
Case 0: c = 1;
Break;
Case 1:
If (g = 0)
{
Create ();
}
Else
{For (I = 0; I <4; I ++)
{Pro [I]. flags = 0;
Pro [I]. starttime = 0;
Pro [I]. finishtime = 0;
Pro [I]. Turnovertime = 0;
}
}

Printf ("***************************** the result of fcfsn ");
Printf ("NumbertArrivetServertStarttFinishtTurnovetTake power turnover timen ");
For (B = 0; B <4; B ++)/* call two functions to change the value of the struct Number */
{
K = Serach_fcfs ();
D [B] = k;
Begin (k );
}
For (B = 0; B <4; B ++)
Printresult (d [B]);/* call the print function to print the result */
Printf ("************************************* * ****************** n ");
Printf ("if g! = 1 return input the number. else input the number a: n ");
Printf ("************************************* * ****************** n ");
Printf ("please input a number g: n ");
Scanf ("% d", & g );
Continue;
Case 2:
If (g = 0)
{
Create ();
}
Else
{For (I = 0; I <4; I ++)
{Pro [I]. flags = 0;
Pro [I]. starttime = 0;
Pro [I]. finishtime = 0;
Pro [I]. Turnovertime = 0;
}
}
Printf ("******************************* the result of sjfn ");
Printf ("NumbertArrivetServertStarttFinishtTurnovetTake power turnover timen ");
For (B = 0; B <4; B ++)
{
K = Search_sjf ();
D [B] = k;
Begin (k );
}
For (B = 0; B <4; B ++)
Printresult (d [B]);
Printf ("************************************* * ****************** n ");
Printf ("if g! = 1 return input the number. else input the number a: n ");
Printf ("************************************* * ****************** n ");
Printf ("please input a number g: n ");
Scanf ("% d", & g );
Continue;
Default: printf ("the number Error. please input another number! N ");
}
}

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.