Demo entry 6742749

process.go

   

Submitted by anonymous on May 18, 2018 at 18:29
Language: Go. Code size: 11.4 kB.

package main

//进程管理
type initReadyList struct {
	//总就绪队列
	priority0 *processList
	priority1 *processList
	priority2 *processList
	list      *processList
}

func (p *initReadyList) getPriority(priority int) { //获取进程优先级
	if priority == 0 { //优先级为0
		p.list = p.priority0
	} else if priority == 1 { //优先级为1
		p.list = p.priority1
	} else if priority == 2 { //优先级为2
		p.list = p.priority2
	} else { //优先级错误
		p.list = nil
	}
}

type PCB struct {
	//进程控制块
	pName      string               //名称
	pID        int                  //PID
	pResources *processResources    //资源列表
	pType      string               //进程状态ready、blocked、running
	pList      *processList         //就绪进程队列
	reListNode *processListNode     //逆向列表指针
	parents    *PCB                 //父进程
	reChildren *childrenProcessNode //逆向子进程指针
	pChildren  *childrenProcessNode //子进程链表
	priority   int                  //优先级
}
type processResources struct {
	needR1   int
	needR2   int
	needR3   int
	needR4   int
	occupyR1 int
	occupyR2 int
	occupyR3 int
	occupyR4 int
}
type childrenProcessNode struct {
	//子进程链表节点
	pID       int                  //子进程ID
	child     *PCB                 //子进程
	prevChild *childrenProcessNode //上一个子进程
	nextChild *childrenProcessNode //下一个子进程
}
type processRun struct {
	//当前运行进程
	runProcess *PCB
}

func createInit() { //创建init
	_, ok := mapProcess["init"]
	if ok {
		systemOut <- "The process init has already existed"
		return
	}
	//创建init进程
	var childHead = &childrenProcessNode{-1, nil, nil, nil} //子进程头结点
	var initResources = &processResources{0, 0, 0, 0, 0, 0, 0, 0,}
	systemReadyList.getPriority(0)
	var init = &PCB{"init", pIDNum, initResources, "ready", systemReadyList.list, nil, nil, nil, childHead, 0} //初始化init进程
	mapProcess ["init"] = init
	pIDNum++
	//插入相应优先级就绪队列
	var newProcessRLNode = &processListNode{init.pID, init, nil, nil} //初始化连接节点
	init.pList.rear.next = newProcessRLNode                           //插入
	newProcessRLNode.prev = init.pList.rear                           //指向上一个节点
	init.pList.rear = newProcessRLNode                                //移动尾指针
	init.reListNode = newProcessRLNode                                //添加逆向指针
	//进程调度
	scheduler()
}

func (p *PCB) createProcess(name string, priority int) { //创建进程
	_, ok := mapProcess[name]
	if ok {
		systemOut <- "The process has already existed"
		return
	}
	//创建新进程
	var childHead = &childrenProcessNode{-1, nil, nil, nil}     //子进程头结点
	var pResources = &processResources{0, 0, 0, 0, 0, 0, 0, 0,} //资源列表
	systemReadyList.getPriority(priority)
	var newProcess = &PCB{name, pIDNum, pResources, "ready", systemReadyList.list, nil, p, nil, childHead, priority} //初始化新进程
	pIDNum++                                                                                                         //进程号加1
	//连接父亲节点
	var newProcessParentsChild = &childrenProcessNode{newProcess.pID, newProcess, nil, nil} //初始化连接节点
	var head = newProcess.parents.pChildren                                                 //找到头结点
	if head.nextChild != nil { //插入
		newProcessParentsChild.nextChild = head.nextChild
		newProcessParentsChild.prevChild = head
		head.nextChild = newProcessParentsChild
		newProcessParentsChild.nextChild.prevChild = newProcessParentsChild
	} else {
		newProcessParentsChild.nextChild = head.nextChild
		newProcessParentsChild.prevChild = head
		head.nextChild = newProcessParentsChild
	}
	newProcess.reChildren = newProcessParentsChild //逆向指针

	//插入相应优先级就绪队列
	var newProcessRLNode = &processListNode{newProcess.pID, newProcess, nil, nil} //初始化连接节点
	newProcess.pList.rear.next = newProcessRLNode                                 //插入
	newProcessRLNode.prev = newProcess.pList.rear                                 //指向上一个节点
	newProcess.pList.rear = newProcessRLNode                                      //移动尾指针
	newProcess.reListNode = newProcessRLNode                                      //逆向指针

	//插入进程图
	mapProcess[newProcess.pName] = newProcess
	//进程调度
	scheduler()
}

