(Operation) Experiment 1 of scheduling algorithm

Experiment 1 Process scheduling experiment

【Purpose】

(1) Deepen the understanding of the concept of process and process scheduling algorithm; conduct training in programming.

(2) Process scheduling is the core content of processor management. Write and debug a simple process scheduler by yourself. Through this experiment, you can deepen your understanding of the concept of process control blocks and process queues, and understand and understand the specific implementation methods of algorithms such as first-come-first-served, short job priority, and high priority priority scheduling.

【Experiment content】

Design a process scheduling simulation program with N processes executing concurrently.

Process scheduling algorithm: (1) First-come-first-served scheduling algorithm: First store all processes into the queue at the same time according to the time of entering the CPU (first enter the presence of the CPU near the head of the team), and then transfer the process of the head of the team into Memory, allocate resources for him, and put it into operation until the process is completely completed, and then transfer to the first process of the team until the queue is empty.

(2) Short job priority scheduling algorithm: first arrange all ready queues in the order of remaining time from small to large, and store them in the queue in turn, and then execute them according to the first come first service method.

1. Each process is represented by a process control block (PCB). The process control block contains the following information: process name, priority, arrival time, required running time, elapsed CPU time, process status, etc.

2. The priority of the process and the required running time can be manually specified in advance, and the running time of the process is calculated in units of time slices.

3. The ready process can only run one time slice after acquiring the CPU. It is expressed by adding 1 to the occupied CPU time.

4. If the CPU time of the process has reached the required running time after running a time slice, then cancel the process. If the CPU time of the process has not reached the required running time after running a time slice, also Even if the process needs to continue to run, the priority of the process should be reduced by 1 (that is, lowered by one level), and then inserted into the ready queue for scheduling.

5. Print the PCB of each process in the running process and ready queue every time the scheduler is executed for inspection.   

6. Repeat the above process until all the required processes are completed.

【Design ideas】

The essence of scheduling in the OS is a kind of resource allocation, so the scheduling algorithm refers to: the resource allocation algorithm specified by the system's resource allocation strategy. The commonly used algorithms for process scheduling are: first-come-first-served scheduling, priority scheduling, time slice rotation scheduling, and multi-level feedback queue scheduling. This experiment is to design the first-come-first-served, short job first, and two scheduling algorithms and compare their performance. The program provides the basic functions of creating processes and selecting process scheduling algorithms.

First, you should design the data structure, create the process structure, and then carefully design the algorithm for each process scheduling, and use the program to implement it. After the algorithm is designed, debug the overall program and test it to compare the pros and cons of various process scheduling in different environments.

Among the existing scheduling algorithms, some short hairs are suitable for job scheduling, and some algorithms are suitable for process scheduling; but some scheduling algorithms can be used for both job scheduling and process scheduling.

[Flow diagram]                                            

 

 【Main program code】

#include<stdio.h>

#define N 50

int main ()

{    

    void xfw (); // First come first service

    void dzy (); // Short job priority

    int a;

    while(true)

    {

        printf("\n\n");

        printf ("Please choose one of the scheduling algorithms:");

        printf ("\ n 1. First-come-first-served scheduling");

        printf ("\ n 2. Short job priority scheduling");

        printf ("\ n 0, exit \ n");    

        printf ("\ n \ n Please select the menu item:");

        scanf("%d",&a);

        printf("\n");

        switch(a){

            case 1: xfw();break;

            case 2: between (); break;

            default: break;

        }

        if(a<0&&a>3) break;

    }

}

void xfw()

