用链表写的一个简单的进程 还有地方不知怎么实现 求指点啊
- C/C++ code
#include <stdio.h>#include <stdlib.h> struct PCB{ char name[10]; int rtime; int priority; char state; struct PCB *next;};/**初始化PCB*/struct PCB *initPCB(){ struct PCB *head; head=(struct PCB *)malloc(sizeof(struct PCB)); if(NULL==head) { exit(-1); } head->next=NULL; printf("please input the information of process\n"); printf("name:"); scanf("%s",head->name); printf("run time:"); scanf("%d",&head->rtime); printf("priority:"); scanf("%d",&head->priority); head->state='R'; printf("\n"); return head;}/**找到优先级最高的进程*/void MaxPriorityProcess(struct PCB * head,struct PCB* pCurrent){ struct PCB* temp; struct PCB* p; temp=head; if(NULL==temp)//队列为空 { temp=pCurrent; } if(pCurrent->priority>temp->priority)//如果优先级最大 { pCurrent->next=temp; temp=pCurrent; } if(NULL==temp->next)//队列里只有一个元素 { if(temp->priority>pCurrent->priority) { temp->next=pCurrent; } } /*队列元素排列*/ p=head; while(p!=NULL) { if(temp->priority>pCurrent->priority) { p=temp; temp=temp->next; } else { break; } } pCurrent->next=p->next; p->next=pCurrent;}/**打印PCB*/void printPCB(struct PCB * head){ struct PCB* temp; temp=head->next; while(temp!=NULL) { printf("\n process name: %s\n priority num: %d\n run time: %d\n process state:%c\n", temp->name,temp->priority,temp->rtime,temp->state); temp=temp->next; } printf("\n");}int main(){ struct PCB* head; struct PCB* temp; //head=new PCB; PCB AA; head = &AA; for(int i=0;i<3;i++) { temp=initPCB(); temp->next = NULL; MaxPriorityProcess(head,temp); } printPCB(head); return 0;}
优先数-1
要求运行时间-1
来模拟进程的一次运行。
提醒注意的是:在实际的系统中,当一个进程被选中运行时,必须恢复进程的现场,让它占有处理器运行,直到出现等待事件或运行结束。在这里省去了这些工作。
(5) 进程运行一次后,若要求运行时间?0,则再将它加入队列(按优先数大小插入,且置队首标志);若要求运行时间=0,则把它的状态修改成“结束”(E),且退出队列。
不知怎么实现
[解决办法]
- C/C++ code
/**模拟简单的进程调度*/#include <stdio.h>#include <stdlib.h>typedef struct PCB{ char name[10]; int rtime; int priorityNum; char state; struct PCB *next;}PCB;/**初始化PCB*/PCB *initPCB(){ PCB *head; head=(PCB *)malloc(sizeof(PCB)); if(NULL==head) { exit(-1); } head->next=NULL; return head;}/** 新建一个节点,并返回*/PCB *inputValue(){ PCB *temp = NULL; temp = initPCB(); printf("please input the information of process\n"); printf("name:"); scanf("%s",temp->name); printf("run time:"); scanf("%d",&temp->rtime); printf("priorityNum:"); scanf("%d",&temp->priorityNum); temp->state = 'R'; printf("\n"); return temp;}/**找到优先级最高的进程*/void findProcess( PCB * head, PCB * pCurrent ){ if( head->next == NULL ) { head->next = pCurrent; return ; } /* 从大到小排 */ PCB* p = head->next; PCB* q = head; while( p != NULL ) { if( (pCurrent->priorityNum) > (p->priorityNum) ) { q->next = pCurrent; pCurrent->next = p; break; } q = p; p = p->next; } q->next = pCurrent;}/**打印PCB*/void printPCB( PCB * head ){ PCB *temp; temp = head->next; while( temp!=NULL ) { printf("\n process name: %s run time: %d priority num: %d process state:%c", temp->name,temp->rtime,temp->priorityNum,temp->state); temp = temp->next; } printf("\n");}/**运行进程*/void runProcess( PCB * head){ PCB *processblock = NULL; PCB *pcb = head->next; while(pcb != NULL) { pcb->rtime -= 1; pcb->priorityNum -= 1; if(pcb->rtime == 0) { pcb->state = 'E'; pcb = pcb->next; if(pcb == NULL) return ; } else { processblock = pcb; pcb = pcb->next; processblock->next = NULL; findProcess(pcb,processblock); system("cls"); } head->next = pcb; printPCB(head); getchar(); } return ;}int main(){ PCB *head = NULL; PCB *temp = NULL; head = initPCB(); // 头结点为空 for(int i=0;i<5;i++) { temp = inputValue(); findProcess(head,temp);//其实是不符合要求的 它的复杂度为O(n) 只要和队首在比较一次 就可以了 printf("=================\n"); } runProcess(head); return 0;}