Simulate a simple process scheduling Experiment

Source: Internet
Author: User

I. Purpose and requirements of the experiment
1. Write and debug a simulated process scheduling in C LanguageProgramTo deepen the concept of processes and Process SchedulingAlgorithmThe data structure and algorithms of the simulation process comply with the process scheduling rules of the mainstream operating systems (process rules are given starting from ).
2. Process Scheduling Algorithm: Use the highest priority scheduling algorithm (that is, the processor is assigned to the process with the highest priority ).
3. Each process has a process control block (PCB. The process control block can contain the following information: process name, priority, running time, CPU used time, Process status, and so on.
4. The priority and running time of a process can be specified manually (or generated by a random number ).
5. The status of each process can be w (wait), R (run), or F (finish.
6. The running time of a process is calculated in units of time slices.
7. The ready process can run only one time slice after obtaining the CPU. It is expressed by the CPU usage time plus 1.
8. use the dynamic priority rule of the highest priority algorithm to control the process: If the CPU time occupied by the process has reached the required running time after running a time slice, cancel the process, if the CPU time occupied by the process after running a time slice is not reached, that is, the process must continue to run, in this case, the priority of the process should be reduced by 1 (that is, the first level is reduced), and then it should be inserted into the ready queue to wait for the CPU.
9. Each time a scheduler runs a running process, a ready queue, and a PCB of each process for inspection.
10. Repeat the preceding process until all processes are completed.

Ii. experiment content and steps

1. Data Structure
1.1 Process Control Block Structure PCB: a struct defined by struct. Its definition is as follows:
Typedef struct PCB
{
Char QNAME [20];/* process name */
Char state;/* Process status */
Int super;/* process priority */
Int ndtime;/* time required for the process */
Int runtime;/* time when the process is running */
Int CPU;/* current time slice size obtained by the process */
} PCB;

1.2 queue node: the node stores PCB information, which is defined as follows:
Typedef struct Node
{
PCB data;/* node data */
Struct node * Next;/* pointer to the next node */
} Node;

1.3 The queue extended by the queue node is defined as follows:
Typedef struct queue
{
Node * front;/* First Team */
Node * rear;/* team end */
} Queue;

2. Related functions
2.1 function int is_empty (queue * q) used to judge whether a queue Q is null );
2.2 void enqueue (pcb x, queue * q) function for adding process control block X to queue Q );
2.3 Delete the first process of the queue Q, assign the value to X, and modify the status function void dequeue (PCB * X, queue * q );
This function deletes the first process in the queue Q. Because the process may not be completed, you need to enter the next priority queue. Therefore, first modify the member variables in the structure: the running time is the last running time plus the CPU time obtained this time; the priority is reduced by 1 (if the process is already at the lowest priority, it will be restored during the master process ); the time slice obtained next time is added with 1 for this time slice. Then, assign the modified process to a temporary PCB variable X to insert X into the next priority queue.
2.4 Main Function
The preceding data structures and functions are used to schedule simulated processes.

3. Process generation simulation

Generate a process through standard input: Enter the number of processes first, and then enter the process name, priority, and running time of each process in sequence.

Iii. ReferencesCode

