Process Management Demo

Source: Internet
Author: User
Tags rand

First, the design content

Design a process management simulation system that allows n processes to run concurrently. The system includes simple process control, synchronization and communication mechanism, and its process scheduling algorithm can be arbitrarily selected (priority scheduling, time slice rotation, one of the short process priorities). Each process is represented by a PCB and its contents are set according to the specific situation. There is a certain synchronization relationship between the processes (optional). The system should be able to display or print the status of each process and the change of relevant parameters in order to observe the process of operation and the management process of the system.


twoDesign Guide1) Data structures used in the experiment:

(1) PCB Process Control block

These include the parameter ① process name Name;② requires run time Runtime;③ priority Prior;④ State State;⑤ elapsed time runedtime, and so on.

(2) For simplicity, only run queue, ready list, block queue three data structures, process scheduling in these two queues switch, 1.1 is shown




2) Flowchart of process Management




Figure 1.2 Process Management flowchart





third, the source program
#include "stdlib.h" #include <stdio.h> #include <iostream> #include <time.h>using namespace std;     typedef struct pcb{int name; int runtime; int runedtime; int state; int killtime; int waitpoint; int waittime;struct PCB *next;} PCB, #define NUM 10PCB *runqueue=null;  Run the queue PCB *top=null,*tail=null,*temp,*temp_pre;   Ready-to-queue PCB *top_wait=null,*tail_wait=null;int timeslice=2;void CreateProcess () {int I;srand (10);         for (i=0;i<num;i++) {temp=new PCB; temp->name=i; Temp->runtime=rand ()%20+1;         temp->runedtime=0; temp->next=null;         temp->killtime=0; Temp->waitpoint=rand ()%temp->runtime-1; Temp->waittime=rand ()%20+1;if (i==0) {top=temp; tail=temp;} else{tail->next=temp;tail=temp;} printf ("Process name%d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%d\n",tail->name,tail-> Runtime,tail->runedtime,tail->killtime,tail->waitpoint,tail->waittime); }}void processschedule () {int Killtime_temp=0;while (Top!=null| |  Top_wait!=null) {if (top)//Schedule {//Select a node from the ready queue, insert the run queue runqueue=top;      top=top->next;  runqueue->next=null; runqueue->waitpoint=runqueue->waitpoint-timeslice;  if (runqueue->waitpoint>0)//does not produce blocking {runqueue->runtime= runqueue->runtime-timeslice;  if (runqueue->runtime<=0) {//process will run to end printf ("process%d takes up processor, will destroy \ n", runqueue->name);    runqueue->killtime=runqueue->runtime+timeslice;  The process consumes processor Time runqueue->runedtime=runqueue->runedtime+runqueue->killtime;   The process takes a total of processor time killtime_temp=runqueue->killtime;runqueue->runtime=0; The process also needs to occupy the processor's time printf ("Process name%d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%d\n", Runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime,runqueue->waitpoint,    Runqueue->waittime);//To end the process, free the node space//..... ...}        else{printf ("process%d occupancy processor \ n", runqueue->name);                runqueue->killtime=timeslice; Killtime_temp=runqueue->killtime; runqueue->runedtime=runqueue->runedtime+runqueue->killtime;printf ("Process name%d, runtime=%d, runedtime =%d,killtime=%d, waitpoint=%d,waittime=%d\n ", Runqueue->name,runqueue->runtime,runqueue->runedtime,      Runqueue->killtime,runqueue->waitpoint,runqueue->waittime);//insert the running queue into the ready queue tail->next=runqueue;  tail=tail->next; }} else {printf ("process%d is blocked!")  Will wake up after%ds!\n ", runqueue->name,runqueue->waittime);  runqueue->killtime=runqueue->waitpoint+timeslice;  runqueue->runedtime=runqueue->runedtime+runqueue->killtime;  runqueue->runtime=runqueue->runtime-runqueue->killtime;    runqueue->waitpoint=100; The run queue is checked to the blocking queue if (tail_wait==null) {printf ("The Blocking queue is empty!").  \ n ");  Tail_wait=runqueue;  Top_wait=runqueue;  } else {tail_wait->next=runqueue;   tail_wait=tail_wait->next;  }} Runqueue=null;  } if (top_wait!=null) {temp=top_wait; Temp_pre=null; while (temp!=null)//scan blocking queue,Whether there is a wait time to the node {temp->waittime=temp->waittime-killtime_temp;if (temp->waittime<=0) {printf ("process%d has been awakened, Join the Ready queue! \ n ", temp->name);//node removed from ready queue, insert ready queue tail->next=temp;tail=tail->next;if (temp_pre) {temp_pre->next= Temp->next;//free (temp); temp=temp_pre->next;} Else{//temp is top, temp_pre=nulltop_wait=temp->next;//free (temp); Temp=top_wait;if (tail_wait!=null) printf (" Block queue head%d\n ", tail_wait->name); elseprintf (" Block queue is empty \ n ");} if (temp==null) tail_wait=null;}     Else{temp_pre=temp;     temp=temp->next;  }}} if ((Top==null) && (top_wait!=null)) {temp=top_wait;     while (temp!=null)//scan blocking queue, whether there is a waiting time to the node {temp->waittime-10; temp=temp->next; }}}}int Main (int argc, char* argv[]) {CreateProcess (); Processschedule ();p rintf ("Hello world!\n"); return 0;}



Process Management Demo

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.