模擬進程調度

功能

data.h

#ifndef _Data_h_
#define _Data_h_#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define ElemType PCB
#define Status int
#define OK		1
#define	ERROR	0
#define TimeSlice	1
#define Infinity 10 //INT_MAX#define NAME_MAXSIZE 20
typedef enum 
{Ready,Running,Block
}ProState;typedef enum 
{FCFS, SPF		//先來先服務,短進程優先
}PriorityRule;typedef struct 
{char Name[NAME_MAXSIZE];	//進程名int Priority;				//優先數int ArrivalTime;			//到達時間		以時間片為單位int NeedRunningTime;		//運行時間		以時間片為單位int StartTime;				//開始執行時間int FinishTime;				//完成時間int TimeUsedCPU;			//已用CPU時間		以時間片為單位ProState ProcessState;		//進程狀態
}PCB;typedef struct Node
{ElemType data;struct Node * Next;		
}LNode,*LinkList;#endif

?ChainList.h

#ifndef _ChainList_h_
#define _ChainList_h_#include "Data.h"//功能:鏈表初始化
Status Init(LinkList *L);//功能:賦值運算,將e2賦值給e1
void Assignment(ElemType *e1, ElemType e2);//功能:獲取第i個結點元素
Status GetElemt_L(LinkList L,int i,ElemType *e);//功能:鏈表根據優先級插入元素
Status ListInsert_L(LinkList L,ElemType e);//功能:鏈表刪除頭結點
Status ListDelete_L(LinkList L,ElemType *e);#endif

ProPCB.h

#ifndef _ProPCB_h_
#define _ProPCB_h_#include "ChainList.h"//功能:將e插入鏈表Q
Status GetProcess(LinkList Q,ElemType e);		//上就緒隊列//功能:根據不同的優先級規則,返回優先數
int GetPriority(ElemType *e, PriorityRule PR);  //根據不同的規則PR 設置優先數//功能:將鏈表Q的頭結點數據放到e指向的內存,并刪除
Status OutProsess(LinkList Q,ElemType *e);	    //下就緒隊列//功能:CPU運行pcb指向的進程,并輸出所有進行進程狀態
Status CPURunPro(LinkList Q, PCB *pcb);	        //CPU運行PCB//功能:打印所有PCB信息
void PrintProQueue(LinkList Q, PCB *pcb);		//打印運行后PCB信息//功能:當一個進程結束,打印進程信息
void PrintProResult(PCB *pcb);#endif

實現