//销毁进程
func (p *PCB) processRelease() { //释放节点
	p.pList = nil
	p.reListNode = nil
	p.pResources = nil
	p.parents = nil
	p.reChildren = nil
	p.pChildren.child = nil
	p.pChildren.prevChild = nil
	p.pChildren.nextChild = nil
	p.pChildren = nil
}

func destroyProcess(name string) { //销毁函数
	process, ok := mapProcess[name]
	if !ok {
		systemOut <- "The process does not exist"
		return
	}
	delete(mapProcess, name)

	//销毁进程树
	process.destroyProcessTree()
	//资源调度
	systemResource.resourceScheduler()
	//进程调度
	scheduler()
}

func (p *PCB) destroyProcessTree() { //销毁进程树
	for p.pChildren.nextChild != nil {
		p.pChildren.nextChild.child.destroyProcessTree()
	}

	//清除父节点关系
	childNode := p.reChildren //获取父节点链接节点
	if childNode.nextChild != nil {
		childNode.nextChild.prevChild = childNode.prevChild
		childNode.prevChild.nextChild = childNode.nextChild
	} else {
		childNode.prevChild.nextChild = childNode.nextChild
	}
	childNode.nextChild = nil //避免内存泄漏
	childNode.prevChild = nil //避免内存泄漏
	childNode.child = nil     //避免内存泄漏
	childNode = nil           //避免内存泄漏

	//清除进程队列
	if p.pType == "running" {
		run.runProcess = nil
	} else {
		node := p.reListNode
		if p.pList.rear == node { //若为最后一个节点
			p.pList.rear = p.pList.head //尾指针指向头结点
			p.pList.head.next = nil     //头结点下一个赋空
		} else {
			node.next.prev = node.prev //删除节点
			node.prev.next = node.next //删除节点
		}
		node.process = nil //避免内存泄漏
		node.next = nil    //避免内存泄漏
		node.prev = nil    //避免内存泄漏
		p.reListNode = nil //删除逆向指针
	}

	//释放资源
	systemResource.R1.rStatus += p.pResources.occupyR1
	systemResource.R2.rStatus += p.pResources.occupyR2
	systemResource.R3.rStatus += p.pResources.occupyR3
	systemResource.R4.rStatus += p.pResources.occupyR4

	//释放节点
	p.processRelease()
}

