[Operating system] Use C language to realize round-robin process scheduling (with code)

See the code at the end of the article

Rotation method realizes process scheduling idea:

  • Divide CPU processing time into fixed-size time slices
  • According to the order of the processes in the ready queue, schedule each process to use the CPU in turn
  • After each process executes a time slice, it releases the CPU, queues it to the end of the ready queue, and waits for the next scheduling

Use C language to realize the idea:

  • Each process in the system is represented by a process control block PCB
  • Arrange multiple processes in the ready queue linked list in the order of input (process information is entered from the keyboard)
  • Scheduled in sequence according to the order of the processes in the linked list, each scheduled process executes a time slice, and then returns to the ready queue, "elapsed running time" plus 1
  • If the process "required running time" == "elapsed running time", set its status to "end" and exit the queue
  • Run the program, display the process id that is scheduled to run each time, and the dynamic change process of each process control block

     

According to the idea of ​​the rotation method, the focus is that each process uses time slices and process queues in turn, so the process control block (PCB) needs to contain the following information:

  1. Process id, ie pid
  2. What needs to run is time, ie req_time
  3. The time it has been running, that is, cpu_time
  4. Status, R means the process is not over, E means the process is over
  5. Pointer next, pointing to the next pointer in the process queue

The PCB form is as follows:

The process queue is as follows:

       To sum up, we only need to use the structure to contain all the contents of the PCB to represent each process, and use the data structure as a chain structure to connect each process. When a process has a time period, the process's Add 1 to cpu_time, and use the next pointer to execute the next process. If the cpu_time and req_time are equal at this time, it means that the process is completed, and the process state is changed to "E", and the process is deleted from the process queue. Freeze your hands after speaking

//轮转法
typedef struct pcb {
	int pid;//进程id
	char state;//状态
	int total_time;//要求运行的时间
	int cputime;//已经运行的时间
	struct pcb* next;//next指针
}*proc;

int proc_num;
proc head, tail;//head是头,tail是尾

int init_pcb() {
	int i;
	proc p, tmp;
	//设置进程数量
	printf("please input the number of processes:\n");
	scanf("%d", &proc_num);
	printf("there are %d pricesses,please input pcb info:\n", proc_num);
	//初始化
	p = (proc)malloc(sizeof(struct pcb));
	printf("process id:");
	scanf("%d", &p->pid);
	printf("cputime required:");
	scanf("%d", &p->total_time);
	p->state = 'R';
	p->cputime = 0;
	//为头指针head
	head = p;

	//初始化中间的进程
	for (i = proc_num; i > 1; i--) {
		tmp = p;
		p = (proc)malloc(sizeof(struct pcb));
		printf("process id:");
		scanf("%d", &p->pid);
		printf("cputime required:");
		scanf("%d", &p->total_time);
		p->state = 'R';
		p->cputime = 0;
		tmp->next = p;
	}
	//最后一个进程为尾进程,并将尾指向头
	tail = p;
	p->next = head;
	return 0;
}

//输出每个进程的pid、已经运行的时间、总共需要的时间
void display() {
	int i;
	proc p = head;
	printf("pid\tcpu\treq_time\n");
	for (i = 0; i < proc_num; i++) {
		printf("%d\t%d\t%d\n", p->pid, p->cputime, p->total_time);
		p = p->next;
	}
}

//tail和head是全局变量
void sched() {
	int round = 1;
	proc tmp = tail;
	proc p = head;
	//从头开始执行每一个进程
	while (p->total_time > p->cputime) {
		printf("\nRound %d, Process %d is running\n", round, p->pid);
		p->cputime++;
		display();
		//一个进程完成
		if (p->total_time == p->cputime) {
			p->state = 'E';
			proc_num--;
			tmp->next = p->next;
			if (p == head) {
				head = p->next;
			}
			printf("process %d is finished\n", p->pid);
		}
		else
			tmp = p;
		p = p->next;
		round++;
	}
}

int main() {
	init_pcb();
	display();
	sched();
	return 0;
}

C language implementation of process scheduling algorithm based on dynamic priority See you next time! ! ! !

Guess you like

Origin blog.csdn.net/peng_lv/article/details/127700112