banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

操作系统,模拟进程管理之PCB块管理法,C语言实现

首先大家祝福平安夜快乐啊,今天要发布的代码是一款 C 语言编写的模拟操作系统管理进程的程序调试环境 TC,使用了 PCB 进行进程管理控制,建立三个基本的队列:等待、执行、阻塞进行模拟操作系统的进程管理,模拟进程的调度,模拟用户的创建、执行、阻塞、挂起、唤醒等操作

最近要准备准备操作系统考试,所以放一个程序跟大家分享

代码如下:

/*
*yctc cg
*/
#include "stdio.h"
#include "dos.h"
#include "stdlib.h"
#include "conio.h"
#define SEC 3
#define NULL 0
/* 定义结构体 */
typedef struct PCB
{
int PID;
int UID;
struct PCB * next;
}PCB;
PCB *really , *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 ("输入 PID 和 UID 以创建新进程 \n");
scanf("%d %d",&p -> PID,&p -> UID);
if(enqueue(really , p))
printf ("创建进程: PID = % d UID = % d\n", p -> PID , p -> UID);
else
printf ("创建失败 \n");
}/*create*/

/* 执行 fexcute*/
int fexcute()
{
PCB *p = dequeue(really);
if(p == NULL)
{
printf ("队列中没有进程 \n");
return 0;
}
else
{
enqueue(excute , p);
printf ("将进程添加到执行队列: PID = % d UID= % d\n" ,p->PID , p->UID);
return 1;
}
}/*excute*/

int suspend()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
printf ("队列中没有进程 \n");
return 0;
}
else
{
enqueue(really , p);
printf ("将进程添加到等待队列: PID = % d UID= % d\n" ,p->PID , p->UID);
return 1;
}
}

int wake()
{
PCB *p = dequeue(wait);
if(p == NULL)
{
printf ("队列中没有进程 \n");
return 0;
}
else
{
enqueue(really , p);
printf ("将进程添加到真实队列: PID = % d UID= % d\n" ,p->PID , p->UID);
return 1;
}
}

int block()
{
PCB *p = dequeue(excute);
if(p == NULL)
{
printf ("队列中没有进程 \n");
return 0;
}
else
{
enqueue(wait , p);
printf ("将进程添加到等待队列: PID = % d UID= % d\n" ,p->PID , p->UID);
return 1;
}
}/*block*/
/* 输出队列 outputqueue*/
int outputqueue(PCB *head)
{
PCB *p;
if(head -> next == NULL)
{/* 队列为空 */
printf ("队列为空 \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 ("真实队列:\n");
outputqueue(really);
printf ("执行队列: \n");
outputqueue(excute);
printf ("等待队列: \n");
outputqueue(wait);
}/*output*/
/*init 初始化 */
int init()
{
PCB *p;
clrscr();
really = (PCB*)malloc(sizeof(PCB));
really -> next=NULL;
excute = (PCB*)malloc(sizeof(PCB));
excute -> next=NULL;
wait = (PCB*)malloc(sizeof(PCB));
wait -> next = NULL;
printf ("____________进程调度___________\n");
printf ("现在初始化中.....................\n");
printf ("输入 PID 和 UID 作为整数,0 0 表示结束 \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(really , p))
{
printf ("新进程 PID = % d UID = % d 已添加!\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 ("执行队列中没有进程 \n");
return 0;
}
else
{
p = excute -> next;
printf ("系统将睡眠 % ds 作为进程运行时间 \n",s);
sleep(3);/*sleep as process runing time*/
printf ("进程: PID = % d UID= % d 执行成功..\n" , p -> PID , p -> UID );
excute -> next = p -> next;
free(p);
}
}/*run*/
/* 离开 */
int leave()
{
PCB *p,*t;
while(really->next || excute->next || wait->next)
{
p = really -> next;
while(p)
{
t = p -> next;
free(p);
p = t;
}
really -> 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 ("_____________________帮助菜单_____________________\n");
printf ("t-h 帮助 显示帮助选项 \n");
printf ("t-c 创建 创建一个新进程,并放入真实队列 \n");
printf ("t-b 阻塞 在执行队列中阻塞一个进程 \n");
printf ("t-w 唤醒 在等待队列中唤醒一个进程 \n");
printf ("t-e 执行 在真实队列中执行一个进程 \n");
printf ("t-s 暂停 在执行队列中暂停一个进程 \n");
printf ("t-o 输出 输出所有队列中的进程 \n");
printf ("t-r 运行 执行一个在执行队列中的进程 \n");
printf ("t-x 退出 退出此程序 \n");
printf("___________________________________________________\n");
printf ("t 输入 'H' 将显示此菜单 \n");
}/*help*/

int main()
{
char COMMAND = NULL;
if( init() != 1)
{
printf ("初始化失败!\n");
getch();
exit(0);
}
else
{
printf ("初始化... 成功 \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*/

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。