Experimental three-process scheduling simulation program

Source: Internet
Author: User

Experiment Three Process scheduling simulation program

13 Internet of things 201306104131 Wen Tianlin

1. Purpose and requirements

Experimental purpose

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

Experimental requirements

Design a process scheduling simulator with n (n not less than 5) processes executing concurrently.

Process scheduling algorithm: "Time slice rotation method" scheduling algorithm to n processes.

2. Experimental Content

The design, coding and debugging of two algorithms (Simple time slice rotation method and multistage feedback queue scheduling algorithm) are completed, and the experimental report is completed.

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 status of each process can be either ready, running R (Running), or completing one of the three states of F (finished).

3) The ready process can only run one time slice after acquiring the CPU. Represented by the elapsed CPU time plus.

4) If the elapsed CPU time of the 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, it should be inserted into the ready queue for the next dispatch.

5) Each time the schedule is scheduled, the program prints a run process, the PCB of each process in the ready queue for inspection.

6) Repeat the process until the process is complete.

3. Experimental results

4 source Code

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMAX 1005  6  7typedefstruct8 {9 Charname[4];//ProcessTen intPriority//Priority Level One intSttime;//start time arrives in the system A intNeedtime;//Run time required - intCPUTime;//CPU Time - CharState//process Status the }PR; -    - PR A[max]; - intN//Number of processes + intT//Time Slice size - voidinput () + { A     inti; atprintf"Enter the process name---priority--arrive at system time--run time: \ n"); -      for(i=0; i<n;i++) -     { -scanf"%s",&a[i].name); -scanf"%d",&a[i].priority); -scanf"%d",&a[i].sttime); inscanf"%d",&a[i].needtime); -A[i].cputime=0; toA[i].state='W'; +     } - } the   *   $ voidSort ()Panax Notoginseng { -    inti,j; the PR temp;  +       for(i=0; i<n-1; i++)  A      {   the           for(j=0; j<n-1-i;j++) +              if(a[j].sttime>a[j+1].sttime)//Sort by arrival system time -              { $   $temp=A[j]; -a[j]=a[j+1]; -a[j+1]=temp;  the              } -      }Wuyi      the       for(i=0; i<n-1; i++)  -      {   Wu           for(j=0; j<n-1-i;j++) -              if(a[j].priority<a[j+1].priority)//priority, high-priority number About              { $temp=A[j]; -a[j]=a[j+1]; -a[j+1]=temp; -              } A      } +      if(a[0].state!='F') the      {    -a[0].state='R';  $           the      } the } the   the voidoutput () - {   in    inti; theSort ();//sort, output print theprintf"\ t Process name--priority--arrival time--elapsed time--elapsed time--process state--------\ n"); About      for(i=0; i<n;i++) the     { theprintf"%8s%10d%10d%10d%10d%10c\n", a[i].name,a[i].priority,a[i].sttime,a[i].needtime,a[i].cputime,a[i].state); the     } +printf"\ n"); - } the  Bayi voidRR () the { the   -       Do{ -         if((a[0].needtime-a[0].cputime) >t)//If the remaining time is greater than the time slice the         { thea[0].cputime+=T; thea[0].priority--;//priority minus One thea[0].state='W';//after a time slice is executed, it becomes a wait . -         } the         Else the         { thea[0].cputime=a[0].needtime;//Elapsed time equals run time94a[0].priority=11111;//The priority is 11111, which indicates the completion process thea[0].state='F'; the         } theOutput ();//Output Results98} while(a[0].state!='F'); About } -  101  102 intMain ()103 {104printf"Please enter the number of processes N:"); thescanf"%d",&n);106printf"\ n");107printf"Please enter the time slice size T:");108scanf"%d",&t);109printf"\ n"); the input ();111Output ();//the input process is displayed in sorted order, and the function contains a sort algorithm inside the RR ();113}

5. Summary

Process implementation: Sort the input process first (by reaching the system time and priority), and then set the state of the first process to R to run after sequencing .

Because at any one time, you can only run the first process after the order is sorted. Each run of the process is only for the first process after the sequence of No. 0 elements, when the first process runs a time slice, the priority is reduced by one and then the process by the arrival of the system time and priority order, so the execution of the process is prioritized to 11111 after automatically queued to the back, and finally realize rotation.

In this process, I found that the time slices to sort RR where is not very understand, after asking the classmate, later solved the problem, I believe that through in-depth study, you can more familiar with the use of time slices and processes.

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