linux动态优先数调度策略,操作系统(Linux)--按优先数调度算法实现处理器调度...

这道题慢悠悠地做,出现了很多错误,大多都是空指针产生的中断,最后还是实现了,代码写得有点乱,有时间优化下(当然有同学指点下也是不错的,哈哈)。

实习题目:

设计一个按优先数调度算法实现处理器调度的程序。

[提示]:

(1) 假定系统有5个进程,每个进程用一个PCB来代表。PCB的格式为:

进程名、指针、要求运行时间、优先数、状态。

进程名——P1~P5。

指针——按优先数的大小把5个进程连成队列,用指针指出下一个进程PCB的首地址。

要求运行时间——假设进程需要运行的单位时间数。

优先数——赋予进程的优先数,调度时总是选取优先数大的进程先执行。

状态——假设两种状态,就绪,用R表示,和结束,用E表示。初始状态都为就绪状态。

(2) 每次运行之前,为每个进程任意确定它的“优先数”和“要求运行时间”。

(3) 处理器总是选队首进程运行。采用动态改变优先数的办法,进程每运行1次,优先数减1,要求运行时间减1。

(4) 进程运行一次后,若要求运行时间不等于0,则将它加入队列,否则,将状态改为“结束”,退出队列。

(5) 若就绪队列为空,结束,否则,重复(3)。

思路流程图:

3b788185e2b3903c41c7f8b6385bf4a0.png

bbee2c1edf76ba59f0c525f6b902831a.png

e3a9486d9e16b1d6173a0515b8aeeb7f.png

013426c684e1cdc552ac8a08300761f6.png

662220ec432f6f41fa93e2114c19b8db.png

对运行时间的判断:

6210035503d9f23c0e2c7f6b172393da.png

736edbe2a12978949836ed4f04b0f694.png

对优先数的判断:

b899749c2f766a47b05497d15c509bd2.png

fa568625755f16bdfe1d2b7a002f702a.png

270f6b1cb808bb3f04bb066ae34ad75f.png

/*

author:huangpingyi

date:2016 11 24

*/

#include #include #include using namespace std;

static int point = 0;//用于计算进程是否运行完成

struct PCB

{

int pid;

int priority;

int time;

PCB *next;

};

void tailCreate(PCB *L)

{

srand((unsigned int)time(0));//以时间为随机数种子,保证每次随机数不一样

int priority = rand() % 5;//随机优先数

PCB *s, *r = L;

for (int i = 0; i<5; i++)

{

//随机时间为1到50

int number = rand() % 5;

while (number == 0)

//如果是0就一直随机,直到出现不是0的为止

number = rand() % 5;

//tail_insert用尾插法初始化

s = new PCB;

s->pid = i + 1;

s->priority = (i + priority) % 5 + 1;

s->time = number;

if (s->priority != 5 || r->next == NULL)//如果r->next==NULL表示为队列只有一个头结点,就直接插入

{

r->next = s;

r = s;

}

if ((s->priority == 5) && (r->next != NULL))//如果队列不为空,就将它放在头结点后面

{

s->next = L->next;

L->next = s;

}

}

r->next = NULL;

}

void run(PCB *L)//运行

