Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1064476
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-23 14:52:58

/*
 *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号进程运行完毕!
阅读(905) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~