Experiment 5: Process Scheduling Algorithm Simulation - RR

Simulation time slice rotation algorithm 1. Data structure used /* PCB */ struct PCB { pid_t pid;//process PID int state; //status information, 1 means running, 0 means pause, -1 means end unsigned long runned_time;//elapsed running time unsigned long need_running_time;//remaining running time}; /* PCB collection*/ struct PCB pcb[TOTAL]; //PCB collection 2, algorithm idea, algorithm implementation sub-main function (main) and dispatch function (Dispatch). (1) The core function of the main function (main) is: to realize the creation of 6 sub-processes and the initialization of the sub-process PCB. When initializing the child process PCB, the state is set to 0, and the running time is generated by random numbers. After the child process is created, it is suspended by the signal SIGSTOP, and can only continue to execute when selected by the dispatch function (Dispatch), and output the information that the child process x is executing. At the same time, a timer should be set in the main program. The interval of the timer is the length of the time slice. When the time slice is up, the dispatch function (Dispatch) will be called to re-select the program. (2) The core function of the dispatch function: suspend the executing child process, change the status to 0, and modify the running time and remaining running time. If the remaining time of the sub-process is less than or equal to 0, it means that the execution is completed, the PCB status is changed to -1, and the sub-process is terminated. Re-select the next sub-process, the state becomes 1, output the elapsed running time and remaining running time of the sub-process, and let the sub-process resume running. When all child processes end, the parent program ends

Relevant code:

operation result:

Guess you like

Origin blog.csdn.net/weixin_52357218/article/details/128619044