【用C语言编程模拟处理机调度(实现一种算法)一、实验内容选择一个调度算法,实现处理器调度。二、实验目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪】
<p>问题:【用C语言编程模拟处理机调度(实现一种算法)一、实验内容选择一个调度算法,实现处理器调度。二、实验目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪】<p>答案:↓↓↓<p class="nav-title mt10" style="border-top:1px solid #ccc;padding-top: 10px;">全欣娜的回答:<div class="content-b">网友采纳 #include #include #definegetpch(type)(type*)malloc(sizeof(type)) #defineNULL0 structpcb{/*定义进程控制块PCB*/ charname; charstate; intsuper; intntime; intrtime; structpcb*link; }*ready=NULL,*p; typedefstructpcbPCB; voidsort()/*建立对进程进行优先级排列函数*/ { PCB*first,*second; intinsert=0; if((ready==NULL)||((p->super)>(ready->super)))/*优先级最大者,插入队首*/ { p->link=ready; ready=p; } else/*进程比较优先级,插入适当的位置中*/ { first=ready; second=first->link; while(second!=NULL) { if((p->super)>(second->super))/*若插入进程比当前进程优先数大,*/ {/*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else/*插入进程优先数最低,则插入到队尾*/ { first=first->link; second=second->link; } } if(insert==0)first->link=p; } } voidinput()/*建立进程控制块函数*/ { inti,num; system("cls");/*清屏*/ printf("n请输入进程数:"); scanf("%d",&num); for(i=1;iname); printf("n输入进程优先数:"); scanf("%d",&p->super); printf("n输入进程运行时间:"); scanf("%d",&p->ntime); printf("n"); p->rtime=0;p->state='W'; p->link=NULL; sort();/*调用sort函数*/ } } intspace() { intl=0; PCB*pr=ready; while(pr!=NULL) { l++; pr=pr->link; } return(l); } voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/ { printf("n进程名t状态t优先数t需要运行时间t已经运行时间n"); printf("|%st",pr->name); printf("|%ct",pr->state); printf("|%dt",pr->super); printf("|%dtt",pr->ntime); printf("|%dt",pr->rtime); printf("n"); } voidcheck()/*建立进程查看函数*/ { PCB*pr; printf("n****当前正在运行的进程是:n");/*显示当前运行进程*/ disp(p); pr=ready; printf("n****当前就绪队列状态为:n");/*显示就绪队列状态*/ while(pr!=NULL) { disp(pr); pr=pr->link; } } voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/ { printf("n进程[%s]已完成.n",p->name); free(p); } voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/ { (p->rtime)++; if(p->rtime==p->ntime) destroy();/*调用destroy函数*/ else { (p->super)--; p->state='W'; sort();/*调用sort函数*/ } } voidmain()/*主函数*/ { intlen,h=0; charch; input(); len=space(); while((len!=0)&&(ready!=NULL)) { ch=getchar(); h++; printf("-----------------------------------------------------"); printf("n现在是第%d次运行:n",h);
页:
[1]