Operating system process scheduling algorithm first served short job priority time slice Rotation

Source: Internet
Author: User

# Include <iostream>

# Include <stdio. h>

# Include <string>

// # Include <windows. h>

Using namespace STD;

// Hyugtyftydrtdtrdrrtrdrt

Struct Node

{

String name; // process (job) Name

Int arrivetime; // arrival time

Int servertime; // service time

Int lefttime; // The left time

Node * link; // pointer to the next node

};

Class cprocess

{

 

Public:

Cprocess (); // Constructor

~ Cprocess (); // destructor

Const cprocess & operator = (const cprocess & P); // overload value assignment operator

Void insertnode (string & Na, Int & at, Int & St); // Insert new elements (at from small to large) to the proper position of the linked list

Void sort (); // sort by service time from large to small

Bool isempty (); // determines whether it is null.

Void destroy (); // destroy

Int length (); // obtain the length of the linked list.

Void print (); // print the element

Void FCFS (); // first served

Void SJF (); // short process (job) Priority

Void RR (Int & Q); // time slice Rotation

Void priority (); // Priority Scheduling

Protected:

Node * first;

Node * last;

 

};

Const cprocess & cprocess: Operator = (const cprocess & P)

{

Node * newnode;

Node * current;

If (this! = & P) // avoid assigning values to yourself

{

If (first! = NULL) // If the linked list is not empty

Destroy ();

If (P. First = NULL)

{// If the object to be copied is empty

This-> first = NULL;

This-> last = NULL;

}

Else

{

Current = P. first;

First = new node;

First-> name = Current-> name ;//

First-> arrivetime = Current-> arrivetime;

First-> servertime = Current-> servertime;

First-> link = NULL;

Last = first;

Current = Current-> link;

While (current! = NULL)

{

Newnode = new node;

Newnode-> name = Current-> name;

Newnode-> arrivetime = Current-> arrivetime;

Newnode-> servertime = Current-> servertime;

Newnode-> link = NULL;

Last-> link = newnode;

Last = newnode;

Current = Current-> link;

}

}

}

Return * this;

}

 

Cprocess: cprocess ()

{// Constructor

First = NULL;

Last = NULL;

}

Cprocess ::~ Cprocess ()

{

Node * temp;

While (first! = NULL)

{

Temp = first;

First = first-> link;

Delete temp;

}

Last = NULL;

 

}

 

Void cprocess: insertnode (string & Na, Int & at, Int & St)

{// Sort by arrival time in ascending order

Node * current;

Node * trailcurrent; // point to the previous node of current

Node * newnode;

Bool found;

Newnode = new node; // create a new node

Newnode-> name = Na;

Newnode-> arrivetime =;

Newnode-> servertime = sT;

Newnode-> link = NULL ;//

If (first = NULL) // if the first node is empty (if the element is inserted for the first time)

First = newnode; // assign the new node to the first node

Else

{// If it is not the first time

Current = first;

Found = false;

While (current! = NULL &&! Found)

{

If (current-> arrivetime> =)

Found = true;

Else

{

Trailcurrent = current;

Current = Current-> link;

}

}

If (current = first)

{

Newnode-> link = first;

First = newnode;

}

Else

{

Trailcurrent-> link = newnode;

Newnode-> link = current;

}

}

}

Int cprocess: length ()

{

Int COUNT = 0; // declare the variable and initialize it to 0 (used to record the length)

Node * current;

Current = first;

While (current! = NULL) // the current node is not empty. The record value is automatically added and is traversed backward,

{

Count ++;

Current = Current-> link;

}

Return count; // return Length

}

Void cprocess: Sort () // sort by service time in ascending order

{// Bubble sort

String sname;

Int;

Int st;

Node * Current; // point to the current node

Node * trailcurrent; // point to the previous node of the current node

For (trailcurrent = first-> link; trailcurrent! = NULL; trailcurrent = trailcurrent-> link) // The control condition is incorrect.

{

For (current = trailcurrent-> link; current! = NULL; current = Current-> link) // The control condition is incorrect.

{

If (trailcurrent-> servertime> current-> servertime)

{

 

Sname = trailcurrent-> name;

At = trailcurrent-> arrivetime;

St = trailcurrent-> servertime;

 

Trailcurrent-> name = Current-> name;

Trailcurrent-> arrivetime = Current-> arrivetime;

Trailcurrent-> servertime = Current-> servertime;

 

Current-> name = sname;

Current-> arrivetime =;

Current-> servertime = sT;

 

}

}

}

}

 

