⑴ 用c語言編寫一段簡單的程序,作業調度和低級調度演算法
真不容易啊,怕是沒人弄了!
優先順序調度演算法程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
   char name[10];  /*進程標識符*/
   int prio;   /*進程優先數*/
   int round;  /*進程時間輪轉時間片*/
   int cputime; /*進程佔用CPU時間*/
   int needtime; /*進程到完成還要的時間*/
   int count;  /*計數器*/
   char state; /*進程的狀態*/
   struct node *next; /*鏈指針*/
}PCB;
PCB *finish,*ready,*tail,*run; /*隊列指針*/
int N; /*進程數*/
/*將就緒隊列中的第一個進程投入運行*/
firstin()
{
   run=ready;   /*就緒隊列頭指針賦值給運行頭指針*/
   run->state='R';   /*進程狀態變為運行態*/
   ready=ready->next;  /*就緒對列頭指針後移到下一進程*/
}
/*標題輸出函數*/
void prt1(char a)
{
   if(toupper(a)=='P') /*優先數法*/
      printf("  name     cputime  needtime  priority  state\n");
   else
      printf("  name     cputime  needtime   count   round     state\n");
}
/*進程PCB輸出*/
void prt2(char a,PCB *q)
{
   if(toupper(a)=='P')  /*優先數法的輸出*/
      printf("  %-10s%-10d%-10d%-10d %c\n",q->name,
       q->cputime,q->needtime,q->prio,q->state);
   else/*輪轉法的輸出*/
      printf("  %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
       q->cputime,q->needtime,q->count,q->round,q->state);
}
/*輸出函數*/
void prt(char algo)
{
   PCB *p;
   prt1(algo);  /*輸出標題*/
   if(run!=NULL) /*如果運行指針不空*/
      prt2(algo,run); /*輸出當前正在運行的PCB*/
   p=ready;  /*輸出就緒隊列PCB*/
   while(p!=NULL)
   {
      prt2(algo,p);
      p=p->next;
   }
   p=finish;  /*輸出完成隊列的PCB*/
   while(p!=NULL)
   {
      prt2(algo,p);
      p=p->next;
   }
   getch();  /*壓任意鍵繼續*/
}
/*優先數的插入演算法*/
insert1(PCB *q)
{
   PCB *p1,*s,*r;
   int b;
   s=q;  /*待插入的PCB指針*/
   p1=ready; /*就緒隊列頭指針*/
   r=p1; /*r做p1的前驅指針*/
   b=1;
   while((p1!=NULL)&&b)  /*根據優先數確定插入位置*/
      if(p1->prio>=s->prio)
      {
     r=p1;
     p1=p1->next;
      }
      else
     b=0;
   if(r!=p1)  /*如果條件成立說明插入在r與p1之間*/
   {
      r->next=s;
      s->next=p1;
   }
   else
   {
      s->next=p1;  /*否則插入在就緒隊列的頭*/
      ready=s;
   }
}
/*輪轉法插入函數*/
insert2(PCB *p2)
{
   tail->next=p2;  /*將新的PCB插入在當前就緒隊列的尾*/
   tail=p2;
   p2->next=NULL;
}
/*優先數創建初始PCB信息*/
void create1(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL; /*就緒隊列頭指針*/
   finish=NULL;  /*完成隊列頭指針*/
   run=NULL; /*運行隊列指針*/
   printf("Enter name and time of process\n"); /*輸入進程標識和所需時間創建PCB*/
   for(i=1;i<=N;i++)
   {
      p=malloc(sizeof(PCB));
      scanf("%s",na);
      scanf("%d",&time);
      strcpy(p->name,na);
      p->cputime=0;
      p->needtime=time;
      p->state='w';
      p->prio=50-time;
      if(ready!=NULL) /*就緒隊列不空調用插入函數插入*/
     insert1(p);
      else
      {
     p->next=ready; /*創建就緒隊列的第一個PCB*/
     ready=p;
      }
   }
   clrscr();
   printf("          output of priority:\n");
   printf("************************************************\n");
   prt(alg);  /*輸出進程PCB信息*/
   run=ready; /*將就緒隊列的第一個進程投入運行*/
   ready=ready->next;
   run->state='R';
}
/*輪轉法創建進程PCB*/
void create2(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL;
   finish=NULL;
   run=NULL;
   printf("Enter name and time of round process\n");
   for(i=1;i<=N;i++)
   {
      p=malloc(sizeof(PCB));
      scanf("%s",na);
      scanf("%d",&time);
      strcpy(p->name,na);
      p->cputime=0;
      p->needtime=time;
      p->count=0; /*計數器*/
      p->state='w';
      p->round=2;  /*時間片*/
      if(ready!=NULL)
     insert2(p);
      else
      {
     p->next=ready;
     ready=p;
     tail=p;
      }
   }
   clrscr();
   printf("              output of round\n");
   printf("************************************************\n");
   prt(alg);   /*輸出進程PCB信息*/
   run=ready;  /*將就緒隊列的第一個進程投入運行*/
   ready=ready->next;
   run->state='R';
}
/*優先數調度演算法*/
priority(char alg)
{
   while(run!=NULL)  /*當運行隊列不空時,有進程正在運行*/
   {
      run->cputime=run->cputime+1;
      run->needtime=run->needtime-1;
      run->prio=run->prio-3; /*每運行一次優先數降低3個單位*/
      if(run->needtime==0)  /*如所需時間為0將其插入完成隊列*/
      {
     run->next=finish;
     finish=run;
     run->state='F';  /*置狀態為完成態*/
     run=NULL;  /*運行隊列頭指針為空*/
     if(ready!=NULL) /*如就緒隊列不空*/
        firstin(); /*將就緒對列的第一個進程投入運行*/
      }
      else /*沒有運行完同時優先數不是最大,則將其變為就緒態插入到就緒隊列*/
     if((ready!=NULL)&&(run->prio<ready->prio))
     {
        run->state='W';
        insert1(run);
        firstin(); /*將就緒隊列的第一個進程投入運行*/
     }
      prt(alg); /*輸出進程PCB信息*/
   }
}
/*時間片輪轉法*/
roundrun(char alg)
{
   while(run!=NULL)
   {
      run->cputime=run->cputime+1;
      run->needtime=run->needtime-1;
      run->count=run->count+1;
      if(run->needtime==0)/*運行完將其變為完成態,插入完成隊列*/
      {
     run->next=finish;
     finish=run;
     run->state='F';
     run=NULL;
     if(ready!=NULL)
        firstin(); /*就緒對列不空,將第一個進程投入運行*/
      }
      else
     if(run->count==run->round)  /*如果時間片到*/
     {
        run->count=0;  /*計數器置0*/
        if(ready!=NULL) /*如就緒隊列不空*/
        {
           run->state='W'; /*將進程插入到就緒隊列中等待輪轉*/
           insert2(run);
           firstin(); /*將就緒對列的第一個進程投入運行*/
        }
     }
      prt(alg); /*輸出進程信息*/
   }
}
/*主函數*/
main()
{
   char algo;  /*演算法標記*/
   clrscr();
   printf("type the algorithm:P/R(priority/roundrobin)\n");
   scanf("%c",&algo); /*輸入字元確定演算法*/
   printf("Enter process number\n");
   scanf("%d",&N); /*輸入進程數*/
   if(algo=='P'||algo=='p')
   {
      create1(algo); /*優先數法*/
      priority(algo);
   }
   else
      if(algo=='R'||algo=='r')
      {
     create2(algo); /*輪轉法*/
     roundrun(algo);
      }
}
⑵ 用c語言或者Java設計出一個任務調度器。。。
公眾:類PrivilegeProcess {
公共靜態無效的主要(字串[] args){
MyQueue的MyQueue的新MyQueue的();/ /聲明隊列
印刷電路板[PCB = {新的PCB(001 ,8,1),新的PCB(002,7,9),新的PCB(003,3,8),新的PCB(004,1,7),新的PCB(005,7,4)};
> PCB段=新的PCB(); 
(INT I = 0; <pcb.length; + +){/ /初始化先進行排序,選擇排序這里使用的是高優先順序的一線隊
(J =我; <pcb.length; J + +){
(PCB [I]。特權<PCB [J]。特權){
段= PCB [1]; 
PCB [I] = PCB [J]; 
PCB [J] =段; 
} 
} 
} 
體系。通過out.println(「入隊後第一時間的進程的順序:」); 
(INT I = 0; <pcb.length; + +){
的System.out調用println(第一次入隊#程序名稱:「+ PCB [我]。名稱+ totaltime:」+ PCB [I]。totaltime +「的」特權「+ PCB [我]。特權); } 
(); 
myqueue.start(PCB); 
} 
} 
類MyQueue的{
INT指數= 0; 
PCB [] PC =新的PCB [5]; 
PCB [] PC1 =新的PCB [4]; 
PCB溫度=新的PCB() BR />公共無效排隊(PCB工藝){/ /排隊演算法
(指數== 5){
(「出界!」); 
返回
} 
PC [索引] =進程; 
指數+ +; 
} 
公共:PCB DEQUEUE(){/ /出隊演算法(索引== 0)
返回空; 
(INT I = 0; <pc1.length; + +){
PC1 [I] = PC [ +1]; 
} 
指數 - 
溫度= PC [0]; 
(INT I = 0; <pc1.length; + +){ BR /> PC [I] = PC1 [I]; 
} 
回報條件; 
} 
公共無效啟動(PCB [] PC){/ /進程表演算法
(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。時isNotFinish ==真| | PC [4]。isNotFinish ==){
/ / *注:| |運算符都是假的,所有的表達式結果為假,否則真
(INT I = 0; <PC長度; + +){
PC [I]。運行(這一點); />} 的System.out.println(); 
(INT I = 0; <pc.length; + +){/ /處理每個運行一次運行的時間片的長度重新排序優先一旦
(J =我; <pc.length; J + +){
如果(PC [I]特權<PC [J]。特權){
溫度= PC [I]; 
PC [I] = PC [J]; 
PC [J] =溫度; 
} 
} 
} 
} 
} 
} 
類PCB {/ /聲明過程級
和int名,totaltime ,運行時特權; 
布爾isNotFinish的; 
公眾PCB(){
} 
公開PCB(名稱,詮釋totaltime特權){
this.name =的名稱;/ /進程名
this.totaltime = totaltime ;/ / 
this.privilege =特權;/ /總時間優先 this.runtime = 2 ;/ /時間片值是2 
this.isNotFinish =真;/ /是否執行完成
(「初始值:程序名稱:」+名+「totaltime:」+ totaltime +「特權」+特權); 
System.out的。調用println(); 
} 
MyQueue的MQ公共無效的run(){/ /處理的基礎上實施的時間片演算法
(totalTime> 1){ totaltime =運行;/ /總時間大於1,總時間=總時間 - 時間片
特權 - 
(「程序名稱:」+姓名+「 remaintime:「+ +」特權「+特權); totaltime 
的} else if(totaltime == 1){
totaltime - ;/ /總時間為1時,執行時間為1
>特權 - 
(「程序名稱:」+姓名+「remaintime:」+ totaltime +「特權」+特權); 
}其他{
isNotFinish =假;/ / 0,將isNotFinish標志設置為假
} 
如果(isNotFinish ==真){br mq.deQueue();  mq.enQueue(本); } 
} 
}
⑶ 用C語言編寫並調試一個模擬的進程調度程序,採用「簡單時間片輪轉法」調度演算法對五個進程進行調度。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
struct PCB {
 char NAME[10]; /*進程名*/
 int ROUND; /*進程輪轉時間片*/
 int REACHTIME; /*進程到達時間*/
 int CPUTIME; /*進程佔用CPU時間*/
 int COUNT; /*計數器*/
 int NEEDTIME; /*進程完成還要的CPU時間*/
 char STATE; /*進程的狀態*/
 struct PCB *NEXT; /*鏈指針*/
};
 
struct LINK { /*PCB的鏈結構*/
 struct PCB *RUN; /*當前運行進程指針*/
 struct PCB *READY; /*就緒隊列頭指針*/
 struct PCB *TAIL; /*就緒隊列尾指針*/
 struct PCB *FINISH; /*完成隊列頭指針*/
};
 
void INIT(LINK *); /*對PCB的鏈結構初始化*/
void INSERT(LINK *); /*將執行了一個單位時間片數且還未完成的進程的PCB插到就緒隊列的隊尾*/
void FIRSTIN(LINK *); /*將就緒隊列中的第一個進程投入運行*/
void PRINT(LINK *); /*列印每執行一個時間片後的所有進程的狀態*/
void PR(PCB *); /*列印一個進程的狀態*/
int CREATE(LINK *,int); /*創建新的進程*/
void ROUNDSCH(LINK *); /*按時間片輪轉法調度進程*/
 
void main() {
 LINK pcbs;
 int i;
 INIT(&pcbs);
 i=0;
 printf("創建5個進程\n\n");
 while(i<5) {
  if(CREATE(&pcbs,i+1)==1) {
   printf("進程已創建\n\n");
   i++;
  }
  else
   printf("進程創建失敗\n\n");
 }
 FIRSTIN(&pcbs);
 ROUNDSCH(&pcbs);
}
 
void ROUNDSCH(LINK *p) {
 PCB *pcb;
 while(p->RUN!=NULL) {
  pcb=(PCB *)malloc(sizeof(PCB));
  strcpy(pcb->NAME,p->RUN->NAME);
  pcb->ROUND=p->RUN->ROUND;
  pcb->REACHTIME=p->RUN->REACHTIME;
  pcb->CPUTIME=p->RUN->CPUTIME;
  pcb->COUNT=p->RUN->COUNT;
  pcb->NEEDTIME=p->RUN->NEEDTIME;
  pcb->STATE=p->RUN->STATE;
  pcb->NEXT=p->RUN->NEXT;
  pcb->CPUTIME++;
  pcb->NEEDTIME--;
  pcb->COUNT++;
  if(pcb->NEEDTIME==0) {
   pcb->NEXT=p->FINISH->NEXT;
   p->FINISH->NEXT=pcb;
   pcb->STATE='F';
   p->RUN=NULL;
   if(p->READY!=p->TAIL)
    FIRSTIN(p);
  }
  else {
   p->RUN=pcb;
   if(pcb->COUNT==pcb->ROUND) {
    pcb->COUNT=0;
    if(p->READY!=p->TAIL) {
     pcb->STATE='W';
     INSERT(p);
     FIRSTIN(p);
    }
   }
  }
  PRINT(p);
 }
}
 
void INIT(LINK *p) {
 p->RUN=NULL;
 p->TAIL=p->READY=(PCB *)malloc(sizeof(PCB));
 p->READY->NEXT=NULL;
 p->FINISH=(PCB *)malloc(sizeof(PCB));
 p->FINISH->NEXT=NULL;
}
 
int CREATE(LINK *p,int n) {
 PCB *pcb,*q;
 pcb=(PCB *)malloc(sizeof(PCB));
 flushall();
 printf("請輸入第%d個進程的名稱:\n",n);
 gets(pcb->NAME);
 printf("請輸入第%d個進程的輪轉時間片數:\n",n);
 scanf("%d",&(pcb->ROUND));
 printf("請輸入第%d個進程的到達時間:\n",n);
 scanf("%d",&(pcb->REACHTIME));
 pcb->CPUTIME=0;
 pcb->COUNT=0;
 printf("請輸入第%d個進程需運行的時間片數:\n",n);
 scanf("%d",&(pcb->NEEDTIME));
 pcb->STATE='W';
 pcb->NEXT=NULL;
 if(strcmp(pcb->NAME,"")==0||pcb->ROUND<=0||pcb->NEEDTIME<=0) /*輸入錯誤*/
  return 0;
 q=p->READY;
 while(q->NEXT!=NULL&&q->NEXT->REACHTIME<=pcb->REACHTIME)
  q=q->NEXT;
 pcb->NEXT=q->NEXT;
 q->NEXT=pcb;
 if(pcb->NEXT==NULL)
  p->TAIL=pcb;
 return 1;
}
 
void FIRSTIN(LINK *p) {
 PCB *q;
 q=p->READY->NEXT;
 p->READY->NEXT=q->NEXT;
 q->NEXT=NULL;
 if(p->READY->NEXT==NULL)
  p->TAIL=p->READY;
 q->STATE='R';
 p->RUN=q;
}
 
void INSERT(LINK *p) {
 PCB *pcb;
 pcb=(PCB *)malloc(sizeof(PCB));
 strcpy(pcb->NAME,p->RUN->NAME);
 pcb->ROUND=p->RUN->ROUND;
 pcb->REACHTIME=p->RUN->REACHTIME;
 pcb->CPUTIME=p->RUN->CPUTIME;
 pcb->COUNT=p->RUN->COUNT;
 pcb->NEEDTIME=p->RUN->NEEDTIME;
 pcb->STATE=p->RUN->STATE;
 pcb->NEXT=p->RUN->NEXT;
 p->TAIL->NEXT=pcb;
 p->TAIL=pcb;
 p->RUN=NULL;
 pcb->STATE='W';
}
 
void PRINT(LINK *p) {
 PCB *pcb;
 printf("執行一個時間片後的所有進程的狀態:\n\n");
 if(p->RUN!=NULL)
  PR(p->RUN);
 if(p->READY!=p->TAIL) {
  pcb=p->READY->NEXT;
  while(pcb!=NULL) {
   PR(pcb);
   pcb=pcb->NEXT;
  }
 }
 pcb=p->FINISH->NEXT;
 while(pcb!=NULL) {
  PR(pcb);
  pcb=pcb->NEXT;
 }
}
 
void PR(PCB *p) {
 printf("進程名:%s\n",p->NAME);
 printf("進程輪轉時間片:%d\n",p->ROUND);
 printf("進程到達時間:%d\n",p->REACHTIME);
 printf("進程佔用CPU時間:%d\n",p->CPUTIME);
 printf("計數器:%d\n",p->COUNT);
 printf("進程完成還要的CPU時間:%d\n",p->NEEDTIME);
 printf("進程的狀態:%c\n\n",p->STATE);
}
⑷ 用C語言編程模擬處理機調度(實現一種演算法)
#include <stdlib.h> 
#include <conio.h> 
#define getpch(type) (type*)malloc(sizeof(type)) 
#define NULL 0 
struct pcb { /* 定義進程式控制制塊PCB */ 
 char name[10]; 
 char state; 
 int super; 
 int ntime; 
 int rtime; 
 struct pcb* link; 
}*ready=NULL,*p; 
typedef struct pcb PCB; 
void sort() /* 建立對進程進行優先順序排列函數*/ 
{ 
 PCB *first, *second; 
 int insert=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; 
 } 
} 
void input() /* 建立進程式控制制塊函數*/ 
{ 
 int i,num; 
 system("cls"); /*清屏*/ 
 printf("\n 請輸入進程數: "); 
 scanf("%d",&num); 
 for(i=1;i<=num;i++) 
 { 
  printf("\n 進程號No.%d:\n",i);
  p=getpch(PCB); 
  printf("\n 輸入進程名:"); 
  scanf("%s",p->name); 
  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函數*/ 
 } 
} 
int space() 
{ 
 int l=0; 
 PCB* pr=ready; 
 while(pr!=NULL) 
 { 
  l++; 
  pr=pr->link; 
 } 
 return(l); 
} 
void disp(PCB * pr) /*建立進程顯示函數,用於顯示當前進程*/ 
{ 
 printf("\n 進程名\t 狀態\t 優先數\t 需要運行時間\t 已經運行時間\n"); 
 printf("|%s\t",pr->name); 
 printf("|%c\t",pr->state); 
 printf("|%d\t",pr->super); 
 printf("|%d\t\t",pr->ntime); 
 printf("|%d\t",pr->rtime); 
 printf("\n"); 
} 
void check() /* 建立進程查看函數 */ 
{ 
 PCB* pr; 
 printf("\n **** 當前正在運行的進程是:\n"); /*顯示當前運行進程*/ 
 disp(p); 
 pr=ready; 
 printf("\n **** 當前就緒隊列狀態為:\n"); /*顯示就緒隊列狀態*/ 
 while(pr!=NULL) 
 { 
  disp(pr); 
  pr=pr->link; 
 } 
} 
void destroy() /*建立進程撤消函數(進程運行結束,撤消進程)*/ 
{ 
 printf("\n 進程 [%s] 已完成.\n",p->name); 
 free(p); 
} 
void running() /* 建立進程就緒函數(進程運行時間到,置就緒狀態*/ 
{ 
 (p->rtime)++; 
 if(p->rtime==p->ntime) 
  destroy(); /* 調用destroy函數*/ 
 else 
 { 
  (p->super)--;
  p->state='W'; 
  sort(); /*調用sort函數*/ 
 } 
} 
void main() /*主函數*/
{ 
 int len,h=0; 
 char ch;
 input(); 
 len=space(); 
 while((len!=0)&&(ready!=NULL)) 
 { 
  ch=getchar();
  h++; 
  printf("-----------------------------------------------------");
  printf("\n 現在是第%d次運行: \n",h); 
  p=ready; 
  ready=p->link; 
  p->link=NULL; 
  p->state='R'; 
  check(); 
  running();
  printf("\n 按任意鍵繼續......\n"); 
 } 
 printf("\n\n 進程已經完成.\n"); 
}
