9.2时间片轮转进程调度

该程序定义了一个PCB(ProcessControlBlock)结构体,用于存储进程的标识符、已运行时间、要求运行时间、状态和链接指针。通过用户输入为每个进程分配运行时间,然后初始化一个循环链表表示进程队列。在操作函数中,模拟进程调度,更新进程状态和运行时间。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<stdlib.h>

struct PCB{
	int pid;//进程标识符
	int rr;//已运行时间
	int time;//进程要求运行时间
	char sta;//进程的状态
	struct PCB * next;//链接指针
};

struct PCB pcb1,pcb2,pcb3,pcb4pcb5,*tail,*head,*rp;

//初始化各个进程的运行时间
void init(){
	int time;
	pcb1.pid = 1;
	pcb2.pid = 2;
	pcb3.pid = 3;
	pcb4.pid = 4;
	pcb5.pid = 5;

	pcb1.rr = pcb2.rr = pcb3.rr = pcb4.rr = pcb5.rr = 0;

	pcb1.sta = pcb2.sta = pcb3.sta = pcb4.sta = pcb5.sta = 'w';
	printf("请输入进程p1需要运行的时间:");
	scanf("%d",&time);
	pcb1.time = time;

	printf("请输入进程p2需要运行的时间:");
	scanf("%d",&time);
	pcb2.time = time;

	printf("请输入进程p3需要运行的时间:");
	scanf("%d",&time);
	pcb3.time = time;

	printf("请输入进程p4需要运行的时间:");
	scanf("%d",&time);
	pcb4.time = time;

	printf("请输入进程p5需要运行的时间:");
	scanf("%d",&time);
	pcb5.time = time;

	pcb1.next = &pcb2;
	pcb2.next = &pcb3;
	pcb3.next = &pcb4;
	pcb4.next = &pcb5;
	pcb5.next = &pcb1;

	head = &pcb1;
	tail = &pcb5;
}


//显示表头
void printf1(){
	printf("+ ------------ | ------------ | ------------ | ------------ +\n");
	printf(""\tpid\trr\ttime\tSTA\t|\n);
	printf("| ------------ | ------------ | ------------ | ------------ |\n");

}

void printf2(){
	printf("processes p %d running\n",head->pid);
	printf1();
	printf("|\t%d\t|\t%d\t||\t%d\t||\t%c\t|\n",head->pid,head->rr,head->time,head->sta);
	printf("| ------------ | ------------ | ------------ | ------------ |\n");
	rp = head;
	while(rp!=tail){
		rp = rp->next;
		printf("|\t%d\t|\t%d\t||\t%d\t||\t%c\t|\n",rp->pid,rp->rr,rp->rime,rp->sta);
		printf("| ------------ | ------------ | ------------ | ------------ |\n");
	}

}



void operation(){
	int flag = 1;
	while(flag<=5){
		head->rr++;
		if((head->rr==head->time)||(head->time==0)){
			tail->sta = 'w';//将进程状态设置为等待状态
			head->sta = 'f';//将进程状态设置为执行态
			printf2();
			head = head->next;
			tail->next = head;
			flag++;
		}else{
			tail->sta = 'w';//将进程状态设置为等待状态
			head->sta = 'r';//将进程状态设置为就绪态
			printf2();
			tail = head;
			head = head->next;
		}
	}
}

void main(){
	init();
	printf2();
	operation();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值