Bool cprocess: isempty () // determines whether it is null.

{

Return (first = NULL); // if the first node is empty, Return Value

}

Void cprocess: Print ()

{

Node * current;

Current = first-> link; // the header node is assigned to the current node.

While (current! = NULL) // the current node is not empty. Print it backward.

{

Cout <Current-> name <"";

Cout <Current-> arrivetime <"";

Cout <Current-> servertime <"/N ";

Current = Current-> link;

}

}

Void cprocess: Destroy ()

{

Node * temp; // defines a temporary pointer variable

While (first! = NULL)

{

Temp = first;

First = first-> link;

Delete temp;

}

Last = NULL;

}

Void cprocess: FCFS () // first served

{

Node * current;

Int T0 = 0; // completion time

Int T1 = 0; // turnaround time

Current = first-> link; // the header node is assigned to the current node.

While (current! = NULL)

{

If (T0 <Current-> arrivetime)

{

T0 = Current-> arrivetime + current-> servertime;

T1 = T0-Current-> arrivetime;

Cout <Current-> name <"/t"; // print the process name

Cout <t0 <"/t"; // print the completion time

Cout <t1 <"/N"; // print the turnaround time

Current = Current-> link;

}

Else

{

T0 = Current-> servertime + t0;

T1 = T0-Current-> arrivetime; // the turnaround time equals to the completion time-arrival time

Cout <Current-> name <"/t"; // print the process name

Cout <t0 <"/t"; // print the completion time

Cout <t1 <"/N"; // print the turnaround time

Current = Current-> link;

}

}

 

}

Void cprocess: SJF () // short process (job) Priority

{

// First execute the first arrived job

Node * current;

Int T0 = 0; // completion time

Int T1 = 0; // turnaround time

T0 = first-> link-> servertime + t0;

T1 = T0-first-> link-> arrivetime;

Cout <first-> link-> name <"/t ";

Cout <t0 <"/t"; // print the completion time

Cout <t1 <"/N"; // print the turnaround time

First-> link = first-> link; // Delete

// Execute the remaining

Sort (); // sort the rest

Current = first-> link; // the header node is assigned to the current node.

While (current! = NULL)

{

If (T0 <Current-> arrivetime)

{

T0 = Current-> arrivetime + current-> servertime;

T1 = T0-Current-> arrivetime;

Cout <Current-> name <"/t"; // print the process name

Cout <t0 <"/t"; // print the completion time

Cout <t1 <"/N"; // print the turnaround time

Current = Current-> link;

}

Else

{

T0 = Current-> servertime + t0;

T1 = T0-Current-> arrivetime; // the turnaround time equals to the completion time-arrival time

Cout <Current-> name <"/t"; // print the process name

Cout <t0 <"/t"; // print the completion time

Cout <t1 <"/N"; // print the turnaround time

 

Current = Current-> link;

}

}

}

Void cprocess: RR (Int & Q) // time slice Rotation

{

Cout <"time slice rotation completed! /N ";

}

Void cprocess: Priority () // Priority Scheduling

{

 

Cout <"Priority operation completed! /N ";

}

Void main ()

