/* *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; }
|