进程PCB管理与调度程序

这是操作系统课上的一次作业,我是属于旁听去的,也许正是由于旁听的缘故,对作业总是抱着一中拖延的心态,没花什么心思在这门课的作业上。这次的进程调度作业其实早已经上交了,这两天想写点程序了,顺便更新下博客里面的内容。闲话少说,上程序了,不会组织语言,只能多写点注释了

基本构造:

使用数据结构struct PCB表示进程, 记录进程的相关信息,设置三个队列(链表),分别是就绪队列、执行队列和等待PCB队列。程序初始化的时候先创建空闲PCB队列,然后根据用户输入的进程数量创建进程并构成进程就绪队列。随机调度进程,最后结束调度,输出所有进程状态。

#define RUNNING    0  
#define READY      1  
#define WAIT	   2  
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>

typedef struct PCB
{
	int pid;//进程的标识号
	int priority;//进程的优先级,数值越大,优先级越高
	int pstatus;//进程的状态
	struct PCB * next;//指向下一个进程的指针
}PCB;

PCB * Create(int n)
{
	//head指向头节点的指针,cur表示指向当前结点的指针
	PCB * head,* temp,* cur;
	int i;
	head=(PCB *)malloc(sizeof(PCB));//分配空间
	if(head==NULL)//分配失败
	{
		printf("ERROR");
		exit(1);
	}
	cur=head;head->next=NULL;//头结点
	for(i=0;i<n;i++)
	{
		temp=(PCB *)malloc(sizeof(PCB));
		if(!temp)
		{
			printf("ERROR");
			exit(1);
		}
		temp->pid=i+1; //初始化进程的标识号
		temp->pstatus=READY;//进程刚创建时,初始化其状态为"就绪态"
		printf("\n请输入进程 %d 的优先级别(Int型) :",temp->pid);
		scanf("%d",&temp->priority);//由用户输入进程的优先级并初始化,写入对应的PCB表
		temp->next=NULL;
		cur->next=temp;
		cur=cur->next;
	}
	return head;//返回指向头结点的头指针
}

void Insert(PCB * head,PCB * cur)
{
	if(cur==NULL)
		return;
	for(;head->next!=NULL;head=head->next);
	cur->next=NULL;
	head->next=cur;
}

PCB * readyTorun(PCB * head)
{
	PCB * temp1=head,* temp=head->next;
	if(head->next==NULL)
	{
		printf("The Ready is NUll!");
		temp=NULL;
	}
	for(;head->next!=NULL;head=head->next)
	{
		if(temp->priority < head->next->priority)//当前优先级小于下一结点优先级
		{
			temp1=head;
			temp=head->next;//temp记录选中的进程的PCB
		}
	}
	if(temp!=NULL)
		temp1->next=temp->next;//从就绪进程的PCB链表中去除选中进程的PCB
	return temp;//返回该结点
}

void blockToready(PCB * ready,PCB * wait,int j)
{
	PCB * temp1=wait,* temp=wait->next;
	if(wait->next==NULL)
	{
		printf("The Wait is Null!");
	}
	for(;wait->next!=NULL;wait=wait->next)//寻找指定进程的PCB
	{
		if(wait->next->pid==j)
		{
			temp1=wait;
			temp=wait->next;
		}
	}
	if(temp!=NULL)
	{
		temp1->next=temp->next;//从等待PCB链表中去除选中的PCB
		temp->pstatus=READY;//将选中进程的PCB状态改为就绪态
	}
	Insert(ready,temp);//向就绪PCB链表中插入选中的PCB
}

void print(PCB * head)
{
	PCB * temp=head->next;
	if(head->next==NULL)
	{
		printf("It is NULL");
		return;
	}
	for(;temp!=NULL;temp=temp->next)
	{
		printf("p<%d>  ",temp->pid);
	}
}

void Display(PCB * r)
{
	for(;r->next!=NULL;r=r->next)
	{
		printf("\np<%d> information : PID: %d  Priority: %d  Pstatus: %d",r->next->pid,r->next->pid,r->next->priority,r->next->pstatus);
	}
}

void Destroy(PCB * head)
{
	PCB * temp=head->next,* cur=NULL;
	if(temp==NULL)
		return;
	for(;temp!=NULL;)
	{
		cur=temp;
		temp=temp->next;
		free(cur);
	}
}
int main()
{
	int n,k=1,j;
	struct PCB * ready_list=NULL,* wait_list=NULL,* run=NULL;
	wait_list=(struct PCB *)malloc(sizeof(struct PCB));
	wait_list->next=NULL;
	printf("请输入准备创建的进程数 :");
	scanf("%d",&n);
	ready_list=Create(n);//根据用户输入,创建进程
	run=readyTorun(ready_list);//从就绪PCB链表中选择优先级最高的进程,进入CPU运行
	srand((unsigned)time(NULL));//产生一个随机种子
    while(k!=0)
	{
		
		int i = rand() % 4 ;//在0-3中随机产生一个数,0表示退出
		k=i;
        printf("\n=================进程调度==================");
		if(run!=NULL)
		{
			run->pstatus=RUNNING;
			printf("\np<%d> is Running ......",run->pid);
		}
		printf("\nThe Ready : ");
		print(ready_list);
		printf("\nThe Wait : ");
		print(wait_list);
		switch(i)
		{
		case 1 : 
			run->pstatus=READY;
			Insert(ready_list,run);//将当前运行态的进程转为就绪态
			run=readyTorun(ready_list);//系统自动从就绪PCB链表选择高优先级进程投入运行态
			break;
			
		case 2 :
			run->pstatus=WAIT;
			Insert(wait_list,run);//将当前运行态的进程转为等待态
			run=readyTorun(ready_list);//系统自动从就绪PCB链表选择高优先级进程投入运行态
			break;
			
		case 3 : 
			printf("\n输入等待态(Wait)转换为就绪态(Ready)的进程标识号 : ");//将指定的等待态进程转为就绪态
			scanf("%d",&j);
			blockToready(ready_list,wait_list,j);
			break;
			
		default :break;
		}
		
	}
	//调度结束,输出创建进程的相关信息
	printf("\n进程模拟调度结束\n");
	printf("\n所有进程信息详细信息 :\n");
	Display(wait_list);
	Display(ready_list);            
	printf("\n p<%d> information : PID: %d  Priority: %d  Pstatus: %d\n",run->pid,run->pid,run->priority,run->pstatus);
	
	//销毁创建进程的链表
	Destroy(ready_list);
	Destroy(wait_list);
	ready_list=NULL;
	return 0;
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值