用c语言模拟进程调度
来源:互联网 发布:windows c语言多线程 编辑:程序博客网 时间:2024/06/05 08:10
/*
*Author:Mr Sunny
*Time:Oct 2010
*
*/
#include
#include
#include
#include
#define N 3
enum state {
e, r, t, w, c
};
struct PCB {
int id, priority, runningtime;
enum state status;
char name[10];
struct PCB *next;
}pro[N];
//分别代表各个状态队列的头指针
struct PCB * hready;
struct PCB * lready;
struct PCB * wait;
struct PCB * execute;
struct PCB * complete;
struct semaphore {
int id, value;
}s1, s2;
int process_init(); //进程初始化
int insert_queue(struct PCB **head, struct PCB * id); //将id进程添加到head的头队列
int schedule(); //进程调度函数
int pro_running();
int main()
{
process_init();
printf("The %d process have been ready,we can GO!!\n", N);
sleep(1);
while(1) {
schedule();
pro_running();
}
return 0;
}
int schedule()
{
//先查找高就绪队列中的进程
if(hready != NULL) {
execute = hready;
hready = hready->next;
// execute->next = NULL;
return 0;
}
//再查找低就绪队列中的进程
if(lready != NULL) {
// insert_queue(&hready, lready);
hready = lready;
lready = lready->next;
hready->next = NULL;
// hready->next = NULL;
sun_wakeup();
return 0;
}
//说明所有进程运行完毕
}
int pro_running()
{
int i, slice;
printf("%d号进程正在运行!\n", execute->id);
for(slice = execute->runningtime; slice > 0; slice--) {
printf("\t这个进程还剩%d个时间片\n", slice);
}
sleep(1);
printf("%d号进程运行完毕!\n\n", execute->id);
insert_queue(&lready, execute);
// sun_wakeup();
return 0;
}
int sun_wakeup()
{
execute = hready;
hready = hready->next;
return 0;
}
int process_init()
{
int i;
hready = lready = wait = execute = complete = NULL;
for(i = 0; i < N; i++) {
insert_queue(&hready, &pro[i]);
pro[i].id = i;
pro[i].runningtime = i+2;
pro[i].status = r;
pro[i].priority = i;
strcpy(pro[i].name, "苹果");
}
return 0;
}
int insert_queue(struct PCB **head, struct PCB *id)
{
struct PCB *p;
id->next = NULL;
if((*head) == NULL) {
*head = id;
return 0;
} else {
for(p = *head; p->next != NULL; p = p->next);
p->next = id;
}
return 0;
}
运行结果;
The 3 process have been ready,we can GO!!
0号进程正在运行!
这个进程还剩2个时间片
这个进程还剩1个时间片
0号进程运行完毕!
1号进程正在运行!
这个进程还剩3个时间片
这个进程还剩2个时间片
这个进程还剩1个时间片
1号进程运行完毕!
2号进程正在运行!
这个进程还剩4个时间片
这个进程还剩3个时间片
这个进程还剩2个时间片
这个进程还剩1个时间片
2号进程运行完毕!
0号进程正在运行!
这个进程还剩2个时间片
这个进程还剩1个时间片
0号进程运行完毕!
1号进程正在运行!
这个进程还剩3个时间片
这个进程还剩2个时间片
这个进程还剩1个时间片
1号进程运行完毕!
2号进程正在运行!
这个进程还剩4个时间片
这个进程还剩3个时间片
这个进程还剩2个时间片
这个进程还剩1个时间片
2号进程运行完毕!
0号进程正在运行!
这个进程还剩2个时间片
这个进程还剩1个时间片
0号进程运行完毕!
上一篇:父进程干掉子进程
下一篇: 算法设计与分析 实验报告
- APP开发流程,你知道多少...
- BLE-NRF51822教程14-adc和电池...
- 对Linux的进程内核栈的认识...
- Linux系统下init进程的前世今...
- 文件系统存在的深层次的原因如...
- test123
- 编写安全代码——小心有符号数...
- 彻底搞定C语言指针详解-完整版...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
- 用c语言模拟进程调度
- 进程调度模拟(C语言)
- C 模拟进程调度
- 操作系统—C语言进程调度模拟.c
- C语言模拟最高优先数优先进程调度算法
- 操作系统用C语言模拟基于时间片进程调度程序
- 操作系统用C语言模拟进程基于优先级的调度程序
- 模拟进程调度管理[C#]
- C语言 磁盘调度模拟
- 用C语言实现对N个进程的简单时间片轮转法Round Robin的调度模拟
- 进程调度算法模拟,用动态优先数及时间片轮转法实现进程调度_Java语言模拟实现
- 进程调度---c模拟程序实现
- 进程调度模拟程序
- 进程调度模拟
- 模拟进程调度
- 进程调度模拟
- 进程调度模拟算法
- 模拟设计进程调度
- Redis 集群搭建完之后
- zigbee广播限制
- 父进程干掉子进程
- ajax跨域报错
- java容器类
- 用c语言模拟进程调度
- html5使用js确定用户坐标位置
- tensorflow提取VGG特征
- 算法设计与分析 实验报告
- linux下rsync服务的搭建
- android 仿微信、支付宝支付密码框效果
- Oracle中获取系统当前时间和处理时间
- linux基本指令
- 类加载器