Experimental two-process scheduling simulation program

Source: Internet
Author: User

1. Purpose of the experiment

A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.

2. Experimental requirements

Design a process scheduling simulator with n processes executing concurrently.

Process scheduling algorithm: Using the highest priority scheduling algorithm (that is, the processor is assigned to the highest priority process) and first-come first service (if the same priority) algorithm.

(1). Each process has a Process Control block (PCB) representation. The Process Control block contains the following information: Process name, priority, arrival time, required run time, elapsed CPU time, process state, and so on.

(2). The priority of the process and the desired run time can be specified in advance, and the running time of the process is calculated in time slices.

(3). The state of each process can be either ready, running R (Running), or completing one of the three states of F (finished).

(4). The ready process can only run one time slice after the CPU is acquired. Represented by the elapsed CPU time plus.

(5). If the elapsed CPU time of a process has reached the desired run time after a time slice is run, the process is undone, and if the elapsed CPU time of the process after running a time slice has not reached the required run time, that is, the process needs to continue running, the priority number of the process should be reduced by 1 (that is, lowering the first , and then insert it into the ready queue for scheduling.

(6). Each time the scheduler is run, the PCB for each process in the running process and ready queue is printed for inspection.

(7). Repeat the process until the process is complete.

3. Experimental environment

Turbo C can be chosen as the development environment. You can also choose a visual environment such as VB,CB under Windows, which makes it easy to use various controls. Choose the experimental environment independently.

4. Job scheduling algorithm:

1) The first-come-first service (FCFS) scheduling algorithm, that is, according to the order of operation arrived scheduling. The job that waits the longest in the system is always dispatched first.

2) Short job first (SJF) scheduling algorithm, priority scheduling requires the shortest running time of the job.

3) in response to high-priority (HRRN) scheduling algorithm, set a priority (response ratio) for each job, before scheduling to calculate the priority of each job, priority of the higher priority scheduling. RP (response ratio) = job turnaround time/job run time =1+ job wait time/job run time

Each job is represented by a job control block, JCB can include the following information: Job name, Submission (arrival) time, required run time, required resources, job status, chain pointers, and so on.

The status of the job can be one of waiting for W (wait), running R (run), and completing F (finish) three. The initial state of each job is to wait for W.

5. Source program and its interpretation

#include <stdio.h>
#include <string.h>
typedef struct jcb{
Char name[10];//job Name
int arrtime;//Arrival time
int reqtime;//Request Service time
int startime;//Scheduling time
int finitime;//End Time
Float tatime,tawtime;//turnaround time with right turnaround time (turnaround time/service time)
}JCB;
int systime=0;
int intarr=0,intfin=0,intjob=0;//Number of jobs, number of jobs completed, number of jobs not reached
JCB jobarr[24],jobfin[24],job[24];

void input ()//input
{
int i;
Int J;
int n=0;
printf ("Please enter the number of jobs:");
scanf ("%d", &n);
for (i=0;i<n;i++,intjob++)
{
printf ("\ n%d jobs: \ n Please enter a job name:", i+1);
scanf ("%s", job[intjob].name);
do{
printf ("Please enter arrival time:");
scanf ("%d", &job[intjob].arrtime);
if (job[i].arrtime<systime)
printf ("Arrival time is less than the current system time!") Please re-enter: ");
}while (Job[i].arrtime<systime);
printf ("Please enter the required service time:");
scanf ("%d", &job[intjob].reqtime);
job[i].startime=0;
job[i].finitime=0;
Job[i]. tatime=0;
Job[i]. tawtime=0;
}
for (i=0;i<intjob;i++)//If the input arrival time is the current system time
{
if (job[i].arrtime==systime)
{
Jobarr[intarr]=job[i];
for (j=i;j<=intjob;j++)
{
JOB[J]=JOB[J+1];

}
intarr++;
intjob--;
i--;
}
else if (job[i].arrtime<systime)
{
printf ("Arrival time is less than the current system time!") Please re-enter ");
}
}
}