# Include <stdio. h> # include <string. h> # include <malloc. h> # include <conio. h> # define pcb_len sizeof (PCB) # define node_len sizeof (node) # define queue_len sizeof (Queue) /* Process Control Block Structure PCB */typedef struct PCB {char QNAME [20];/* process name */Char state;/* Process status */INT super; /* process priority */INT ndtime;/* time required for the process */INT runtime;/* time when the process is running */int cpu; /* current time slice size obtained by the process */} PCB;/* queue node, node stores PCB information */typedef struct node {PCB data; struct node * Next;} n Ode;/* queue implementing process control blocks */typedef struct queue {node * front; node * rear;} queue; /* determine whether the queue is empty */INT is_empty (queue * q) {If (Q-> front) return 0; elsereturn 1 ;} /* Add Process Control Block X to queue Q */void enqueue (pcb x, queue * q) {node * P = (node *) malloc (node_len ); (P-> data ). state = x. state; (p-> data ). super = x. super; (p-> data ). ndtime = x. ndtime; (p-> data ). runtime = x. runtime; (p-> data ). CPU = x. CPU; strcpy (P-> data ). QNAME, X. QNAME); P-> next = 0; If (Q-> front) q-> rea R-> next = P; elseq-> front = P; q-> rear = P;}/* deletes the first process of queue Q, assign the value to X and modify the status */void dequeue (PCB * X, queue * q) {node * P = (node *) malloc (node_len ); if (is_empty (q) return;/* modify the status before entering the next priority queue */X-> state = 'W '; /* status changed to ready */strcpy (X-> QNAME, (Q-> front-> data ). QNAME);/* The run time is the last run time plus the CPU time obtained this time */X-> runtime = (Q-> front-> data ). runtime + (Q-> front-> data ). CPU;/* the priority is reduced by 1. If the process is already of the lowest priority, it will be restored during the master process */X-> super = (Q-> front-> data ). super-1; X-> ndtime = (Q-> fron T-> data ). ndtime;/* Add 1 */X-> CPU = (Q-> front-> data) to the time slice obtained next time ). CPU + 1; P = Q-> front; q-> front = Q-> front-> next; free (p);}/* master process */void main () {queue * queue = NULL;/* set the ready queue array */node * Wait = (node *) malloc (node_len); pcb x; int numberofcourse, I, j, super, time; int hight = 0, num = 0; int temp_ndtime, temp_runtime, temp_cpu; char name [20]; printf ("\ n please enter the total number of processes? "); Scanf (" % d ", & numberofcourse);/* opens up space for the queue array. Each array represents a queue of different priorities */queue = (queue *) calloc (numberofcourse, queue_len);/* enter the process information and initialize it, and add it to the corresponding priority queue */for (I = 0; I <numberofcourse; I ++) {printf ("\ n process no. % d \ n ", I); printf (" \ N input process name: "); scanf (" % s ", name); printf (" \ N input process priority: "); scanf (" % d ", & Super); If (Super> hight) hight = super; printf (" \ N input process running time :"); scanf ("% d", & time); strcpy (X. QNAME, name); X. state = 'W'; X. super = super; X. ndtime = time; x.ru Ntime = 0; X. CPU = 1; enqueue (x, & queue [super-1]);} printf ("\ n "); /* process scheduling process */for (I = hight-1; I> = 0; I --) {/* schedule the process from the highest priority queue until the queue is empty, the next priority queue is scheduled */while (! Is_empty (& queue [I]) {num ++;/* scheduling times */printf ("continue with one click ...... \ n "); getch (); printf (" the execute number: % d \ n ", num ); /* print the running process */(queue [I]. front)-> data ). state = 'R'; printf ("****** the current working process is: % s \ n", (queue [I]. front)-> data ). QNAME); printf ("QNAME state super ndtime runtime \ n"); printf ("% s", (queue [I]. front)-> data ). QNAME); printf ("R"); printf ("% d", (queue [I]. front)-> data ). super); printf ("% d", (queue [I]. front)-> data ). ndtime); printf ("% d \ n", (queue [I]. front)-> data ). runtime);/* after a process runs a time slice, the time required for running temp_time */temp_ndtime = (queue [I]. front)-> data ). ndtime; temp_runtime = (queue [I]. front)-> data ). runtime; temp_cpu = (queue [I]. front)-> data ). CPU; temp_ndtime = temp_ndtime-temp_runtime-temp_cpu;/* If the process has been completed */If (temp_ndtime <= 0) {/* print completed information, and delete it from the queue */printf ("process [% s] completed \ n", (queue [I]. front)-> data ). QNAME); (queue [I]. front)-> data ). state = 'F'; dequeue (& X, & queue [I]);}/* if the process is not completed */else {dequeue (& X, & queue [I]);/* delete it from the current queue * // * If the original priority is not the lowest priority, insert it to the next priority queue */if (I> 0) enqueue (x, & queue [I-1]);/* If the original priority is the lowest priority, insert it to the end of the current queue */else {/* because the priority is reduced by 1 in the delete operation, so recover here */X. super = x. super + 1; enqueue (x, & queue [I]) ;}/ * print the ready queue status */printf ("****** the current ready queue status is: \ n "); For (j = I; j> = 0; j --) {If (queue [J]. front) {Wait = queue [J]. front; while (wait) {printf ("QNAME state super ndtime runtime \ n"); printf ("% s", (wait-> data ). QNAME); printf ("W"); printf ("% d", (wait-> data ). super); printf ("% d", (wait-> data ). ndtime); printf ("% d \ n", (wait-> data ). runtime); wait = wait-> next ;}} printf ("\ n ");}} /* end */printf ("all processes have been completed \ n"); free (wait); free (Queue); getch ();}