#include "ChainList.h"extern int CPUUsedTime;//功能:鏈表初始化
Status Init(LinkList *L)
{*L = (LinkList)malloc(sizeof(LNode));(*L)->data.NeedRunningTime = -1;(*L)->Next = NULL;return OK;
}//功能:賦值運算,將e2賦值給e1
void Assignment(ElemType *e1, ElemType e2)
{e1->ArrivalTime = e2.ArrivalTime;strcpy(e1->Name,e2.Name);e1->Priority = e2.Priority;e1->ProcessState = e2.ProcessState;e1->FinishTime = e2.FinishTime;e1->StartTime = e2.StartTime;e1->NeedRunningTime = e2.NeedRunningTime;e1->TimeUsedCPU = e2.TimeUsedCPU;
}//鏈表中按照優先級:從大到小排序插入
Status ListInsert_L(LinkList L,ElemType e)	//這樣修改應該不對 p = *L出錯
{LinkList p = L->Next, pre = L, s;while (p && e.Priority <= p->data.Priority)	{pre = p;p = p->Next;}s = (LinkList)malloc(sizeof(LNode));Assignment(&s->data, e);s->Next = pre->Next;pre->Next = s;return OK;
}
//鏈表中頭部刪除
Status ListDelete_L(LinkList L,ElemType *e)
{LinkList p = L, q;q = p->Next;if(!q)return ERROR;p->Next = q->Next;Assignment(e, q->data);free(q);return OK;
}
#include "ProPCB.h"extern int CPUUsedTime;//功能:將e插入鏈表Q
Status GetProcess(LinkList Q,ElemType e)
{return ListInsert_L(Q, e);
}//功能:根據不同的優先級規則,返回優先數
int GetPriority(ElemType *e, PriorityRule PR)
{if(PR == FCFS)return Infinity - e->ArrivalTime;else if(PR == SPF)return Infinity - e->NeedRunningTime;elseprintf("GetPriority Function ERROR!\n");return ERROR;
}//功能:將鏈表Q的頭結點數據放到e指向的內存,并刪除
Status OutProsess(LinkList Q,ElemType *e)
{return ListDelete_L(Q ,e);
}//上一次CPU運行時間增加1個時間片
Status CPURunPro(LinkList Q,PCB *pcb)
{if(pcb->StartTime == -1)pcb->StartTime = CPUUsedTime;pcb->ProcessState = Running;//PrintProQueue(Q, pcb);pcb->TimeUsedCPU += TimeSlice;return OK;
}//功能:打印所有PCB信息
void PrintProQueue(LinkList Q, PCB *pcb)
{LinkList p = Q->Next;printf("進程名  優先數  到達時間  運行時間  已用CPU時間  完成時間  進程狀態\n");if(pcb)printf(" %4s     %2d      %4d      %4d     %3d(+1)       %3d        %4s  \n",pcb->Name,pcb->Priority,pcb->ArrivalTime,pcb->NeedRunningTime,pcb->TimeUsedCPU, pcb->FinishTime,pcb->ProcessState == Ready ? "就緒" : "運行");while (p){printf(" %4s     %2d      %4d      %4d     %3d           %3d        %4s  \n",p->data.Name,p->data.Priority,p->data.ArrivalTime,p->data.NeedRunningTime,p->data.TimeUsedCPU,p->data.FinishTime, p->data.ProcessState == Ready ? "就緒" : "運行");p = p->Next;}printf("-------------------------------------------------------------------------------\n");
}//功能:當一個進程結束,打印進程信息
void PrintProResult(PCB *pcb)
{printf("進程名  到達時刻 運行時間 開始時刻 完成時刻 周轉時間 帶權周轉時間 進程狀態\n");if(pcb)printf(" %2s     %3d      %4d        %4d      %3d     %4d       %5.2lf       %4s  \n",pcb->Name,pcb->ArrivalTime,pcb->NeedRunningTime,pcb->StartTime,pcb->FinishTime,pcb->FinishTime-pcb->ArrivalTime,((pcb->FinishTime - pcb->ArrivalTime)*1.0)/pcb->NeedRunningTime,"完成");printf("-------------------------------------------------------------------------------\n");
}

main:

