banner
李大仁博客

李大仁博客

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

オペレーティングシステム、プロセス管理のPCBブロック管理法のシミュレーション、C言語実装

まず皆さん、平安夜おめでとうございます。今日発表するコードは、C 言語で書かれたオペレーティングシステムのプロセス管理を模擬するプログラムデバッグ環境 TC で、PCB を使用してプロセス管理制御を行い、待機、実行、ブロックの 3 つの基本的なキューを構築してオペレーティングシステムのプロセス管理を模擬し、プロセスのスケジューリング、ユーザーの作成、実行、ブロック、一時停止、再開などの操作を模擬します。

最近、オペレーティングシステムの試験の準備をしているので、プログラムを皆さんと共有します。

コードは以下の通りです:

/*
*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;
/* キューのヘッダーを作成 */
/* キュー操作 入隊 */
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*/
/* 出隊列 */
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;
}
/* ヘッダーから次へ */
}/*dequeue*/

/*PCB 操作 */
/* 新しいプロセスを作成 */
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; /* ノードポインタ */
while(p)
{/* プロセス ID UID を印刷 */
printf("PID = %d UID = %d n" , p -> PID , p -> UID);
p = p -> next;
}
return 0;
}
/* 出力 */
int output()
{
printf (" 本当にキュー");
outputqueue(really);
printf ("実行キュー: n");
outputqueue(excute);
printf ("待機キュー: n");
outputqueue(wait);
}/*output*/
/* 初期化 */
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*/
/* プロセスを実行する */
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);/* プロセスの実行時間としてスリープ */
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 ("初期化...OKn");
output();
help();
}
while(1)
{
/*3 つのキューが空でない場合、スケジューリングを実行 */
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*/

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。