• 时间片轮转进程调度


    #include <cstdio>
    #include <cstdlib>
    #define N 6
    struct PCB
    {
    	int pid;			// 进程标识符
    	int rr;				// 已运行时间
    	int time;			// 进程要求运行时间
    	char state;			// 进程的状态
    	struct PCB * next;	// 链接指针 
    };
    struct PCB pcb[N];
    struct PCB *tail, *head, *rp;
    
    void init()
    {
    	int time;
    	for(int i = 1; i < N; ++ i)
    	{
    		pcb[i].pid = i;
    		pcb[i].rr = 0;
    		pcb[i].state = 'w';
    		printf("请输入进程p%d需要运行的时间:", i);
    		scanf("%d", &pcb[i].time);
    	}
    	pcb[1].next = &pcb[2];
    	pcb[2].next = &pcb[3];
    	pcb[3].next = &pcb[4];
    	pcb[4].next = &pcb[5];
    	pcb[5].next = &pcb[1];
    	head = &pcb[1];
    	tail = &pcb[5];
    	
    }
    
    // 显示表头 
    void print1()
    {
    	printf("+---------------|---------------|---------------|---------------+
    ");
    	printf("|	pid	|	rr	|	time	|	STATE	|
    ");
    	printf("|---------------|---------------|---------------|---------------|
    ");
    } 
    
    // 显示各个进程的初始状态
    void print2()
    {
    	printf("processes p %d running
    ", head->pid);
    	print1();
    	printf("|	%d	|	%d	|	%d	|	%c	|
    ", head->pid, head->rr, head->time, head->state);
    	printf("|---------------|---------------|---------------|---------------|
    ");
    	rp = head;
    	while(rp != tail)
    	{
    		rp = rp->next;
    		printf("|	%d	|	%d	|	%d	|	%c	|
    ", rp->pid, rp->rr, rp->time, rp->state);
    		printf("|---------------|---------------|---------------|---------------|
    ");
    	}
    } 
    
    // 运行
    void operation()
    {
    	int flag = 1;
    	while(flag <= 5)
    	{
    		head->rr ++;
    		if((head->rr == head->time) || (head->time == 0))
    		{
    			tail->state = 'w';		// 将进程状态设置为等待态
    			head->state = 'f';		// 将进程状态设置为终止态 
    			print2();
    			head = head->next;
    			tail->next = head;
    			flag ++;
    		}
    		else
    		{
    			tail->state = 'w';		// 将进程状态设置为等待态
    			head->state = 'r';		// 将进程状态设置为运行态
    			print2();
    			tail = head;
    			head = head->next;
    		}
    	}
    } 
    
    int main()
    {
    	init();
    	print2();
    	operation();
    	
    	return 0;
    } 
    

      

  • 相关阅读:
    linux安装nodejs
    Ubuntu下配置TFTP服务以及 android下使用TFTP
    笔记-《数据通信与网络教程》-第一章
    X86汇编基础-《Linux内核分析》云课堂笔记
    文章点击量排行TOP100-IBM power8算法挑战赛第三期
    LeetCode:Climbing Stairs
    LeetCode:Search for a Range
    LeetCode:Longest Substring Without Repeating Characters
    LeetCode:Linked List Cycle II
    LeetCode:Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/mjn1/p/10710326.html
Copyright © 2020-2023  润新知