linux进程调度模拟 linux进程调度详解
创始人
2024-06-07 03:31:13
0
/*模拟实现LINUX进程调度的静态优先级算法和时间片轮转算法引入LINUX调度
*/
#include
#include
#include
#include
#include
#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){
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
}

【编辑推荐】

  1. Linux应用:在Fedora 下安装opera浏览器
  2. linux死机了怎么办 处理办法详细解析
  3. linux块设备,字符设备

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...