📄 process.c
字号:
/*
Jianyou Wang
neuwjyou@163.com
http://hi.baidu.com/neuwjyou
2007-4-25
*/
#include <stdio.h>
#include <string.h>
#define ESC 27
#define SPACE 32
#define COMMAND_COUNT 6
#define W_PROCEEE_ARRAY_LENGTH 15
#define PRINTPCB printf("%d\t%s\t%d\t%d\t%s\n",q->pid,q->name,q->cputime,q->needtime,q->status)
char cmd[COMMAND_COUNT][10]={"CREATE","KILL","BLOCK","WAKEUP","SPACE","EXIT"};
int running = -1;
/******************************************/
/*********** queue ***************/
#define QLENGTH 10
struct queue{
int array[QLENGTH];
int b;
int e;
int size;
};
void push(struct queue *q,int pid)
{
if( q->size >= QLENGTH ){
printf("\nError:The space is full !\n");
}
else{
q->array[q->e]=pid;
q->e += QLENGTH+1;
q->e %= QLENGTH;
++(q->size);
}
}
int pop(struct queue *q)
{
if(q->size<=0){
printf("Warning:there is no data !!!\n");
}
else{
int r=q->array[q->b];
q->b += QLENGTH+1;
q->b %= QLENGTH;
--(q->size);
return r;
}
return -1;
}
int front(struct queue *q)
{
if(q->size>0)
return q->array[q->b];
return -1;
}
void delque(struct queue *q,int pid)
{
int b1;
int b=q->b,
e=q->e;
while(b!=e){
if(q->array[b]==pid){
while(b!=e){
b1 = b+1;
b1 %= QLENGTH;
q->array[b] = q->array[b1];
b = b1;
}
q->array[e] = 0;
e += QLENGTH -1;
e %= QLENGTH;
q->e = e;
break;
}
b += QLENGTH+1;
b %= QLENGTH;
}//end_while
}
void showqueue(struct queue* q);
/******************************************/
struct queue readyq, blockedq;
/******************************************/
/*********** process struct ***************/
struct process{
int pid;
char name[10];
char status[10];
int cputime;
int needtime;
}proarr[W_PROCEEE_ARRAY_LENGTH];
void initprocess()
{
int i=0;
for(i=0; i<W_PROCEEE_ARRAY_LENGTH; ++i)
{
proarr[i].pid=-1;
strcpy(proarr[i].name,"");
strcpy(proarr[i].status,"");
proarr[i].cputime = 0;
proarr[i].needtime = 50;
}
}
/******************************************/
/*********** process ***************/
void dispatch();
int create()
{
static int pid=1;
int i=1;
while(proarr[i].pid!=-1 && i< W_PROCEEE_ARRAY_LENGTH)//search the space
++i;
if(i>=W_PROCEEE_ARRAY_LENGTH){
printf("Warning,the space is full!!!\n\n\n");
exit(0);
}
else{
printf("Please input the name:");
scanf("%s",proarr[i].name);
proarr[i].pid=pid;
strcpy(proarr[i].status,"ready");
proarr[i].cputime = 0;
proarr[i].needtime = 15;
push(&readyq,i);
++pid;
printf("Create process %d !\n",pid-1);
}
if(running==-1)
dispatch();
}
void kill(int ipro)
{
if( ipro==running ){//the killed process is running process?
running = -1;
proarr[ipro].pid=-1;
dispatch();
}
else if(strcmp(proarr[ipro].name,"ready")==0){//the killed process is ready?
delque(&readyq,ipro);
}
else if(strcmp(proarr[ipro].name,"blocked")==0){//the killed process is blocked?
delque(&blockedq,ipro);
}
strcpy(proarr[ipro].name,"none");
printf("Kill the process %d !\n",proarr[ipro].pid);
}
void block()
{
int ipro;
if(running != -1){
ipro=running;
running = -1;
push(&blockedq,ipro);
printf("%d is running\n",proarr[ipro].pid);
dispatch();
}
}
void wakeup()
{
int ipro;
if(blockedq.size>0){
ipro=pop(&blockedq);
push(&readyq,ipro);
strcpy(proarr[ipro].status,"ready");
printf("Wakeup process :%d \n",proarr[ipro].pid);
}
}
void dispatch() //dispatch process from ready queue
{
int ipro;
if(running==-1 && readyq.size>0){
ipro=pop(&readyq);
running = ipro;
strcpy(proarr[ipro].status,"running");
printf("Now the running process is:%d\n",proarr[ipro].pid);
}
}
void timeout()//time out
{
int ipro=running;
if(ipro != -1){
proarr[ipro].cputime += 5;
proarr[ipro].needtime -= 5;
if( proarr[ipro].needtime <= 0 ){//process successful
printf("process complate !");
kill(ipro);
}
else{
push(&readyq,ipro);
strcpy(proarr[ipro].status,"ready");
running = -1;//set the out of running
dispatch();//dispatch the running process
}
}
else{
dispatch();
}
}
/*****************************************/
void showqueue(struct queue* qq)
{
struct process* q = NULL;
int b1;
int b=qq->b,
e=qq->e;
while(b!=e){
q = &proarr[qq->array[b]];
PRINTPCB;
b += QLENGTH+1;
b %= QLENGTH;
}//end_while
}
void showinfo()
{
struct process* q = NULL ;
printf("|****************** show the process infomation ****************\n");
printf("PID\tNAME\tUSEDTIME\tNEEDTIME\tSTATUS\n");
//the running process
printf("the running process\n");
if( running>=0 && proarr[running].pid != -1){
q = &proarr[running];
PRINTPCB;
}
else{
printf("No process is running!\n");
}
//the ready process
printf("the front of ready process\n");
if( readyq.size > 0 ){
showqueue(&readyq);
}
else{
printf("there is no process in ready\n");
}
//the blocked process
printf("the blocked process!\n");
if(blockedq.size > 0){
showqueue(&blockedq);
}
else{
printf("there is no process in blocked\n");
}
printf("\n\n\n");
}
/*
bool iscommand(char* com)
{
int i;
for(i=0; i<COMMAND_COUNT; ++i){
if(strcmp(cmd[i],com)==0)
return true;
}
return false;
}
*/
void help()
{
printf("CREATE [NAME]\n\tCreate process,you may choose the name\n");
printf("KILL PID\n\tKill the process\n");
printf("BLOCK\n\tBlock the running process\n");
printf("WAKEUP\n\tWakeup the front of blocked queue\n");
printf("SPACE\n\t\n");
printf("EXIT\n\tQuit!!!\n");
}
/*********** 主函数 **************/
int main()
{
int i=0;
int flag=0;
char ch;
char* com;
char key;
initprocess();
do{
printf("Input command(you may input \"help\" to show the command):\n——>");
key=getche();
switch(key){
case 'b' :
case 'B' :
printf("\nBlocked process...\n");
block();
break;
case 'w' :
case 'W' :
printf("\nWakeup process...\n");
wakeup();
break;
case 'c' :
case 'C' :
printf("\nCreate process\n");
create();
break;
case SPACE :
timeout();
break;
case ESC :
printf("Quit!\n");
exit(0);
default ://时间片到
printf("the command of you input is wrong,you may input \"help\" to show the command\n ");
}
showinfo();//show the process infomation
}while(1);
/*
char key[20];
do{
printf("请选择命令(你可以键入help显示帮助信息):\n——>");
ch=getchar();
while(ch!=10){
if(flag==0){//set the letter to lower
if(ch!=' ')
ch=toupper(ch);
else
flag=1;
}
key[i++]=ch;
ch=getchar();
}
key[i]='\0';
if(strlen(key)==0)
strcmp(key,"SPACE");
com=strtok(key," ");//get the command
if(iscommand(com)){
switch(com[0]){
case 'B' ://BLOCK
printf("\n锁存进程\n");
// printf("\n杩愯
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -