用c语言模拟进程调度

/*
 *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) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值