Experimental three-process scheduling simulation program--Operating system

Source: Internet
Author: User

A.    Purpose and Requirements 1.1.           Experiment objective to complete a process scheduler in high-level language to deepen the understanding of process concept and process scheduling algorithm. 1.2.           experimental requirements 1.2.1 Example: 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 beforehand, 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 a single time slice once the CPU is acquired. Represented by the elapsed CPU time plus. (5).  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, the priority number of the process should be reduced by 1 (that is, the lower one). 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. Thinking: What is the difference between job scheduling and process scheduling? 1.2.2 Experimental question A: Write and debug a simulated process scheduler, using the "highest priority priority" scheduling algorithm to the N (n not less than 5) of the process scheduling. The basic idea of the "highest priority priority" scheduling algorithm is to allocate the CPU to the process with the highest number of priorities in the ready queue. (1). The static precedence number is determined when the process is created and no longer changes during the entire process run. (2). The dynamic precedence number refers to the process's priority number, which can be given an initial value when the process is created, and the priority number can be modified by a certain rule. For example, when a process obtains a CPU, it reduces its priority by 1, and the process waits longer than a certain time period (2 timecode time) to increase its priority number, and so on.
#include <stdio.h> #include <stdlib.h> #include <time.h>struct pcb{int id;//identification code char* pname;   Process name int priority; Priority int arrtime;//arrival time int reqtime;//run time int usetime;//use CPU time char* statio;//process status} pcb[24];int systime=0;//    Priority sort void sort (int n) {int i,j; struct PCB temp; for (i=1;i<n;i++) for (j=i+1;j<=n;j++) if (pcb[i].priority<pcb[j].priority)//Sort by Priority {Temp=pcb[i        ];        PCB[I]=PCB[J];     Pcb[j]=temp;    }}void putresult (int n) {int i;printf ("\nid process name process priority process arrives time process uses CPU time process run time process state \ n"); for (I=1; i<=n; i++) {printf ("%d%5s%10d%12d%12d%15d%17s\n", Pcb[i].id,pcb[i].pname, Pcb[i].priority,pcb[i].arr    Time, Pcb[i].usetime,pcb[i].reqtime,pcb[i].statio);    }}//pseudo-random number generation data int pseudo_random_number () {int i,n;  Srand ((unsigned) time (0));    The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand ().    Number of input processes N=rand ()%5+2; for (I=1; i<=n; i++) {//Unique identification code pcb[i].id=i;//process name pcb[i].pname= "PCB";//Process Priority PCB[I].PRiority=rand ()%9+1;        Process arrival Time Pcb[i].arrtime=rand ()%29+1;    The time required for the process to complete pcb[i].reqtime=rand ()%7+1;//process uses CPU time pcb[i].usetime=0;//process State pcb[i].statio= "ready";       }putresult (n); return n;} Make a CPU call to void Cpurun (int n) {int i;while (n!=0) {for (i=1;i<=n;i++) {pcb[i].priority-=1; Pcb[i].reqtime-=1; Pcb[i].usetime+=1; pcb[i].statio= "runing"; if (pcb[i].reqtime!=0) {sort (n);p Utresult (n);} else{pcb[i].statio= "Finish";p Utresult (n); break;}}    n--;}  }main () {printf ("\t\t\t\t randomly generated process \ n"); int n=pseudo_random_number (); printf ("The number of \t\t\t processes is%d\n", N), sort (n);p rintf ("\t\t\t precedence sort \ n");p Utresult (n); Cpurun (n); return 0;}

  



Experimental three-process scheduling simulation program--Operating system

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.