首先大家祝福平安夜快樂啊,今天要發布的代碼是一款 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 = % dn", 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 (" 真正的隊列");
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*/