オペレーティングシステム、シミュレーションプロセス管理のPCBブロック管理法、C言語実現
10285 ワード
まず、皆さん、クリスマスイブおめでとうございます.今日発表するコードはC言語で作成されたオペレーティングシステム管理プロセスをシミュレートするプログラムデバッグ環境TCです.PCBを使用してプロセス管理制御を行い、3つの基本的なキューを構築します.待機、実行、ブロックはシミュレートオペレーティングシステムのプロセス管理、シミュレートプロセスのスケジューリング、シミュレートユーザーの作成、実行、ブロック、サスペンド、起動などの操作
最近オペレーティングシステムの試験を準備するので、プログラムを置いてみんなと分かち合います.
コードは次のとおりです.
--------------------------------------------------------------------------------------
-著作権宣言:
-本ページに特別な説明がなければ、本文の内容はすべて[李大仁ブログ]オリジナルで、本文の著作権は[李大仁ブログ]の所有に帰属します.
-転載を歓迎します.転載は必ず文章のページの明らかな位置に原文のリンクを提供し、出典を明記してください.本文の転載時にこの声明を保留することを歓迎します.
-文章タイトル:オペレーティングシステム、シミュレーションプロセス管理のPCBブロック管理法、C言語実現
-独立ブログ:李大仁ブログ
-永続リンク:http://www.lidaren.com/archives/261
--------------------------------------------------------------------------------------
以上の内容はブログ自動配信ツールにより自動配信され、最終的に表示内容や効果が原文内容とずれてしまうことがありますのでご了承ください.
最近オペレーティングシステムの試験を準備するので、プログラムを置いてみんなと分かち合います.
コードは次のとおりです.
/*
*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("input PID and UID to a new processn");
scanf("%d %d",&p -> PID,&p -> UID);
if(enqueue(really , p))
printf("create a process: PID = %d UID = %dn", p -> PID , p -> UID);
else
printf("create Failedn");
}/*create*/
/* fexcute*/
int fexcute()
{
PCB *p = dequeue(really);
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(really , p);
printf("add a process into really 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(really , p);
printf("add a process into wait really 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("REALLLY QUEUE:n");
outputqueue(really);
printf("EXCUTE QUEUE: n");
outputqueue(excute);
printf("WAIT QUEUE: 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("____________PROCESS SECHUDLE__________n");
printf("now initing.....................n");
printf("input PID and UID as integer , 0 0 as overn");
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("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 runningn",s);
sleep(3);/*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(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("_____________________HELP MENU_____________________n");
printf("t-h HELP show help optionn");
printf("t-c CREATE create a new process , and put to really queuen");
printf("t-b BLOCK block a process in excute queuen");
printf("t-w WAKE wake a process in wait queuen");
printf("t-e EXCUTE excute a process in really queuen");
printf("t-s SUSPEND suspend a process in excute queuen");
printf("t-o OUTPUT output all processes in queuesn");
printf("t-r RUN excute a process in excute queuen");
printf("t-x EXIT exit this programn");
printf("___________________________________________________n");
printf("t type 'H' will show this menun");
}/*help*/
int main()
{
char COMMAND = NULL;
if( init() != 1)
{
printf("init falied ! n ");
getch();
exit(0);
}
else
{
printf("init...OKn");
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*/
--------------------------------------------------------------------------------------
-著作権宣言:
-本ページに特別な説明がなければ、本文の内容はすべて[李大仁ブログ]オリジナルで、本文の著作権は[李大仁ブログ]の所有に帰属します.
-転載を歓迎します.転載は必ず文章のページの明らかな位置に原文のリンクを提供し、出典を明記してください.本文の転載時にこの声明を保留することを歓迎します.
-文章タイトル:オペレーティングシステム、シミュレーションプロセス管理のPCBブロック管理法、C言語実現
-独立ブログ:李大仁ブログ
-永続リンク:http://www.lidaren.com/archives/261
--------------------------------------------------------------------------------------
以上の内容はブログ自動配信ツールにより自動配信され、最終的に表示内容や効果が原文内容とずれてしまうことがありますのでご了承ください.