#include "ProPCB.h"/****************************
*  實驗01: 非搶占式靜態優先權	*
*  ① 優先權始終保持不變		*
*  ② 一旦進入CPU便運行到結束	*
*  ③ FCFS只考慮到達時間進CPU	*
*  ④ SPF認為到達時間相同		*
****************************/int CPUUsedTime = 0;void InputData(LinkList * pPCBdata, PriorityRule PR)
{ElemType e = {{0},-1,-1,-1,-1,-1,0,Ready};e.ArrivalTime = 0;e.ProcessState = Ready;e.TimeUsedCPU = 0;strcpy(e.Name,"A");e.NeedRunningTime = 1;e.Priority = GetPriority(&e, PR);if(PR == SPF)   e.ArrivalTime = 0;GetProcess(*pPCBdata,e);e.ArrivalTime = 1;e.ProcessState = Ready;e.TimeUsedCPU = 0;strcpy(e.Name,"B");e.NeedRunningTime = 100;e.Priority = GetPriority(&e, PR);if(PR == SPF)   e.ArrivalTime = 0;GetProcess(*pPCBdata,e);e.ArrivalTime = 2;e.ProcessState = Ready;e.TimeUsedCPU = 0;strcpy(e.Name,"C");e.NeedRunningTime = 1;e.Priority = GetPriority(&e, PR);if(PR == SPF)   e.ArrivalTime = 0;GetProcess(*pPCBdata,e);e.ArrivalTime = 3;e.ProcessState = Ready;e.TimeUsedCPU = 0;strcpy(e.Name,"D");e.NeedRunningTime = 100;e.Priority = GetPriority(&e, PR);if(PR == SPF)   e.ArrivalTime = 0;GetProcess(*pPCBdata,e);}//void InputData1(LinkList * pPCBdata, PriorityRule PR)
//{
//    ElemType e = {{0},-1,-1,-1,-1,-1,0,Ready};
//    e.ArrivalTime = 0;
//    e.ProcessState = Ready;
//    e.TimeUsedCPU = 0;
//    strcpy(e.Name,"A");
//    e.NeedRunningTime = 4;
//    e.Priority = GetPriority(&e, PR);
//    if(PR == SPF)   e.ArrivalTime = 0;
//    GetProcess(*pPCBdata,e);
//
//    e.ArrivalTime = 1;
//    e.ProcessState = Ready;
//    e.TimeUsedCPU = 0;
//    strcpy(e.Name,"B");
//    e.NeedRunningTime = 3;
//    e.Priority = GetPriority(&e, PR);
//    if(PR == SPF)   e.ArrivalTime = 0;
//    GetProcess(*pPCBdata,e);
//
//    e.ArrivalTime = 2;
//    e.ProcessState = Ready;
//    e.TimeUsedCPU = 0;
//    strcpy(e.Name,"C");
//    e.NeedRunningTime = 5;
//    e.Priority = GetPriority(&e, PR);
//    if(PR == SPF)   e.ArrivalTime = 0;
//    GetProcess(*pPCBdata,e);
//
//    e.ArrivalTime = 3;
//    e.ProcessState = Ready;
//    e.TimeUsedCPU = 0;
//    strcpy(e.Name,"D");
//    e.NeedRunningTime = 2;
//    e.Priority = GetPriority(&e, PR);
//    if(PR == SPF)   e.ArrivalTime = 0;
//    GetProcess(*pPCBdata,e);
//
//    e.ArrivalTime = 4;
//    e.ProcessState = Ready;
//    e.TimeUsedCPU = 0;
//    strcpy(e.Name,"E");
//    e.NeedRunningTime = 4;
//    e.Priority = GetPriority(&e, PR);
//    if(PR == SPF)   e.ArrivalTime = 0;
//    GetProcess(*pPCBdata,e);
//}int main(void)
{LinkList PCBQueue;	//InitPCBdata里面存放PCB初始數據ElemType e = {{0},-1,-1,-1,-1,-1,0,Ready};ElemType *pcb = NULL;PriorityRule PR;PR = FCFS;	   //   SPF    or   FCFS//***********    初始化就緒隊列    *************//Init(&PCBQueue);InputData(&PCBQueue, PR);printf("初始數據如下:\n");PrintProQueue(PCBQueue, pcb);//***********    進程根據優先級上CPU    *************//printf("\n進程運行信息如下:\n");while (OutProsess(PCBQueue, &e)){//一次性運行完畢while(e.TimeUsedCPU < e.NeedRunningTime)	//上完CPU的進程是否完畢{CPURunPro(PCBQueue, &e);		        //上CPU++CPUUsedTime;					        //CPU時間增加}//***********    當進程執行完畢時打印輸出    *************//e.FinishTime = CPUUsedTime;PrintProResult(&e);}getchar();return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445103.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445103.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445103.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gdb調試多進程和多線程命令

1. 默認設置下&#xff0c;在調試多進程程序時GDB只會調試主進程。但是GDB&#xff08;>V7.0&#xff09;支持多進程的 分別以及同時 調試&#xff0c;換句話說&#xff0c;GDB可以同時調試多個程序。只需要設置follow-fork-mode(默認值&#xff1a;parent)和detach-on-fork…

python外卷(10)--取整

"取整"那些事1.python 內置函數1.1int()--向下取整1.2round()--四舍五入2.math模塊取整函數2.1 math.floor()--向下取整2.2 math.ceil()--向上取整2.3 math.modf()--分別取小數部分和整數部分3.numpy模塊取整函數3.1 numpy.floor()--向下取整3.2 numpy.ceil()--向上取…

模擬銀行家算法

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define true 1 #define false 0 #define OK 1 #define ERROR 0 #define RESOURCE_NUM …

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…

學點數學(3)-函數空間

函數空間1.距離&#xff1a;從具體到抽象2.范數3.內積4.拓撲本博文為觀看《上海交通大學公開課-數學之旅-函數空間 》所整理筆記&#xff0c;公開課視頻連接&#xff1a;http://open.163.com/newview/movie/free?pidM8PTB0GHI&midM8PTBUHT0數學中的空間 是 大家研究工作的…