📄 processmanager.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define GETPCB(type) (type*)malloc(sizeof(type));
struct pcb
{
char name[10];//process name
char state; //process state:
int super; //process superior
int ntime; //arrive time
int rtime; // run time
struct pcb* link;//pointer to next process
}*ready = NULL, *p;
typedef struct pcb PCB;
void sort()
{
PCB *first, *second;
int insert = 0;
if( ready == NULL || p->super > ready->super )
{
p->link = ready;
ready = p;
}
else
{
first = ready;
second = first->link;
while( second != NULL )
{
if( p->super > second->super )
{
p->link = second;
first->link = p;
second = NULL;
insert = 1;
}
else
{
first = first->link;
second = second->link;
}
}
if( insert == 0 )
first->link = p;
}
}
int input()
{
int i, num;
//clrscr();
printf( "\n 请输入想运行的进程数目:" );
scanf( "%d", &num );
for( i = 0; i < num; i++ )
{
printf( "\n 进程号 No.%d:\n", i );
p = GETPCB( PCB );
printf( "\n 输入进程名: " );
scanf( "%s", p->name );
printf( "\n 输入进程优先数: " );
scanf( "%d", &p->super );
printf( "\n 输入进程所要运行的时间: " );
scanf( "%d", &p->ntime );
printf( "\n" );
p->rtime = 0;
p->state = 'W';
p->link = NULL;
sort();
}
return num;
}
void disp( PCB * pr )//进程显示函数
{
printf( "\n qname state super ndtime runtime \n" );
printf( "|%s\t", pr->name );
printf( "|%c\t", pr->state );
printf( "|%d\t", pr->super );
printf( "|%d\t", pr->ntime );
printf( "|%d\t", pr->rtime );
}
void check()//进程查看函数
{
PCB* pr;
printf( "\n **** 正在运行的程序是: %s", p->name );
disp( p );
printf( "\n **** 正在等待的程序是: " );
pr = ready;
while( pr != NULL )
{
disp( pr );
pr = pr->link;
}
}
void destroy()
{
printf( "\n process[%s] finish\n", p->name );
free( p );
}
void running()
{
p->rtime++;
if( p->rtime == p->ntime )
destroy();
else
{
p->super--;
p->state = 'W';
sort();
}
}
int main()
{
int num = 0;
char ch;
int processLength = input();
while( processLength != 0 && ready != NULL )
{
num++;
printf( "\n 第 %d 次运行\n", num );
p = ready;
ready = p->link;
p->link = NULL;
p->state = 'R';
check();
running();
printf( "\n press any to continue..." );
getch();
}
printf( "\n\n processes done" );
ch = getchar();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -