Experiment three, process scheduling simulation program

Source: Internet
Author: User
Tags clear screen

Experiment three, process scheduling simulation program

Professional network engineering name Fang Junhui No. 201406114309

First, Experimental Purpose

This experiment can deepen the understanding of the concept of process control block and process queue.

Second, experimental content and requirements

1. Definition of the structure of the process PCB

2. Define the structure body

3. Enter the process sequence

4. Sort (by time in place)

5. Output process Run results

Third, test methods, procedures and results

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <time.h>
typedef struct NODE
{
Char name[10]; Process Name
int prio; Process priority number
int CPUTime; Process consumes CPU time
int needtime; The time it takes for the process to complete
int arrivetime; Process arrival time
int starttime; Process Start time
int finishtime; Process Finish Time
int servicetime; Process Service Time
Char state; Status of the process
struct node *next;
}PCB;
PCB *finish,*ready,*run; Queue pointers
int N; Process Volume
void Firstin ()
{
Run=ready; The ready queue header pointer is assigned to the running head pointer
Run->state= ' R '; Process state changes to run mode
ready=ready->next; Ready to move the column head pointer back to the next process
}
void Prt1 (char a)
{
Switch (a)
{
Case 1:/* Precedence number method */
printf ("The name \ t process takes up CPU time \ t takes the time \ t precedence series \ t state \ n");
Case 2:/* First come First service algorithm */
printf ("The name \ t arrival time \ t start time \ t service time \ t completion time \ t state \ n");
Default:break;
}
}
void Prt2 (char a,pcb *q)
{
Switch (a)
{
Case 1:printf ("%s\t%d\t\t%d\t\t%d\t\t%c\n", q->name,q->cputime,q->needtime,q->prio,q->state);
Break
Case 2:printf ("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%c\n", Q->name,q->arrivetime,q->starttime,q->servicetime, Q->finishtime,q->state);
Break
Default:break;
}
}
void prt (char algo)
{
PCB *p;
Prt1 (ALGO); Output Text Format
if (run!=null)//If the running pointer is not empty
Prt2 (Algo,run); Output the currently running PCB
P=ready; Output Ready Queue PCB
while (P!=null)
{
Prt2 (ALGO,P);
p=p->next;
}
P=finish; PCB for Output Completion queue
while (P!=null)
{
Prt2 (ALGO,P);
p=p->next;
}
GetChar (); Press any key to continue
}
void Insert1 (PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; PCB pointer to be inserted
P1=ready; Ready Queue Header pointer
R=P1; A precursor to P1.
B=1;
while ((p1!=null) &&b)//Determine the insertion position based on the priority number
if (P1->prio>=s->prio)
{
R=P1;
p1=p1->next;
}
Else
b=0;
if (R!=P1)//If the conditions of incorporation are inserted between R and P1
{
r->next=s;
s->next=p1;
}
Else
{
s->next=p1; Otherwise, insert the header in the ready queue
Ready=s;
}
}
void Insert2 (PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; Pointer s pointing to the new process to be inserted
P1=ready; Pointer P1 points to the original process's first
R=P1; Use the pointer r to point to the process in front of P1
B=1;
while ((P1!=null) &&b)
{
if (p1->arrivetime<s->arrivetime)
{
R=P1;
p1=p1->next;
}
Else
b=0;
}
if (R!=P1)
{
r->next=s;
s->next=p1;
}
Else
{
s->next=p1;
Ready=s;
}
}
void Create1 (char alg)
{
PCB *p;
int i,time;
Char na[10];
Ready=null; Ready Queue Header pointer
Finish=null; Complete the queue header pointer
Run=null; Run the queue header pointer
Enter n process name and time required to create a PCB
for (i=1;i<=n;i++)
{
printf ("Please enter the first name and run time of the%d process \ Process name:", I);
p= (PCB *) malloc (sizeof (PCB));
scanf ("%s", NA);
printf ("Time required:");
scanf ("%d", &time);
strcpy (P->name,na);
p->cputime=0;
p->needtime=time;
P->state= ' W ';
P->prio=rand ()%15+1; Randomly assigned precedence [1,15]
if (ready!=null)//Ready queue is not empty then call insert function Insert
Insert1 (P); Insert a ready queue for a new process
Else
{
p->next=ready; Create a ready queue for the first PCB
Ready=p;
}
}
System ("CLS");
printf ("precedence algorithm result output \ n");
printf ("---------------------------------------------------------------------------\ n");
PRT (ALG); Output process PCB Information
Run=ready; Put the first process of the ready queue into operation
ready=ready->next;
Run->state= ' R ';
}
void Create2 (char alg)
{
PCB *p;
int i;
Ready=null;
Run=null;
Finish=null;
for (i=0;i<n;i++)
{
p= (PCB *) malloc (sizeof (PCB));
printf ("Process name:");
scanf ("%s", p->name);
printf ("Arrival time:");
scanf ("%d", &p->arrivetime);
printf ("Takes time:");
scanf ("%d", &p->servicetime);
p->starttime=0;
p->finishtime=0;
P->state= ' W ';
if (ready!=null)
Insert2 (P);//Insert a new process into the ready queue
Else
{
p->next=ready;//the first to create a ready queue
Ready=p;
}
}
System ("CLS");
printf ("First come first service algorithm result output \ n");
printf ("---------------------------------------------------------------------------\ n");
PRT (ALG);
}
void priority (char ALG)
{
while (Run!=null)//When the running queue is not empty, a process is running
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-1; Priority number per run-1
if (run->prio<0)//If the priority number is reduced to less than 0, it is converted to 0
run->prio=0;
if (run->needtime==0)//If the required time is 0, complete, insert it into the completion queue
{
run->next=finish;
Finish=run;
Run->state= ' F '; State is complete
Run=null; Run queue header pointer is empty
if (ready!=null)//If the ready queue is not empty
Firstin (); Put ready to run on the first process of a column
}
else//does not run out and the priority number is not the maximum, then it is inserted into the ready queue
if ((ready!=null) && (Run->prio<ready->prio))
{
Run->state= ' W '; Status changed to Ready
Insert1 (run); Insert process by priority number
Firstin (); Put the first process of the ready queue into operation
}
PRT (ALG); Output process PCB Information
}
}
void FCFS (char alg)
{int time=0;//system time starting from 0
do{
The first process of the run=ready;//ready sequence is placed in the run queue for execution
Run->state= ' R ';//process starts execution
ready=ready->next;//point to Next
Time=run->arrivetime>time? run->arrivetime:time;
run->starttime=time;//Process Start
PRT (ALG);//Displays the process being executed
time=time+run->servicetime;//to calculate the next process minimum start time
run->finishtime=time;//Process End Time
Run->state= ' F ';//End Status identification
PRT (ALG);//process End display again
run->next=finish;
finish=run;//process end into end queue
Run=null;
}while (Ready!=null);
}
/* Menu Display function */
void Menu ()
{
System ("CLS");
printf ("\t\t+━━━━━━━━━━━━━━━━━━━━━━+\n");
printf ("\t\t| PCB Process Scheduling | \ n ");
printf ("\t\t|━━━━━━━━━━━━━━━━━━━━━━| \ n ");
printf ("\t\t| | \ n ");
printf ("\t\t| [1] Precedence algorithm | \ n ");
printf ("\t\t| | \ n ");
printf ("\t\t| [2] first come first service algorithm | \ n ");
printf ("\t\t| | \ n ");
printf ("\t\t| [3] Exit System | \ n ");
printf ("\t\t| | \ n ");
printf ("\t\t|━━━━━━━━━━━━━━━━━━━━━━| \ n ");
printf ("\t\t| By:fjh | \ n ");
printf ("\t\t+━━━━━━━━━━━━━━━━━━━━━━+\n");
printf ("\t\t Please enter Number:");
}
int main ()
{
Char Algo; Receive algorithm number
Char mainmenu;//determine whether to continue
Srand ((unsigned) time (NULL));
System ("CLS");//Clear screen
do{
menu ();//Display menus
scanf ("%d", &algo); Enter the algorithm number
Switch (ALGO)
{
Case 1:
System ("CLS");
printf ("You have chosen a priority algorithm \ n");
printf ("Please enter the number of processes:");
scanf ("%d", &n); Number of input processes
Create1 (ALGO); Create a queue
Priority (ALGO);//Number of priorities
Break
Case 2:
System ("CLS");
printf ("You selected first come first service algorithm \ n");
printf ("Please enter the number of processes:");
scanf ("%d", &n); Number of input processes
Create2 (algo);//Create queue
FCFS (algo);//First Come first service
Break
Case 3:
printf ("\t\ti want to see you again!886! \ n ");
Exit (0);
Break
Default
printf ("Incorrect input \ n");
Break
}
printf ("\ nthe continued operation (y/n)");
Fflush (stdin);
Mainmenu=getchar ();
}
while (mainmenu== ' Y ' | | mainmenu== ' Y ');
return 0;
}

Four, Experimental Summary

This experiment needs to apply the knowledge of the C language previously learned, define the structure, pointers and other methods to complete the experiment, in the process of PCB process management needs to be repeated test, in order to complete.

Experiment three, process scheduling simulation program

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.