Iv. Experiment results and data processing

[Input and output sample]

Enter the total number of processes? 4

Process No. no.0

Input process name: AA

Input Process Priority: 2

Input process running time: 2

Process No. No.1

Input process name: VV

Input Process Priority: 3

Input process running time: 2

Process No. No. 2

Input process name: rr

Input Process Priority: 1

Input process running time: 3

Process No. No. 3

Input process name: kk

Input Process Priority: 2

Input process running time: 1

Continue with one click ......
The execute number: 1

* ***** The current working process is vv.
QNAME state super ndtime Runtime
Vv r 3 2 0

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Aa w 2 2 0

QNAME state super ndtime Runtime
Kk W 2 1 0

QNAME state super ndtime Runtime
Vv w 2 2 1

QNAME state super ndtime Runtime
Rr W 1 3 0

Continue with one click ......
The execute number: 2

* ***** The current working process is aa.
QNAME state super ndtime Runtime
Aa r 2 2 0

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Kk W 2 1 0

QNAME state super ndtime Runtime
Vv w 2 2 1

QNAME state super ndtime Runtime
Rr W 1 3 0

QNAME state super ndtime Runtime
Aa w 1 2 1

Continue with one click ......
The execute number: 3

* ***** The current working process is: kk
QNAME state super ndtime Runtime
Kk R 2 1 0

Process [Kk] completed

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Vv w 2 2 1

QNAME state super ndtime Runtime
Rr W 1 3 0

QNAME state super ndtime Runtime
Aa w 1 2 1

Continue with one click ......
The execute number: 4

* ***** The current working process is vv.
QNAME state super ndtime Runtime
Vv r 2 2 1

Process [VV] completed

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Rr W 1 3 0

QNAME state super ndtime Runtime
Aa w 1 2 1

Continue with one click ......
The execute number: 5

* ***** The current working process is RR.
QNAME state super ndtime Runtime
Rr R 1 3 0

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Aa w 1 2 1

QNAME state super ndtime Runtime
Rr W 1 3 1

Continue with one click ......
The execute number: 6

* ***** The current working process is aa.
QNAME state super ndtime Runtime
Aa r 1 2 1

Process [AA] completed

* ***** The current ready queue status is:
QNAME state super ndtime Runtime
Rr W 1 3 1

Continue with one click ......
The execute number: 7

* ***** The current working process is RR.
QNAME state super ndtime Runtime
Rr R 1 3 1

Process [RR] completed

* ***** The current ready queue status is:

All processes have been completed

 

V. Summary

This program simulates the process scheduling process using standard input and output. Enter the priority and running time of each process, and output the running process, ready queue, and PCB information of each process for each scheduling, output the information about the process completed. Finally, when all processes are completed, the information about all processes completed is provided.

 

 

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.