用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号进程运行完毕!

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(3358) | 评论(0) | 转发(0) |
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...
给主人留下些什么吧!~~
原创粉丝点击