{

Cprocess P0, P1, P2, P3, P4;

Int at, St;

String NA;

Int judge = 1; // control exit Program

Int choice; // control the selection operation

While (judge)

{

Cout <"************************************ * *******************/N ";

Cout <"******* Note: This program applies to single-process (job) ******/N ";

Cout <"********** select your operation ****************/N ";

Cout <"********** enter the corresponding number and press (enter! * *************/N ";

Cout <"************** 5. Enter information *************/N ";

Cout <"************** 1. first-come, first-served ************/N ";

Cout <"**************** 2. short processes (jobs) Give priority to ************/N ";

Cout <"************** 3. time slice rotation ************/N ";

Cout <"************* 4. Priority (static) scheduling ************/N ";

Cout <"************** 0. Exit the program *************/N ";

Cout <"************************************ * *******************/N ";

Cin> choice;

Switch (choice)

{

Case 0:

Judge = 0;

Break;

Case 5:

Cout <"enter information to end with" end! /N ";

Cout <"process name arrival time service time" <Endl;

While (Na. Compare ("end") // if equal, 0 is returned.

{

P0.insertnode (Na, AT, St );

Cin> Na> at> st;

}

Cout <"input successful. Current information:/N ";

Cout <"process name arrival time service time" <Endl;

Export print ();

Break;

Case 1: // first come first served

P1 = P0; // copy one copy

If (p1.isempty ())

{

Cout <"enter information first/N ";

Break;

}

Else

{

Cout <"first come first served/N ";

Cout <"process name completion time turnaround time/N ";

P1.fcfs ();

Break;

}

Case 2: // short job priority

P2 = P0; // copy one copy

// P2.sort ();

// P2.print ();

If (p2.isempty ())

{

Cout <"enter information first/N ";

Break;

}

Else

{

Cout <"short job priority/N ";

Cout <"process name completion time turnaround time/N ";

P2.sjf ();

Break;

}

Case 3: // time slice Rotation

P3 = P0; // copy one copy

Int Q;

If (p3.isempty ())

{

Cout <"enter information first/N ";

Break;

}

Else

{

Cout <"Enter the time slice size ";

Cin> q;

Cout <"time slice rotation/N ";

Cout <"process name completion time turnaround time/N ";

P3.rr (Q );

Break;

}

Case 4: // priority

P4 = P0; // copy one copy

If (p4.isempty ())

{

Cout <"enter information first/N ";

Break;

}

Else

{

Cout <"time slice rotation/N ";

Cout <"process name completion time turnaround time/N ";

P4.priority ();

Break;

}

Default:

Cout <"select the option in the directory! /N ";

Break;

}

}

Return;

}

Void cprocess: Priority () // Priority Scheduling
{
Int T0 = 0; // completion time
Int T1 = 0; // turnaround time
Node * current;
Int PR;
Node * P;
P = first-> link;
Print ();
Cout <"Enter the priority of each process in sequence (in the print order above):/N ";
Cout <"NOTE: the smaller the number, the higher the priority/N ";
While (P! = NULL)
{// Storage priority
Cin> PR;
P-> priority = Pr;
// Cout <p-> name <"" <p-> priority <Endl;
P = p-> link;
}
//////////////////////////////////////// //////////////////////////////////////// /////////////////
// First, determine whether it is arriving at the same time. If yes
If (else all ())
{// Simultaneous arrival
Sort_bypr (); // sort by priority from high to low
Current = first-> link; // the header node is assigned to the current node.
Cout <"process name completion time turnover time priority/N ";
While (current! = NULL)
{

T0 = t0 + current-> servertime;
T1 = T0-Current-> arrivetime;
Cout <Current-> name <"/t"; // print the process name
Cout <t0 <"/t"; // print the completion time
Cout <t1 <"/t"; // print the turnaround time
Cout <Current-> priority <"/N"; // print the priority.
Current = Current-> link;

}
Return;
}
//////////////////////////////////////// //////////////////////////////////////// ////////////
// First execute the oldest Process
T0 = first-> link-> servertime + t0;
T1 = T0-first-> link-> arrivetime;
Cout <"process name completion time turnover time priority/N ";
Cout <first-> link-> name <"/t ";
Cout <t0 <"/t"; // print the completion time
Cout <t1 <"/t"; // print the turnaround time
Cout <first-> link-> priority <"/N"; // print the priority.
First-> link = first-> link; // Delete
Sort_bypr (); // sort by priority from high to low
//////////////////////////////////////// /////////////////////////////////////////
// Execute the remaining
Current = first-> link;
While (current! = NULL)
{
If (T0 <Current-> arrivetime)
{
T0 = Current-> arrivetime + current-> servertime;
T1 = T0-Current-> arrivetime;
Cout <Current-> name <"/t"; // print the process name
Cout <t0 <"/t"; // print the completion time
Cout <t1 <"/t"; // print the turnaround time
Cout <Current-> priority <"/N"; // print the priority.
Current = Current-> link;
}
Else
{
T0 = Current-> servertime + t0;
T1 = T0-Current-> arrivetime; // the turnaround time equals to the completion time-arrival time
Cout <Current-> name <"/t"; // print the process name
Cout <t0 <"/t"; // print the completion time
Cout <t1 <"/t"; // print the turnaround time
Cout <Current-> priority <"/N"; // print the priority.
Current = Current-> link;
}
}



}

Only part, not finished!

You have time to write it again!

Hope to confirm!

Help complete it! Grateful!

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.