ArticleDirectory
- Job description
- Background
Job 2: Process PCB management and scheduling Program
System management processes in the system all run activities are inseparable from the process PCB operations, this job is the process of PCB as the management goal, to achieve the process scheduling status change operations.
Job description
Use the data structure struct PCB to indicate the process and record the information about the process. The process information includes the process internal identifier PID, process name pname, Process status pstatus,
Process running time ptime and process queue pointer pnext. Please design a reasonable data structure to accommodate this information. The following is an exampleCode.
# Define running0/* run */# define ready1/* ready */# define finished2/* ended */# define empty3/* PCB table idle */struct PCB {int PID; char pname [8]; int pstatus; int ptime; PCB * pnext ;};
Set three queues (linked list): ready queue, execution queue, and idle PCB queue.
The program first creates an idle PCB queue during initialization, and then creates a process based on the number of processes entered by the user and forms a process readiness queue.
Use a linked list to represent the queue where the process is located.
Processes are executed in turn in sequence. Each process executes m time slices at a time, and m is greater than or equal to 1.
The initial ptime value in the process PCB is the time required for the process to run. The initial value can be assigned an integer randomly:
Ptime = ptime-m; If (ptime <= 0) execution --> complete;/* process transition from execution state to completion */
Until ptime is smaller than or equal to 0, it indicates that the process can be completed in the last allocated time.
Write basic management functions, including adding insert_pcb (), deleting del_pcb (), and modifying mod_pcb ().
Displays information about process scheduling and status changes.
Here is an example Program (click to download). If you do not quite understand the meaning of the question, you can run this example program to see if you can understand the output result.
Background
1. Process Control Block PCB. PCB is an important data structure of processes, including important control information, such as process identification, processing site, Process status, and resource list.
2. queue and linked list. You can use a linked list to add, modify, and delete nodes in a queue.
3. Process status. The basic status of a process includes execution, readiness, and blocking. After being introduced, the process becomes executed, the activity is ready, the activity is blocked, the static process is ready, and the static process is blocked.
The activities of a process in the lifecycle change in these statuses.
/* Optional /*----------------------------------------------------------------------------------------------------------------*/
The implementation is as follows:
The following code is basically implemented based on the organization of process control blocks (A. Link mode.
Program Implementation is relatively simplified, such as no blocking queue.
Based on this structure, it is considered that the PCB zone opened up by the system is stored in a continuous storage structure.
The following code is based on a static linked list (idle PCB queue, ready queue,
Because of this structure, under the premise of Simulating with the data structure, if dynamic pointers are used (if the PCB is connected with a chain pointer, the dynamics are better), it may seem bloated.
The main idea is clear.
The Code is as follows:
# Include <cstdio> # include <cstdlib> # include <cstring> # include <ctime> # include <iostream> using namespace STD; # ifdef Linux # include <unistd. h> # define sleep (n) # endif # ifdef Win32 # include <windows. h> # define sleep (n) # endif # define num 6 // the default system PCB Area has 6 PCB int each_time = 1; int n = 0; char sname [6] [3] = {"p1", "p2", "P3", "P4", "P5", "P6"}; typedef Enum {running, ready, finished, empty} pstatus ;/ /The enumerated type typedef struct process {int PID; char pname [8]; int pstatus; int ptime; int pnext; process () {pid =-1; pstatus = empty; ptime = 0; pnext =-1; memset (pname, 0, sizeof (pname) ;}} process; process PCB [num], pre_pcb [num]; typedef struct pqueue {int top; int rear;} pqueue; pqueue ready, pre_ready; pqueue pfree; int run, pre_run; void creat_pcb (int pid) {PCB [pid]. pstatus = ready; strcpy (PCB [pid]. pname, SNA Me [n ++]); PCB [pid]. ptime = rand () % 8 + 1; PCB [pid]. pnext =-1;} // assign the PCB to the process void push_pqueue (int pid, pqueue & cur) {If (cur. top =-1) {cur. top = PID;} else {PCB [cur. rear]. pnext = PID;} cur. rear = PID; PCB [cur. rear]. pnext =-1;} // push-in queue, ready queue or idle queue int pop_pqueue (pqueue & cur) {If (cur. top =-1) {printf ("the queue is empty, error. \ N "); Return-1;} int pid = cur. top; cur. top = PCB [pid]. pnext; If (cur. top =-1) {cur. rear =-1;} PCB [pid]. pnext =-1; return PID;} // pop-up queue void del_pcb (int pid) {PCB [pid]. ptime = 0; PCB [pid]. pnext =-1; push_pqueue (PID, pfree);} // reclaim the PCB and restore it to the idle state void insert_pcb () {If (pfree. top =-1) {puts ("the idle queue is empty, failed to create the process"); return;} int pid = pop_pqueue (pfree ); // obtain the PCB creat_pcb (PID) from the idle PCB queue; // assign the PCB to the process push_pqueue (PID, ready ); // Enter the ready queue} // create the process void print_status (process state [], pqueue cur, int ran); void modify_pcb () {// process scheduling if (ready. top =-1) {puts ("No process can be scheduled in the system"); return;} pre_run = run; pre_ready = ready; int pid = pop_pqueue (ready ); run = PID; PCB [pid]. pstatus = running; puts ("previous status:"); print_status (pre_pcb, pre_ready, pre_run); puts ("Current status:"); print_status (PCB, ready, run ); // output status for (INT I = 0; I <n; I ++) {pre_pcb [I] = PCB [I];} // The Precursor that saves the current state. If (PCB [pid]. ptime-each_time> 0) {PCB [pid]. ptime-= each_time; PCB [pid]. pstatus = ready; push_pqueue (PID, ready);} else {PCB [pid]. ptime = 0; PCB [pid]. pstatus = finished; del_pcb (PID) ;}// modify the PCB status and save the field (ptime only)} void init_pcb () {int I; n = 0; each_time = rand () % 6 + 1; // randomly generated time slice size for (I = 0; I <num; I ++) {PCB [I]. PID = I; PCB [I]. pnext = I + 1;} PCB [NUM-1]. pnext =-1; pfree. top = 0; pfree. rear = num-1; Ready. top =-1; ready. rear =-1; run =-1;} // The initialization status of the System PCB Area void print_status (process state [], pqueue cur, int ran) {puts ("average worker "); puts ("Worker Process Name Process status remaining execution time worker"); For (INT I = 0; I <n; I ++) {printf ("bytes \ t % s \ t", State [I]. pname); Switch (State [I]. pstatus) {Case running: printf ("running \ t"); break; case ready: printf ("Ready \ t"); break; case finished: printf ("finished \ t"); break; default: B Reak;} printf ("% d \ t success \ n", State [I]. ptime);} puts ("Please wait until there are too many threads when there are too many threads "); printf ("commandid ready_queue:"); For (INT I = cur. top; I! =-1; I = State [I]. pnext) printf ("% s->", State [I]. pname); printf ("null \ n"); printf ("queue active_queue:"); If (ran! =-1) printf ("% s->", State [ran]. pname); puts ("null");} // output status function void speed (INT v) {Switch (v) {Case 1: Sleep (500); break; case 2: Sleep (1600); break; Case 3: Sleep (2400); break; default: break; }}// sub-files in the automatic Demo mode (pause interval) function int check_input (char STR []) {int I, K = 0; int ST =-1, ED =-1; char stmp [110]; for (I = 0; STR [I]; I ++) if (STR [I]! = '') {ST = I; break;} for (I = strlen (STR)-1; I> = 0; I --) if (STR [I]! = '') {Ed = I; break;} If (ST =-1) Return-2; for (I = sT; I <= Ed; I ++) {If (! (STR [I]> = '0' & STR [I] <= '9') {return-1 ;} else {stmp [k ++] = STR [I] ;}} int r = 1, sum = 0; for (I = k-1; I> = 0; I --, R * = 10) {sum + = (stmp [I]-'0') * R;} return sum;} // input processing, do a simple troubleshooting void solve () {int N; char STR [110]; puts ("Number of input processes (in the range of [1-6 ):"); while (gets (STR) {While (n = check_input (STR) <0 | n> 6) {If (n =-1 | n> 6) {// input check processing puts ("error, please input again") ;}gets (STR) ;}init_pcb ();/ /Initialize for (INT I = 0; I <n; I ++) {insert_pcb () ;}for (INT I = 0; I <n; I ++) {pre_pcb [I] = PCB [I];} // Save the previous state information puts ("Initial Process status:"); print_status (PCB, pre_ready, run ); printf ("zookeeper time slice size: % d \ n", each_time); int mode = 1; int v = 1; puts ("\ n select Demo mode: 1 (automatic) 2 (manual) "); gets (STR); While (! (Mode = check_input (STR) >=1 & mode <= 2) {If (mode! =-2) {puts ("error, please input again"); // input check processing} gets (STR);} If (mode = 1) {puts ("1: quick demo \ t2: Medium Speed demo \ T3: Slow Demo"); gets (STR); While (! (V = check_input (STR) >=1 & V <= 3) {If (V! =-2) {puts ("error, please input again"); // input check processing} gets (STR) ;}} while (true) {system ("CLS"); modify_pcb (); // process scheduling if (ready. top =-1) {// The ready queue is empty. If (mode = 2) {puts ("click to next"); System ("pause") ;}break ;}if (mode = 1) {speed (V); // automatic mode pause} else {puts ("click to next"); System ("pause "); // manually flip pages in manual mode} If (mode = 1) speed (V); // pause system ("CLS") in automatic mode; puts ("previous status: "); print_status (pre_pcb, ready, run); puts (" Current status: "); For (INT I = 0; I <n; I ++) PCB [I]. pstatus = finished; run =-1; print_status (PCB, ready, run); // completion status puts ("Congratulation !!! All processes are successfully run! "); System (" pause "); System (" CLS "); puts (" Number of input processes (in the range of [1-6 ):");}} int main () {srand (unsigned) Time (null); solve (); Return 0 ;}
The following is the. exe executable file of the compression code (the file cannot be uploaded, And the RAR file is compressed ).
Click Download.
12.5-re-wrote a program to connect the PCB with a pointer chain. The pseudo interface is simpler.
The main features of this program are simple and easy to understand, which simplifies the processing of some floating clouds;
Sighing that you do not need pointers at ordinary times makes it seem unfamiliar to start with pointers. In the process, for example, memory cannot be read many times.
However, the program is still clear, and the scheduling function modify_pcb () is clearer than the above program.
(The above write stamp is used. The last output must be specially processed outside.-Is not required here -).
The Code is as follows:
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <time. h> # define num 6 # define running 0/* run */# define ready 1/* ready */# define finished 2/* end */# define empty 3/* PCB table idle */INT each_time = 1; // The default time slice is 1 int n = 0; char sname [6] [3] = {"p1", "p2", "P3", "P4 ", "P5", "P6"}; typedef struct PCB {char pname [8]; int pstatus; int ptime; PCB * pnext;} PCB, * queueptr; typedef struct {queueptr Front; queueptr rear;} linkqueue; linkqueue p_empty, p_ready, p_run, p_end; void initqueue (linkqueue & Q) {q. front = Q. rear = (queueptr) malloc (sizeof (PCB);} // initialize the queue void enqueue (linkqueue & Q, queueptr p) {q. rear-> pnext = P; q. rear = P; P-> pnext = NULL;} // press the queue void dequeue (linkqueue & Q, queueptr & P) {If (Q. front = Q. rear) {printf ("error, the queue is empty! ");} P = Q. front-> pnext; q. front-> pnext = p-> pnext; P-> pnext = NULL; If (Q. rear = P) Q. rear = Q. front;} // output queue. Use P to save void insert_pcb () {queueptr P; dequeue (p_empty, P ); // retrieve the PCB enqueue (p_ready, p) from the idle queue; // Add it to the ready queue strcpy (p-> pname, sname [n ++]); p-> pstatus = ready; P-> ptime = rand () % 8 + 1; // initialization information, name, status // process execution time (RAND randomly generated, rand () % 8 + 1 in the range of 1-8) printf ("create process % s, execution time % d <created successfully> \ n", p-> pname, p-> ptime);} // create Cheng void del_pcb (queueptr & P) {P-> ptime = 0; P-> pstatus = finished; P-> pnext = NULL; enqueue (p_end, P );} // Delete the process and add it to the completion status queue. Void modify_pcb () {// process scheduling queueptr cur; puts ("====================== execution scheduling =================="); If (p_run.front! = P_run.rear) {// The execution queue is not empty. dequeue (p_run, cur); // The Execution Process cur-> ptime-= each_time is displayed; // execute a time slice printf ("scheduling: process % s ", cur-> pname); If (cur-> ptime> 0) {// if the execution is not completed, it is scheduled to the ready state cur-> pstatus = ready; printf ("<run-> ready>. \ n "); enqueue (p_ready, cur);} else {// execution ends printf (" <execution-> execution ends>. \ n "); del_pcb (cur); // Add to end queue} If (p_ready.front! = P_ready.rear) {// dequeue (p_ready, cur) is not empty in the ready queue; // enqueue (p_run, cur) is the first element of the ready queue ); // enter the execution state cur-> pstatus = running; printf ("scheduling: Process % S <ready-> execution>. \ n ", cur-> pname); printf (" scheduling: % s executing... \ n ", cur-> pname); // schedule the execution of printf (" ready queue: "); queueptr ready = p_ready.front-> pnext; while (ready) {printf ("% S <1>->", ready-> pname); ready = ready-> pnext;} // output ready queue puts ("null "); printf ("execution queue:"); printf ("% S <0>-> null \ n", cur-> pname); // Output execution queue} void init_pcb () {initqueue (p_empty); initqueue (p_ready); initqueue (p_run); initqueue (p_end ); // initialize the queue for (INT I = 0; I <num; I ++) {queueptr P = (queueptr) malloc (sizeof (PCB )); p-> pnext = NULL; enqueue (p_empty, P);} // The initial idle queue is 6 idle PCB} void solve () {int N; printf ("Number of processes? [1-6]: "); scanf (" % d ", & N); If (n <1 | n> 6) {puts ("input data not in 1-6"); return;} init_pcb (); // initialize the queue for (INT I = 0; I <n; I ++) {insert_pcb (); // creation process} while (true) {modify_pcb (); // Process Scheduling (if the ready queue is not empty, p_run is not empty after scheduling) if (p_run.front = p_run.rear) {break; // No process exits during execution.} Queueptr run; printf ("End queue:"); queueptr end = p_end.front-> pnext; while (end) {printf ("% S <1>-> ", end-> pname); End = end-> pnext;} puts ("null"); // output end Queue (in order of termination)} int main () {srand (unsigned) Time (null); // use time as random seed solve (); Return 0 ;}
If the original article is reproduced, please note: transferred from ¥ forgot % windHttp://www.cnblogs.com/slave_wc}
Address: job 2: Process PCB management and scheduling program