具體內容
采用的調度算法:高優先數調度算法和先來先服務算法
進程由進程控制塊PCB表示,PCB中包括a)進程名b)優先數c)進程到達時間d)進程結束時間e)進程狀態f)已占用CPU時間(進程運行一個時間片后加一)
運行一個時間片后進程優先級降一級
利用文件操作模擬進程的操作
概念圖
實現過程
導入的庫和宏定義
?
#include?#include? #include? #include? ??????????????????????????????????? #include? #include? #define?u8???char #define?u32??unsigned?int #define?MAXLINE?1024?//工作隊列和等待隊列長度 #define?MAXNUM??100 #define?WAIT?0?//進程狀態為WAIT #define?RUN??1?//進程狀態為RUN #define?FINISH?2?//進程狀態為FINISH #define?NEWBUF??1?//此時工作隊列為新隊列 #define?BUF??0?//此時工作隊列為默認隊列 #define?THENULL?0?//表示進程塊此時為NULL #define?THETRUE?1?//表示進程塊此時有進程 char*?VALUE?=?"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz?";
?
建立進程塊PCB結構體
?
struct?PCB{ ?FILE*???open_file; ?u8?*??file_name; ?clock_t??arrive_time;?//到達時間? ?clock_t??level_time;?//結束時間 ?int??prior_number;?//優先級 ?u8????program_status;?//程序狀態 ????u8??????detection_null_or_value;?//判斷此時進程塊是否為空???? ????u8??????occupy_time_slice_times;?//已占用的CPU時間??? ????int?????data_write_number;???????//記錄已經向文件中寫入內容的長度?? }; #define?PCB_T?struct?PCB*
?
建立工作隊列結構體
?
struct?program_queue{ ?struct?PCB?pcb_buf[MAXLINE];?//默認隊列,一開始將進程塊存入這里 ?struct?PCB?pcb_new_buf[MAXLINE];//與默認隊列交替管理進程塊 ?u32?queue_line;?????//工作隊列的長度 ?u8??buf_state;??????????????????//判斷此時正在使用的是哪一個隊列 }; #define?program_queue_t?struct?program_queue*
?
建立CPU時間片結構體
?
struct?cpu_time_slice{ ?clock_t?start_time;??//時間片開始執行時間 ?clock_t?end_time;??//時間片結束執行時間 ?u32????time_slice_bulk;?//每個時間片的時間,單位ms }; #define?cpu_time_slice_t?struct?cpu_time_slice*
?
清空new隊列方法
?
void?bzero_queue_new(program_queue_t?queue_t) { ????for(int?i?=?0?;?i??pcb_new_buf[i].detection_null_or_value?=?THENULL;//detection_null_or_value等于THENULL表示這個結構體為空 ????} }
?
清空默認隊列方法
?
void?bzero_queue(program_queue_t?queue_t) { ????for(int?i?=?0?;?i??pcb_buf[i].detection_null_or_value?=?THENULL; ????} }
?
初始化時間片機構體方法
?
void?init_time_slice(cpu_time_slice_t?time_slice) { ?time_slice?->?time_slice_bulk?=?20;//設置一個時間片時間為20ms ???? }
?
初始化工作隊列方法
?
void?init_program_queue(program_queue_t?queue) { ?bzero_queue(queue); ?bzero_queue_new(queue); ?queue?->?queue_line?=?0; ?queue?->?buf_state?=?BUF;//設置此時工作隊列默認使用的pcb_buf }
?
工作隊列排序方法(按照進程優先級排序)
?
void?queue_sort(program_queue_t?queue_t) { ????int?len?=?queue_t?->?queue_line;//獲取工作隊列長度 ????if(len?==?0) ????????return; ????if(queue_t?->?buf_state?==?BUF)//判斷當前使用哪個數組作為工作隊列 ????{ ????????bzero_queue_new(queue_t); ????????struct?PCB?tm; ??tm.program_status?=?-1; ????????PCB_T?tmp_pcb?=?&tm; ????????for(int?i?=?0?;?i??queue_line?;?i++)//開始排序 ????????{???????????? ????????????for(int?j?=?0;?j??queue_line?;?++j) ????????????{ ????????????????if(queue_t?->?pcb_buf[j].detection_null_or_value?==?THENULL) ????????????????????continue;???????????????????? ????????????????if(j?<=?0) ????????????????????tmp_pcb?=?&(queue_t?->?pcb_buf[j]); ????????????????else?if(-1?==?tmp_pcb?->?program_status) ????????????????????tmp_pcb?=?&(queue_t?->?pcb_buf[j]); ????????????????else ????????????????{ ????????????????????if(tmp_pcb?->?prior_number?>?queue_t?->?pcb_buf[j].prior_number) ????????????????????{ ????????????????????????tmp_pcb?=?&(queue_t?->?pcb_buf[j]); ????????????????????} ????????????????} ????????????} ????????????queue_t?->?pcb_new_buf[i]?=?*tmp_pcb; ????????????tmp_pcb?->?detection_null_or_value?=?THENULL; ????????} ????????queue_t?->?buf_state?=?NEWBUF;//由另一個數組接收排序結果,排序完全結束后切換列表目前使用的數組 ????} ????else ????{ ????????bzero_queue(queue_t); ????????struct?PCB?tm; ??tm.program_status?=?-1; ????????PCB_T?tmp_pcb?=?&tm; ????????for(int?i?=?0?;?i??queue_line?;?i++) ????????{???????????? ????????????for(int?j?=?0;?j??queue_line?;?++j) ????????????{ ????????????????if(queue_t?->?pcb_new_buf[j].detection_null_or_value?==?THENULL) ????????????????????continue;???????????????????? ????????????????if(j?<=?0) ????????????????????tmp_pcb?=?&(queue_t?->?pcb_new_buf[j]); ????????????????else?if(-1?==?tmp_pcb?->?program_status) ????????????????????tmp_pcb?=?&(queue_t?->?pcb_new_buf[j]); ????????????????else ????????????????{ ????????????????????if(tmp_pcb?->?prior_number?>?queue_t?->?pcb_new_buf[j].prior_number) ????????????????????{ ????????????????????????tmp_pcb?=?&(queue_t?->?pcb_new_buf[j]); ????????????????????} ????????????????} ????????????} ????????????queue_t?->?pcb_buf[i]?=?*tmp_pcb; ????????????tmp_pcb?->?detection_null_or_value?=?THENULL; ????????} ????????queue_t?->?buf_state?=?BUF; ????} }
?
初始化進程塊PCB
?
void?init_pcb_object(PCB_T?pcb_t,char**?argv,int?i) { ????pcb_t?->?open_file?=?NULL; ?if(NULL?==?(pcb_t?->?open_file?=?fopen(argv[i],"a"))) ?{ ??perror("open?file?error"); ??exit(0); ?} ?pcb_t?->?file_name?=?argv[i];???????? ?pcb_t?->?prior_number?=?rand()?%?8;?//進程優先級由隨機數分配 ?pcb_t?->?arrive_time?=?clock();??//獲取進程進入時間 ?pcb_t?->?program_status?=?WAIT;??//設置進程狀態為等待 ????pcb_t?->?detection_null_or_value?=?THETRUE;?//設置次進程塊非空 ????pcb_t?->?occupy_time_slice_times?=?0;//初始化使用CPU時間為0 ????pcb_t?->?data_write_number?=?0;??????//文件寫入內容長度初始化為0 }
?
主函數
?
int?main(int?argc,char?**argv) { ?if(argc?<=?1) ?{ ??perror("parameter?<=?1"); ??exit(1); ?}???? ? ?struct?program_queue?_queue;?//創建工作隊列對象 ?init_program_queue(&_queue); ? ?struct?cpu_time_slice?cts;??//創建CPU時間片對象 ?init_time_slice(&cts); ????int?program_numer?=?argc?-?1;?//設置目前的進程數為argc?-?1,該變量用于通過下標訪問進程需要-1 ???? ?for(int?i?=?1?;?i?=?program_numer) ????????????break; ????????for(int?i?=?0?;?i?=?cts.time_slice_bulk)//結束時間?-?開始時間?=?預設的時間片長度則進程終止執行 ????????????????{ ????????????????????if(_queue.buf_state?==?BUF) ????????????????????{ ????????????????????????_queue.pcb_buf[i].program_status?=?WAIT; ????????????????????????_queue.pcb_buf[i].prior_number++;//進程使用CPU時間+1 ????????????????????????_queue.pcb_buf[i].occupy_time_slice_times++; ????????????????????????_queue.pcb_new_buf[i]?=?_queue.pcb_buf[i];//將進程放到另一個隊列等待 ????????????????????????_queue.pcb_buf[i].detection_null_or_value?=?THENULL;//將當前隊列的此進程塊設為空 ????????????????????} ????????????????????else ????????????????????{ ????????????????????????_queue.pcb_new_buf[i].program_status?=?WAIT; ????????????????????????_queue.pcb_new_buf[i].prior_number++; ????????????????????????_queue.pcb_new_buf[i].occupy_time_slice_times++; ????????????????????????_queue.pcb_buf[i]?=?_queue.pcb_new_buf[i]; ????????????????????????_queue.pcb_new_buf[i].detection_null_or_value?=?THENULL; ????????????????????} ???????????????????? ????????????????????break; ????????????????} ????????????????if(_queue.buf_state?==?BUF) ????????????????{ ????????????????????if(VALUE[_queue.pcb_buf[i].data_write_number]?==?'?')//判斷進程是否已經完成任務 ????????????????????{ ????????????????????????_queue.pcb_buf[i].level_time?=?clock();//獲取結束時間 ????????????????????????printf("program?[?%s?]?execute?end...?program?run?time:?%d?ms ",_queue.pcb_buf[i].file_name,_queue.pcb_buf[i].level_time?-?_queue.pcb_buf[i].arrive_time);//進程的結束時間減去進程的進入時間計算出進程運行耗時 ????????????????????????_queue.pcb_buf[i].detection_null_or_value?=?THENULL; ????????????????????????fclose(_queue.pcb_buf[i].open_file);//關閉文件 ????????????????????????program_numer--;//進程數-1 ????????????????????????break; ????????????????????} ????????????????????if(-1?==?fputc(VALUE[_queue.pcb_new_buf[i].data_write_number],_queue.pcb_new_buf[i].open_file))//向文件中寫入數據 ????????????????????{ ????????????????????????perror("write?error "); ????????????????????????perror(strerror()); ????????????????????????for(int?i?=?0?;?_queue.queue_line?;?i++) ????????????????????????{ ????????????????????????????fclose(_queue.pcb_buf[i].open_file); ????????????????????????} ????????????????????????//exit(1); ????????????????????} ????????????????????else ????????????????????{ ????????????????????????_queue.pcb_buf[i].data_write_number++;//寫入內容的長度+1 ????????????????????} ????????????????} ????????????????else ????????????????{ ????????????????????if(VALUE[_queue.pcb_new_buf[i].data_write_number]?==?'?') ????????????????????{ ????????????????????????_queue.pcb_new_buf[i].level_time?=?clock(); ????????????????????????printf("program?[?%s?]?execute?end...?program?run?time:?%d?ms ",_queue.pcb_new_buf[i].file_name,_queue.pcb_new_buf[i].level_time?-?_queue.pcb_new_buf[i].arrive_time); ????????????????????????_queue.pcb_new_buf[i].detection_null_or_value?=?THENULL; ????????????????????????fclose(_queue.pcb_new_buf[i].open_file); ????????????????????????program_numer--; ????????????????????????break; ????????????????????} ????????????????????if(-1?==?fputc(VALUE[_queue.pcb_new_buf[i].data_write_number],_queue.pcb_new_buf[i].open_file)) ????????????????????{ ????????????????????????perror("write?error "); ????????????????????????perror(strerror()); ????????????????????????for(int?i?=?0?;?_queue.queue_line?;?i++) ????????????????????????{ ????????????????????????????fclose(_queue.pcb_new_buf[i].open_file); ????????????????????????} ????????????????????????//exit(1); ????????????????????} ????????????????????else ????????????????????{ ????????????????????????_queue.pcb_new_buf[i].data_write_number++; ????????????????????} ????????????????} ????????????} ????????} ???????? ????????if(_queue.buf_state?==?BUF)//更換當前隊列 ????????{ ????????????_queue.buf_state?=?NEWBUF; ????????????queue_sort(&_queue);//重新按照優先級排序隊列 ????????} ????????else ????????{ ????????????_queue.buf_state?=?BUF; ????????????queue_sort(&_queue); ????????} ????} ???? ????return?0?; }
?
審核編輯:湯梓紅
評論
查看更多