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









評(píng)論