{

    int i, j, n, min, px; // n: number of processes px: output mode selection

    float sum1,sum2;

    printf ("Please enter n processes (0 <n <= 50):");

    scanf("%d",&n);

    while(n>50||n<=0)

    {

        printf ("nPlease re-enter:");

        scanf("%d",&n);

    }

    printf("\n\n");

    struct PCB{

        int id; // Process name

        int dt; // time of arrival

        int st; // Service time

        int wct; // Complete moment

        float zt; // Turnaround time

        float dczt; // Weighted turnaround time

    };

    PCB a[N];

    for(i=0;i<n;i++)

    {

        a[i].id=i+1;

        printf ("Arrival time of process% d:", a [i] .id);

        scanf("%d",&a[i].dt);

        printf ("Process% d service time:", a [i] .id);

        scanf("%d",&a[i].st);

        printf("\n");

    }

    for(j=n-1;j>=0;j--)

    {

        for(i=0;i<j;i++)

        {

            if(a[i].dt>a[i+1].dt)

            {             

                min=a[i].id;

                a[i].id=a[i+1].id;

                a[i+1].id=min;

                min=a[i].dt;

                a[i].dt=a[i+1].dt;

                a[i+1].dt=min;

                min=a[i].st;

                a[i].st=a[i+1].st;

                a[i+1].st=min;

            }

        }

    }

    a [0] .wct = a [0] .dt + a [0] .st; // Complete time = arrival time + service time

    a [0] .zt = (float) a [0] .st; // first turnaround time = service time

    a [0] .dczt = a [0] .zt / a [0] .st; // weighted turnaround time = turnover time / service time

 

    for(i=1;i<n;i++)

    {

        if (a [i] .dt> a [i-1] .wct) // When a [i] arrives time> a [i-1] complete time

        {

            a [i] .wct = a [i] .dt + a [i] .st; // a [i] completion time = arrival time + service time

            a [i] .zt = (float) a [i] .st; // At this time, turnaround time = service time

            a [i] .dczt = a [i] .zt / a [i] .st; // weighted turnaround time = turnover time / service time

        }

        else

        {

            a [i] .wct = a [i-1] .wct + a [i] .st; // a [i] time of completion = a [i-1] time of completion + a [i] service time

            a [i] .zt = (float) (a [i] .wct-a [i] .dt); // Turnover time = time of completion-time of arrival

            a [i] .dczt = a [i] .zt / a [i] .st; // weighted turnaround time = turnover time / service time

        }

    }                                                                    

    printf ("1, output \ n" in order by id number);

    printf ("2. Output \ n" in order of completion);

    printf ("\ nPlease select the output order:");

    scanf("%d",&px);

    printf ("\ nid: Arrival time \ tService time \ tComplete time \ tTurnaround time \ tPowered turnaround time \ n");

    sum1=0;

    sum2=0;

    switch(px)

    {

        case 1: // 1. Output in order by id number

        {

            for(j=0;j<n;j++)

            {  

                for(i=0;i<n;i++)

                    if(a[i].id==j+1)

                    {

      printf("%d: %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);

                        sum1+=a[i].zt;

                        sum2+=a[i].dczt;

                    }

                    

            }

            printf ("\ nAverage turnaround time:% .2f \ n", sum1 / n);

            printf ("\ nAverage weighted turnaround time:% .2f \ n \ n", sum2 / n);

            break;

        }

        case 2: // 2. Output in order according to the completion order

        {   

            for(i=0;i<n;i++)

            {  

printf("%d: %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);

                sum1+=a[i].zt;

                sum2+=a[i].dczt;

            }

            printf ("\ nAverage turnaround time:% .2f \ n", sum1 / n);

            printf ("\ nAverage weighted turnaround time:% .2f \ n \ n", sum2 / n);

            break;

        }

        default: break;

    }

}

void between ()

{

    int i, j, n, min, px; // n: number of processes px: output mode selection

    int b=0,z;

    float sum1,sum2;

    printf ("\ nPlease enter n processes (0 <n <= 50):");

    scanf("%d/n",&n);

    while(n>50||n<=0)

    {

        printf ("nPlease re-enter:");

        scanf("%d",&n);

    }

    printf("\n");

    struct PCB{

        int id; // Process name

        int dt; // time of arrival

        int st; // Service time

        int wct; // Complete moment

        float zt; // Turnaround time

        float dczt; // Weighted turnaround time

    };

    PCB a[N];

    for(i=0;i<n;i++)

    {

        a[i].id=i+1;

        printf ("Arrival time of process% d:", a [i] .id);

        scanf("%d",&a[i].dt);

        printf ("Process% d service time:", a [i] .id);

        scanf("%d",&a[i].st);

        printf("\n");

    }

    min=a[0].dt;     

    for(j=n-1;j>=0;j--)

    {

        for(i=0;i<j;i++)

        {

            if (a [i] .dt> a [i + 1] .dt) // If the arrival time of a [i]> the arrival time of a [i + 1]

            {

                min=a[i].id;

                a[i].id=a[i+1].id;

                a[i+1].id=min;

                 min=a[i].dt;

                a[i].dt=a[i+1].dt;

                a[i+1].dt=min;

                min=a[i].st;

                a[i].st=a[i+1].st;

                a[i+1].st=min;          

            }

            if (a [i] .dt == a [i + 1] .dt && a [i] .st> a [i + 1] .st) // arrive at the same time and a [i] is shorter

            {

                min=a[i].id;

                a[i].id=a[i+1].id;

                a[i+1].id=min;

                min=a[i].dt;

                a[i].dt=a[i+1].dt;

                a[i+1].dt=min;

                min=a[i].st;

                a[i].st=a[i+1].st;

                a[i+1].st=min;

            }

        }

    }

    a[0].wct=a[0].st+a[0].dt;

    a[0].zt=(float)a[0].st;

    a[0].dczt=a[0].zt/a[0].st;

    for(i=1;i<n;i++)

    {

        if(a[i].dt>a[0].wct) ;

        else b=b+1;

    }

    for(j=b-1;j>=1;j--)

    {

        for(i=1;i<j;i++)

        {

            if(a[i].st>a[i+1].st)

            {

                min=a[i].dt;

                a[i].dt=a[i+1].dt;

                a[i+1].dt=min;

                 

                min=a[i].st;

                a[i].st=a[i+1].st;

                a[i+1].st=min; 

                min=a[i].id;

                a[i].id=a[i+1].id;

                a[i+1].id=min;

            }

        }

    }   

    for(i=1;i<n;i++)

    {

        if(a[i].dt>a[i-1].wct)

        {

            a[i].wct=a[i].dt+a[i].st;

            a[i].zt=(float)a[i].st;

            a[i].dczt=a[i].zt/a[i].st;

        }

        else

        {

            a[i].wct=a[i-1].wct+a[i].st;

            a[i].zt=(float)(a[i].wct-a[i].dt);

            a[i].dczt=a[i].zt/a[i].st;

        }              

        for(j=i+1,b=j;j<n;j++)

        {

            if(a[j].dt>a[i].wct) ;

            else b=b+1;            

        }    

        for(j=b-1;j>=i;j--)

        {

            for(z=i;z<j;z++)

            {

                if(a[z].st>a[z+1].st)

                {

                    min = a [z] .dt;

                    a[z].dt=a[z+1].dt;

                    a [z + 1] .dt = min;

                    min=a[z].st;

                    a[z].st=a[z+1].st;

                    a[z+1].st=min;

                    min=a[i].id;

                    a[i].id=a[i+1].id;

                    a[i+1].id=min;                  

                }

            }

        }

    }                                                                    

    printf ("\ nPlease select the output order \ n");

    printf ("1, output \ n" in order by id number);

    printf ("2. Output \ n" in order of completion);

    scanf("%d",&px);

    printf ("\ nid: Arrival time \ tService time \ tComplete time \ tTurnaround time \ tPowered turnaround time \ n");

    sum1=0;

    sum2=0;

    switch(px)

    { 

        case 1: // 1, output in order according to id number

        {

            for(j=0;j<n;j++)

            {    

                for(i=0;i<n;i++)

                    if(a[i].id==j+1)

                    {

printf("%d: %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);

                        sum1+=a[i].zt;

                        sum2+=a[i].dczt;

                    }

            }

            printf ("\ nAverage turnaround time:% .2f \ n", sum1 / n);

            printf ("\ nAverage weighted turnaround time:% .2f \ n \ n", sum2 / n);

            break;

        }

        case 2: // 2, output in order according to the completion order

        {   

            for(i=0;i<n;i++)

            { 

printf("%d: %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);

                sum1+=a[i].zt;

                sum2+=a[i].dczt;

            }

            printf ("\ nAverage turnaround time:% .2f \ n", sum1 / n);

            printf ("\ nAverage weighted turnaround time:% .2f \ n \ n", sum2 / n);

            break;

        }

        default: break;

    }

}

【Test Results】

1. First-come-first-served scheduling:

 

 2. Short job priority scheduling:

【Experiment summary】

Through this course design, I have a solid grasp of the knowledge about the operating system, especially the process and various scheduling algorithms. Although process scheduling is low-level scheduling within the system, the quality of process scheduling directly affects the performance of job scheduling. The turnaround time and average turnaround time that reflect the merits of job scheduling only reflect the performance of process scheduling to a certain extent. For example, the execution time part actually includes the process wait time (including the wait time in the ready state), and the process The amount of waiting time depends on the process scheduling strategy and when the waiting event occurs. Therefore, the negotiation of process scheduling performance is an important indicator of operating system design. Therefore, the importance of process scheduling cannot be ignored.

Guess you like

Origin www.cnblogs.com/1314-520/p/12727422.html