{

PCB *c = L;

PCB *p = L;

for (L; L; L = L->next)

{

if (L->next == NULL)

break;

//由于存在存在头结点,所以从L->next开始

L->next->priority = L->next->priority - 1;

L->next->time = L->next->time - 1;

if (L->next->time == 0)

//如果运行时间为0,就将它移除队列中

{

cout << "run over" "<< L->next->pid << endl;

L->next->time = -1;

L->next = L->next->next;

//由于出现了L->next = L->next->next;这步,

//接着执行for循环的第三个表达式,便跳过了L->next->next这个结点,接着执行L->next->next->next这个结点

//所以需要判断一下L->next->next这个结点

if (L->next != NULL&&L->next->time != 0)

{

L->next->priority = L->next->priority - 1;

L->next->time = L->next->time - 1;

}

//如果L->next->next->time的值等于0,便会将它移除队列,接着执行L->next=L->next->next这步

//所以需要while循环来判断

while (L->next != NULL&&L->next->time == 0)

{

cout << "run over" "<< L->next->pid << endl;

L->next->time = -1;

L->next = L->next->next;

point = point + 1;

if (L->next != NULL)

{

L->next->priority = L->next->priority - 1;

L->next->time = L->next->time - 1;

}

}

point = point + 1;

}

if (L->next != NULL&&L->next->priority == 0)//如果优先数为0就将它变成0放在队首

{

//******

PCB *q = L->next;

L->next = L->next->next;

q->priority = 5;

q->next = c->next;

c->next = q;

//由于执行了L->next=L->next->next

//所以又会执行上面那步同样地操作

if (L->next != NULL&&L->next->time != 0)

{

L->next->priority = L->next->priority - 1;

L->next->time = L->next->time - 1;

}

while (L->next != NULL&&L->next->time == 0)

{

cout << "run over" << "->PID->"pid << endl;

L->next->time = -1;

L->next = L->next->next;

point = point + 1;

if (L->next != NULL)

{

L->next->priority = L->next->priority - 1;

L->next->time = L->next->time - 1;

}

}

}

}

L = p;

}

int main()

{

PCB *L, *m;

L = new PCB;//初始化头结点

L->next = NULL;

tailCreate(L);

m = L;

L = L->next;

cout << "=============" << endl;

cout << "Init" << endl;

cout << "==============" << endl;

for (L; L; L = L->next)

{

cout

cout << "Time :"

cout << "Priority :"

cout << "******* " << endl;

cout << endl;

}

cout << "=============" << endl;

cout << "Init successful!" << endl;

cout << "==============" << endl;

cout << endl;

cout << "run order!" << endl;

while (point != 5)

{

run(m);

}

system("pause");

return 0;

}

windows下:

165ebe6393b227d83e57679d31fa9e3c.png

9ec9a8edb4235398ca0f9bdf42391969.png

ubuntu:

d721ea04afbf320fcc2cb825b7306675.png

31de5176c51317a2c12d61ba2eefac39.png

这里用了srand((unsigned int)time(0));函数以电脑时间为随机数种子,保证每次随机数不一样

这里设置的运行时间为1到5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要定义一个进程的结构体,包含进程名、进程优先、进程状态等信息: ```c #define MAX_PROCESS_NUM 100 // 最大进程 #define PROCESS_NAME_LEN 20 // 进程名长度 typedef struct { int pid; // 进程ID char name[PROCESS_NAME_LEN]; // 进程名 int priority; // 进程优先 int state; // 进程状态,0表示就绪,1表示运行,2表示阻塞 } PCB; PCB process_list[MAX_PROCESS_NUM]; // 进程列表 int process_num = 0; // 当前进程 ``` 接下来,可以实现一个优先调度的函,该函按照进程的优先从高到低排序,然后选择优先最高的进程执行: ```c void schedule() { int i, j; PCB tmp; // 按优先从高到低排序 for (i = 0; i < process_num - 1; i++) { for (j = i + 1; j < process_num; j++) { if (process_list[i].priority < process_list[j].priority) { tmp = process_list[i]; process_list[i] = process_list[j]; process_list[j] = tmp; } } } // 选择优先最高的进程执行 int running_pid = process_list[0].pid; process_list[0].state = 1; // 执行该进程 // ... } ``` 当一个进程创建时,需要为其分配一个唯一的进程ID,可以使用一个全局变量pid_counter来记录已经分配的最大ID,每次创建新进程时,将pid_counter加1并赋值给新进程的pid: ```c int pid_counter = 0; // 进程ID计器 int create_process(char* name, int priority) { if (process_num >= MAX_PROCESS_NUM) { printf("Error: too many processes!\n"); return -1; } PCB new_process; new_process.pid = ++pid_counter; strcpy(new_process.name, name); new_process.priority = priority; new_process.state = 0; // 初始状态为就绪 process_list[process_num++] = new_process; // 加入进程列表 return new_process.pid; } ``` 以上是一个简单的按优先调度算法实现处理器调度的示例。在实际应用中,还需要考虑更多的情况,例如进程的阻塞和唤醒,进程的时间片轮转等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值