




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、#include<stdio.h>#include<stdlib.h>#include <conio.h>#include<math.h>#define N 20#define MAX 100typedef struct PCB /pcb進程控制塊定義 int numN; /進程序號 char name10; /進程名 char state; /進程狀態 int tijiaotime; /進程到達時間 int runtime; /進程開始時間 int finishtime; /進程結束時間 int needtime; /服務時間 int pro;
2、/進程優先級 struct PCB *next; /鏈接指針 指向下個作業的pcb;struct PCB *head_input;struct PCB *head_run;struct PCB *head_run_pre;unsigned long current; /記錄系統當前時間的變量int time=10000,n; /計時器 pcb *head=NULL,*p,*q;void getInfo() /創建進程 int num; printf("n請輸入要建立的進程個數:"); scanf("%d",&n); for(num=0;num&l
3、t;n;num+) p=(pcb *)malloc(sizeof(pcb); if(head=NULL) head=p;q=p; printf("依次輸入:n進程號 進程名 到達時間 服務時間 n"); scanf("%st%st%dt%d",&p->num,&p->name,&p->tijiaotime,&p->needtime); if(p->tijiaotime < time) time=p->tijiaotime; q->next=p; p->runtime=0
4、; p->finishtime=0; p->next=NULL; p->state='W' q=p; / *1.先來先服務調度算法*void run_fcfo(pcb *p1)/定義先來先到服務的算法 time = p1->tijiaotime > time? p1->tijiaotime:time; p1->runtime=time; printf("n現在時間是%d,開始運行進程%sn",time,p1->name); time+=p1->needtime; p1->state='F
5、39; p1->finishtime=time; printf("進程名 開始時間 所需時間 結束時間n"); printf("%s %d %d %d ",p1->name,p1->runtime,p1->needtime,p1->finishtime);void fcfo()/定義運行進程函數 int i,j,t; for(j=0;j<n;j+) p=head; t=10000; for(i=0;i<n;i+) /找到當前未完成的進程 if(p->tijiaotime<t && p-
6、>state='W') t=p->tijiaotime; q=p; /標記當前未完成的進程 p=p->next; run_fcfo(q); / *2.優先級調度服務算法*int readydata() /建立就緒隊列 if(head_input->next=NULL) return 0; struct PCB *p1=head_input->next,*pmax,*p2; int maxpro=0xffff; pmax=p1; p2=head_input; while(p1!=NULL) if(p1->pro<maxpro) maxpr
7、o=p1->pro; head_run_pre=p2;pmax=p1; p2=p1; p1=p1->next; head_run=pmax; head_run_pre->next=head_run->next; return 1;void runprocess() /運行進程函數 head_run->runtime-=10; head_run->pro+; struct PCB *p1,*p2; printf("時間片的大小 %d",current); current+=10; printf(" %s 開始n",hea
8、d_run->name); printf("時間片的大小 %d",current); printf(" %s 結束n",head_run->name); if(head_run->runtime<=0) /判斷進程是否運行結束 else p1=head_input; p2=head_input->next; p1->next=head_run; head_run->next=p2; int readyprocess() while(1) if(readydata()=0) return 0; else runpr
9、ocess(); void Init() head_input=new PCB; head_input->next=NULL; current=0; int numpro; printf("請重新輸入要建立的進程個數:"); scanf("%d",&numpro); printf("請依次輸入 進程名 運行時間 優先級n"); for(int i=0;i<numpro;i+) struct PCB *p1=new PCB; scanf("%s",p1->name); scanf("
10、;%d",&p1->runtime); scanf("%d",&p1->pro); p1->state='C' p1->next=NULL; struct PCB *p2=head_input->next; head_input->next=p1; p1->next=p2; / *3.時間片輪轉調度服務算法*void shijianpian()int b,i,X,t,k;int aMAX;/存放進程的剩余時間int cntMAX;/存放進程調度次數printf("請輸入進程數:&q
11、uot;);scanf("%d",&X);printf("n請輸入時間片t大小:");scanf("%d",&t);printf("n請依次輸入各個進程的服務時間");for(i=0;i<X;i+)scanf("%d",&ai);cnti=0;printf("被調度進程t進程調度次數 t本次運行時間結果t剩余時間n");k=1;while(k)for(i=0;i<X;i+)if(ai!=0)if(ai>=t) ai-=t; b+=t;
12、 cnti=cnti+1; printf("nt%dtt%dtt%dtt%d",i+1,cnti,b,ai); else b=b+ai; cnti=cnti+1; ai=0; printf("nt%dtt%dtt%dtt%d",i+1,cnti,b,ai);else continue;for(i=0;i<X;i+)if(ai!=0) k=1;break;else continue;if(i>=X) k=0;void main() printf(" *"); printf("n 1. 按先來先到服務調度的算法模擬n"); printf(" *"); getInfo(); fcfo(); printf("n *"); printf("n 2. 按優先級調
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年廣東省中考生物試卷真題及答案詳解(精校打印版)
- 抗生素不當使用考核試卷
- 2025年中國90#汽油數據監測研究報告
- 2025年中國2,3丁二酮數據監測研究報告
- 2025至2030年中國高筒紳士防滑靴市場分析及競爭策略研究報告
- 2025至2030年中國防火防盜門窗市場分析及競爭策略研究報告
- 2025至2030年中國鋼制平托盤市場分析及競爭策略研究報告
- 2025至2030年中國貴族酒水車市場分析及競爭策略研究報告
- 2025至2030年中國筆式繪圖儀市場分析及競爭策略研究報告
- 2025至2030年中國皮毛勻染劑市場分析及競爭策略研究報告
- 第十一講中華一家和中華民族格局底定(清朝中期)-中華民族共同體概論專家大講堂課件
- 【蘇州智能制造研究院】2025半導體量檢測行業白皮書
- 2024年廣東省中考數學試卷【含解析】
- 隨班就讀培訓
- 停車場管理的難點及解決措施
- 我國智慧養老產業的發展現狀、挑戰與對策
- 高空作業施工方案四篇
- 圖書館小小管理員培訓
- 心外科手術進修匯報
- 設計院項目設計流程與規范
- 設備安裝施工環境保護工作措施
評論
0/150
提交評論