歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> linux進程調度模擬

linux進程調度模擬

日期:2017/3/2 16:40:25   编辑:Linux服務器
/*模擬實現LINUX進程調度的靜態優先級算法和時間片輪轉算法引入LINUX調度
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#define RUN 1
#define SLEEP 0
#define READY 2
#define DEG_SCHEDULE
#define NUM 6
struct OSPCB
{
int PcbName ; /*進程名字*/
int ReqCount; /*進程執行計數*/
int RunTime; /*進程執行時間數*/
int Prority; /*進程優先級*/
int PcbStatus; /*進程狀態*/
int PcbTime; /*進程時間片*/
struct OSPCB* prev;
struct OSPCB *next;
};
struct ProcessQueue /*模擬CPU調度隊列*/
{
struct OSPCB *PointerHead; /*指向進程鏈表頭*/
int PcbNumber; /*CPU每次調度計數器*/
};
//static struct CriticalResource
//{
// int flag;
// char BufferVoice[2000];
//}
static int flag;
void *Function(int *arg);
void InitPcb(struct OSPCB *pcb);
int Schedule(struct ProcessQueue *queue);
void InheritSchedule(struct OSPCB *pcb);
int main(void)
{
int i,ret;
struct OSPCB *pNewPcb,*pNew;
struct ProcessQueue *pNewQueue;
int a[4][4] = {{1,1,0,1},{2,2,0,2},{3,3,0,3},{4,4,0,4}};
pNewQueue = (struct ProcessQueue *)malloc(sizeof(struct ProcessQueue));
pNewQueue->PointerHead = NULL;
pNewQueue->PcbNumber = 0;
for(i = 0; i < 4;i++) /*進程初始化*/
{
pNewPcb = (struct OSPCB *)malloc(sizeof(struct OSPCB));
pNewPcb->PcbName = a[i][0];
pNewPcb->ReqCount = a[i][1];
pNewPcb->RunTime = a[i][2];
pNewPcb->Prority = a[i][3];
pNewPcb->PcbStatus = READY;
pNewPcb->PcbTime = 3;
InitPcb(pNewPcb);
if(pNewQueue->PointerHead == NULL)
{
pNewQueue->PointerHead = pNewPcb;
}else{
 

; pNew->next = pNewPcb;
pNewPcb->prev = pNew;
}
pNew = pNewPcb;
pNewQueue->PcbNumber++;
}
#if 0
for(p = pNewQueue->PointerHead; p != NULL; p = p->next)
{
printf("process name = %d\n",p->PcbName);
}
#endif
Schedule(pNewQueue);/*進入進程調度*/
return 0;
}
void InitPcb(struct OSPCB *pcb)
{
pcb->prev = NULL;
pcb->next = NULL;
}
int Schedule(struct ProcessQueue *queue) /*進程調度*/
{
struct OSPCB *pcb,*CurrRun;
int value,SechNumber = 8;
pthread_t pthread_id[NUM];
int i = 0;
// printf("%s\n",__FUNCTION__);
for(pcb = queue->PointerHead;pcb !=NULL;pcb = pcb->next)
{
if(pcb->PcbTime == 0)
{
pcb->Prority +=4;
}
pcb->PcbTime = 3;
}
while(queue->PointerHead != NULL)
{
for(pcb = queue->PointerHead;pcb !=NULL;pcb = pcb->next)
{
if(pcb == queue->PointerHead)
{
CurrRun = pcb;
}else{
if(CurrRun->Prority < pcb->Prority)
CurrRun = pcb;
}
CurrRun->PcbStatus = RUN;
}
SechNumber--;
CurrRun->ReqCount--;
CurrRun->PcbTime--;
if(i != (CurrRun->PcbName))
{
i = CurrRun->PcbName;
pthread_create(&pthread_id[i],NULL,(void*)Function,&(CurrRun->PcbName));
}
#ifdef DEG_SCHEDULE
printf("present process = %d CurrRun->ReqCount = %d\n",CurrRun->PcbName,CurrRun->ReqCount);
#endif
if(CurrRun->PcbTime == 0)
{
CurrRun->Prority -=4; /*進程懲罰性降優先級處理*/
}
if(CurrRun->ReqCount == 0)
{
if(CurrRun == queue->PointerHead)
{
queue->PointerHead = CurrRun->next;
}else if (CurrRun->next != NULL){
&n

bsp; CurrRun->prev->next = CurrRun->next;
CurrRun->next->prev = CurrRun->prev;
}else{
CurrRun->prev->next = NULL;
}
// printf("Run process name = %d Reqcount = %d Sechedule count = %d\n",CurrRun->PcbName,CurrRun->ReqCount,SechNumber);
}
if(SechNumber == 0)/*時間片用完重新調度*/
{
Schedule(queue);
}
}
return 0;
}
void SleepProcess(void)
{
}
void DeleteProcess(void)
{
// return 0;
}
void *Function(int *arg) /*進程執行函數*/
{
int i,count = 0;
int PthreadName;
struct OSPCB *pNew;
pNew = (struct OSPCB *)arg;
PthreadName = *(int *)arg;
#ifdef DEG_SCHEDULE
printf("Enter the function process %d\n", PthreadName);
#endif
while(1)
{
if(flag == 0) /*訪問臨界區資源*/
{
flag = 1;
#ifdef DEG_SCHEDULE
printf("get lcok in process %d\n", PthreadName);
#endif
for(i = 0; i < 10000; i++)
{
pNew->PcbStatus = SLEEP;
sleep(1);
}
flag = 0;/*釋放臨界區資源*/
break;
}else{ /*自旋直到得到可訪問的臨界區資源*/
count++;
if(count == 5000)
{
#ifdef DEG_SCHEDULE
printf("flag = %d can not get lock in process name %d\n",flag,PthreadName);
#endif
}
}
}
#ifdef DEG_SCHEDULE
printf("flag = %d out process %d\n", flag,PthreadName);
#endif
}
Copyright © Linux教程網 All Rights Reserved