模拟进程管理程序设计


1)进程状态至少有运行、就绪和阻塞,相应设置运行队列、就绪队列、等待队列。 (2)设计创建进程、撤消进程、调度进程、阻塞进程、唤醒进程函数执行相应功能。调度算法可选:时间片轮转法、先来先服务、优先级等 (1)进程状态至少有运行、就绪和阻塞,相应设置运行队列、就绪队列、等待队列。 (2)设计创建进程、撤消进程、调度进程、阻塞进程、唤醒进程函数执行相应功能。调度算法可选:时间片轮转法、先来先服务、优先级等<<隐藏
#include "windows.h"
#include "stdio.h"
#include "dos.h"
#include "stdlib.h"
#include "conio.h"
#define SEC 3000 //睡眠时间(ms)
#define NULL 0
/*定义结构体*/
typedef struct PCB
{
int PID;
int UID;
struct PCB * next;
}PCB;
PCB *ready , *excute , *wait;
/*create queue header */
/*queue operation 入队*/
int enqueue(PCB *head , PCB *node)
{
PCB *p;
p = head;
if(p -> next == NULL)
{
head -> next = node;
return 1;
}
while(p)
{
if(p -> next == NULL)
{
p -> next = node;
return 1;
}
else p = p -> next;
}
}/*enquue*/
/*dequeue 出队列 */
PCB * dequeue(PCB *head)
{
PCB *p;
p = head;
if(p -> next == NULL)
{
return NULL;
}
else
{
p = p -> next;
head -> next = p -> next;
p -> next = NULL;
return p;
}
/*head to next*/
}/*dequeue*/
/*PCB operate*/
/*新建进程*/
int create()
{
PCB *p;
p = (PCB*)malloc(sizeof(PCB));
p -> next = NULL;
printf("input PID and UID to a new process\n");
scanf("%d %d",&p -> PID,&p -> UID);
if(enqueue(ready , p))
printf("create a process: PID = %d UID = %d\n", p -> PID , p -> UID);
else
printf("create Failed\n");
return(0);
}/*create*/
/*将就绪队列的先到进程加入到运行队列中 fexcute*/
int fexcute()
{
PCB *p = dequeue(ready);
if(p == NULL)
{
printf("NO process in queue \n");
return 0;
}
else
{
enqueue(excute , p);
printf("add a process into excute queue process: PID = %d UID= %d \n" ,p->PID , p->UID);
return 1;
}
}/*excute*/
//挂起正在运行的进程
int suspend()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
printf("NO process in queue \n");
return 0;
}
else
{
enqueue(ready , p);
printf("add a process into ready queue process: PID = %d UID= %d \n" ,p->PID , p->UID);
return 1;
}
}
//唤醒等待队列中的进程
int wake()
{
PCB *p = dequeue(wait);
if(p == NULL)
{
printf("NO process in queue \n");
return 0;
}
else
{
enqueue(ready , p);
printf("add a process into wait ready process: PID = %d UID= %d \n" ,p->PID , p->UID);
return 1;
}
}
//阻塞运行队列中的进程
int block()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
printf("NO process in queue \n");
return 0;
}
else
{
enqueue(wait , p);
printf("add a process into wait queue process:

PID = %d UID= %d \n" ,p->PID , p->UID);
return 1;
}
}/*block*/
/*输出队列 outputqueue*/
int outputqueue(PCB *head)
{
PCB *p;
if(head -> next == NULL)
{/*队列为空*/
printf("queue is null \n");
return 1;
}
p = head -> next; /*node pointer*/
while(p)
{/*打印process id UID*/
printf("PID = %d UID = %d \n" , p -> PID , p -> UID);
p = p -> next;
}
return 0;
}
/*output输出*/
int output()
{
printf("READY QUEUE:\n");
outputqueue(ready);
printf("EXCUTE QUEUE: \n");
outputqueue(excute);
printf("WAIT QUEUE: \n");
outputqueue(wait);
return(0);
}/*output*/
/*init 初始化*/
int init()
{
PCB *p;

ready = (PCB*)malloc(sizeof(PCB));
ready -> next=NULL;
excute = (PCB*)malloc(sizeof(PCB));
excute -> next=NULL;
wait = (PCB*)malloc(sizeof(PCB));
wait -> next = NULL;
printf("____________PROCESS SECHUDLE__________\n");
printf("now initing.....................\n");
printf("input PID and UID as integer , 0 0 as over\n");
while(1)
{
p = (PCB*)malloc(sizeof(PCB));
p -> next = NULL;
scanf("%d %d",&p -> PID , &p -> UID);
if(p -> PID == 0 && p -> UID == 0)
break;
else
{
if(enqueue(ready , p))
{
printf("new process PID = %d UID = %d added!\n",p -> PID , p -> UID);
}
else return 0;
}
}
return 1;
}/*init*/
/*运行一个process 并释放节点*/
int run()
{
PCB *p = excute;
int s = SEC;
if(excute -> next == NULL)
{
printf("no process in excute queue \n");
return 0;
}
else
{
p = excute -> next;
printf("system will sleep %ds as process running\n",s);
Sleep(s);/*sleep as process runing time*/
printf("process: PID = %d UID= %d excute successed..\n" , p -> PID , p -> UID );
excute -> next = p -> next;
free(p);
}
}/*run*/
/*离开*/
int leave()
{
PCB *p,*t;
while(ready->next || excute->next || wait->next)
{
p = ready -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
ready -> next = NULL;
p = wait -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
wait -> next = NULL;
p = excute -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
excute -> next = NULL;
}
exit(0);
}/*leace*/
//帮助菜单
int help()
{
printf("_____________________HELP MENU_____________________\n");
printf("\t-h HELP 弹出帮助菜单\n");
printf("\t-c CREATE 创建一个新进程,并加入到就绪队列中\n");
printf("\t-b BLOCK 阻塞正在运行的进程\n");
printf("\t-w WAKE 唤醒等待队列中的进程\n");
printf("\t-e EXCUTE 运行就绪队列中的进程\n");
printf("\t-s SUSPEND 挂起正在运行的进程\n");
printf("\t-o OUTPUT 输出所有进程队列\n");
printf("\t-r RUN 运行运行队列中的进程\n");
printf("\t-x EXIT 释放内存并退出程序\n");
printf("___________________________________________________\n");
printf("\t 输

入‘h/H’弹出帮助菜单\n");
return(0);
}/*help*/
//zhu
int main()
{
char COMMAND = NULL;
if( init() != 1)
{
printf("init falied ! \n ");
getch();
exit(0);
}
else
{
printf("init...OK\n");
output();
help();
}
while(1)
{
/*当三队列都不空 执行调度 */
printf(">");
scanf("%c",&COMMAND);
switch(COMMAND)
{
case '\n': break;
case 'H':
case 'h': help(); break;
case 'C':
case 'c': create(); break;
case 'B':
case 'b': block(); break;
case 'W':
case 'w': wake(); break;
case 'S':
case 's': suspend(); break;
case 'E':
case 'e': fexcute(); break;
case 'O':
case 'o': output(); break;
case 'X':
case 'x': leave(); break;
case 'R':
case 'r': run(); break;
}
}
}/*main*/

相关文档
最新文档