//进程调度
func scheduler() {
	var highestPriority *PCB //队列中最高优先级进程
	var first *processListNode
	highestPriority = nil

	//获取队列中最高优先级进程
	if systemReadyList.priority2.head.next != nil {
		systemReadyList.getPriority(2)         //2优先级就绪队列
		first = systemReadyList.list.head.next //获取最高优先级节点
		highestPriority = first.process
	} else if highestPriority == nil && systemReadyList.priority1.head.next != nil {
		systemReadyList.getPriority(1)         //1优先级就绪队列
		first = systemReadyList.list.head.next //保护节点
		highestPriority = first.process
	} else if highestPriority == nil && systemReadyList.priority0.head.next != nil {
		systemReadyList.getPriority(0)         //0优先级就绪队列
		first = systemReadyList.list.head.next //保护节点
		highestPriority = first.process
	}

	//更改running进程
	if run.runProcess != nil && highestPriority != nil && run.runProcess.priority < highestPriority.priority {
		//将进程插入就绪队列
		process := run.runProcess                                               //保护
		run.runProcess = nil                                                    //赋空
		process.pType = "ready"                                                 //更改状态
		var newProcessRLNode = &processListNode{process.pID, process, nil, nil} //初始化连接节点
		systemReadyList.getPriority(process.priority)                           //初始化队列优先级
		process.pList = systemReadyList.list                                    //指向就绪队列
		process.pList.rear.next = newProcessRLNode                              //插入
		newProcessRLNode.prev = process.pList.rear                              //指向上一个节点
		process.pList.rear = newProcessRLNode                                   //移动尾指针
		process.reListNode = newProcessRLNode                                   //添加逆向指针

		//删除队列中node
		systemReadyList.getPriority(highestPriority.priority)
		if systemReadyList.list.rear == first { //若为最后一个节点
			systemReadyList.list.rear = systemReadyList.list.head //尾指针指向头结点
			systemReadyList.list.head.next = nil                  //头结点下一个赋空
		} else {
			first.next.prev = first.prev //删除节点
			first.prev.next = first.next //删除节点
		}
		first.process = nil               //避免内存泄漏
		first.next = nil                  //避免内存泄漏
		first.prev = nil                  //避免内存泄漏
		highestPriority.pType = "running" //更改状态
		highestPriority.pList = nil       //队列指针赋空
		highestPriority.reListNode = nil  //删除逆向指针
		run.runProcess = highestPriority  //运行
	} else if run.runProcess == nil {
		//删除队列中node
		if systemReadyList.list.rear == first { //若为最后一个节点
			systemReadyList.list.rear = systemReadyList.list.head //尾指针指向头结点
			systemReadyList.list.head.next = nil                  //头结点下一个赋空
		} else {
			first.next.prev = first.prev //删除节点
			first.prev.next = first.next //删除节点
		}
		first.process = nil               //避免内存泄漏
		first.next = nil                  //避免内存泄漏
		first.prev = nil                  //避免内存泄漏
		highestPriority.pType = "running" //更改状态
		highestPriority.pList = nil       //队列指针赋空
		highestPriority.reListNode = nil  //删除逆向指针
		run.runProcess = highestPriority
	}
	systemOut <- "process " + run.runProcess.pName + " is running"
}

//超时
func timeOut() {
	//将running进程插入就绪队列
	process := run.runProcess                                               //保护
	run.runProcess = nil                                                    //赋空
	process.pType = "ready"                                                 //更改状态
	var newProcessRLNode = &processListNode{process.pID, process, nil, nil} //初始化连接节点
	systemReadyList.getPriority(process.priority)                           //初始化队列优先级
	process.pList = systemReadyList.list                                    //指向就绪队列
	process.pList.rear.next = newProcessRLNode                              //插入
	newProcessRLNode.prev = process.pList.rear                              //指向上一个节点
	process.pList.rear = newProcessRLNode                                   //移动尾指针
	process.reListNode = newProcessRLNode                                   //添加逆向指针

	var highestPriority *PCB //队列中最高优先级进程
	var first *processListNode
	highestPriority = nil

	//获取队列中最高优先级进程
	if systemReadyList.priority2.head.next != nil {
		systemReadyList.getPriority(2)         //2优先级就绪队列
		first = systemReadyList.list.head.next //获取最高优先级节点
		highestPriority = first.process
	} else if highestPriority == nil && systemReadyList.priority1.head.next != nil {
		systemReadyList.getPriority(1)         //1优先级就绪队列
		first = systemReadyList.list.head.next //保护节点
		highestPriority = first.process
	} else if highestPriority == nil && systemReadyList.priority0.head.next != nil {
		systemReadyList.getPriority(0)         //0优先级就绪队列
		first = systemReadyList.list.head.next //保护节点
		highestPriority = first.process
	}

	//删除队列中node
	systemReadyList.getPriority(highestPriority.priority)
	if systemReadyList.list.rear == first { //若为最后一个节点
		systemReadyList.list.rear = systemReadyList.list.head //尾指针指向头结点
		systemReadyList.list.head.next = nil                  //头结点下一个赋空
	} else {
		first.next.prev = first.prev //删除节点
		first.prev.next = first.next //删除节点
	}
	first.process = nil               //避免内存泄漏
	first.next = nil                  //避免内存泄漏
	first.prev = nil                  //避免内存泄漏
	highestPriority.pType = "running" //更改状态
	highestPriority.pList = nil       //队列指针赋空
	highestPriority.reListNode = nil  //删除逆向指针
	run.runProcess = highestPriority  //运行

	systemOut <- "process " + run.runProcess.pName + " is running"
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).