void output ()//output sorted after queue
{
int i;
printf ("\n\n\n is not in queue \ n");
printf ("\tname\tarrime\treqtime\tstartime fintime tatime\ttawtime\n");
for (i=0;i<intjob;i++) {
printf ("N%d\t%s\t%d\t%d\t%d\t%d\t%.2f\t%.2f\n", I+1,job[i].name,job[i].arrtime,job[i].reqtime,job[i].startime, Job[i].finitime,job[i]. Tatime,job[i]. Tawtime);
}
printf ("\ n" \ n has arrived in the queue);
printf ("\tname\tarrime\treqtime\tstartime fintime tatime\ttawtime\n");
for (i=0;i<intarr;i++) {
printf ("N%d\t%s\t%d\t%d\t%d\t%d\t%.2f\t%.2f\n", i+1,jobarr[i].name,jobarr[i].arrtime,jobarr[i].reqtime,jobarr[i ].startime,jobarr[i].finitime,jobarr[i]. Tatime,jobarr[i]. Tawtime);
}
printf ("\ n completed queue \ n");
printf ("\tname\tarrime\treqtime\tstartime fintime tatime\ttawtime\n");
for (i=0;i<intfin;i++) {
printf ("N%d\t%s\t%d\t%d\t%d\t%d\t%.2f\t%.2f\n", i+1,jobfin[i].name,jobfin[i].arrtime,jobfin[i].reqtime,jobfin[i ].startime,jobfin[i].finitime,jobfin[i]. Tatime,jobfin[i]. Tawtime);
}
printf ("Now System time:%d", systime);
}

void sort (JCB job[])//Sort
{
int i,j;
JCB temp;
for (i=0;i<intjob-1;i++)
{
for (j=i+1;j<intjob;j++)
{
if (job[i].arrtime>job[j].arrtime)
{
Temp=job[i];
JOB[I]=JOB[J];
Job[j]=temp;
}
}
}
}
First come first service algorithm
void Fcfs () {
int i,j=0,k;
while (Job[j].arrtime>systime)
{
systime++;
}
JOBARR[INTARR]=JOB[J];

for (k=j;k<=intjob;k++)
{
JOB[K]=JOB[K+1];
}
intarr++;
intjob--;
j--;
for (i=0;i<intarr;i++)
{
J=i;
Jobarr[i].startime=systime;
Jobarr[i].finitime=systime+jobarr[i].reqtime;
while (jobarr[i].finitime>systime| | intjob!=0)
{
systime++;
if (job[j].arrtime==systime)//Job never arrives in queue to enter reached queue
{
JOBARR[INTARR]=JOB[J];
for (k=j;k<=intjob;k++)
{
JOB[K]=JOB[K+1];
}
intarr++;
intjob--;
j--;
}
}
Jobarr[i]. Tatime=jobarr[i].finitime-jobarr[i].arrtime;
Jobarr[i]. Tawtime=jobarr[i]. Tatime/jobarr[i].reqtime;
Jobfin[intfin]=jobarr[i];


for (k=i;k<=intarr;k++)//The remaining elements in the queue are moved forward
{
JOBARR[K]=JOBARR[K+1];

}
intarr--;
intfin++;
i--;
}
}

Main ()
{
Char A;
Char b[20];
int i,j,key=0;
Input ();
Sort
Sort (Job);
Sort (Jobarr);
Output ();
systime++;
while (1)
{
printf ("\n\ninsert or Delete or Operating or Exit?" ( I or D or O or E): ");
GetChar ();
scanf ("%c", &a);
if (a== ' I ' | | a== ' I ')//Insert
{
Input ();
}
else if (a== ' D ' | | a== ' d ')//delete (delete only in wait queue)
{
printf ("Please enter the job name to delete:");
scanf ("%s", b);
for (i=0;i<intjob;i++)
{
if (strcmp (b,job[i].name) ==0)
{
Key=1;
for (j=i;j<=intjob;j++)
{
JOB[J]=JOB[J+1];

}
intjob--;
}
}
if (key==0)
{
printf ("Can't find the job!") ");
}

}
else if (a== ' O ' | | a== ' O ')
{
Fcfs ();
printf ("\ n \ nthe run");

}
else if (a== ' E ' | | a== ' e ')//exit
{
Exit (0);
}
Else
printf ("Input Error! ");
Sort (Job);
Sort (Jobarr);
Output ();
systime++;
}
}

6. Running Results

Experimental two-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.