📄 process.c
字号:
/*
Jianyou Wang
neuwjyou@163.com
http://hi.baidu.com/neuwjyou
2007-4-25
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W_PROCEEE_ARRAY_LENGTH 15
#define PRINTPCB printf("%d\t%s\t%s\n",q->pid,q->name,q->state)
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 showqueue(struct queue* q);
/******************************************/
struct queue readyq, blockedq;
/******************************************/
/*********** process struct ***************/
void dispatch();
struct process{
int pid;
char name[10];
char state[10];
}proarr[W_PROCEEE_ARRAY_LENGTH];
void initprocess()
{
int i;
int process_num=4;
char name[3]={'P','0','\0'};
for(i=0; i<process_num; ++i)
{
proarr[i].pid=i+1;
++name[1];
strcpy(proarr[i].name,name);
strcpy(proarr[i].state,"ready");
push(&readyq,i);
}
dispatch();
}
/******************************************/
/*********** process ***************/
void dispatch() //dispatch process from ready queue
{
if(running==-1 && readyq.size>0){
running = pop(&readyq);
strcpy(proarr[running].state,"running");
printf("Now the running process is:%d\n",proarr[running].pid);
}
}
void timeout()//time out
{
if(running != -1){
push(&readyq,running);
strcpy(proarr[running].state,"ready");
running = -1;//set the out of running
dispatch();//dispatch the running process
}
else{
dispatch();
}
}
void block()
{
int ipro;
if(running != -1){
ipro=running;
running = -1;
push(&blockedq,ipro);
strcpy(proarr[ipro].state,"blocked");
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].state,"ready");
printf("Wakeup process :%d \n",proarr[ipro].pid);
}
}
/*****************************************/
void showqueue(struct queue* qq)
{
struct process* q = NULL;
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\tSTATE\n");
//the running process
printf("the running process\n");
if( running>=0 ){
q = &proarr[running];
PRINTPCB;
}
else{
printf("\tNo process is running!\n");
}
//the ready process
printf("the ready process\n");
if( readyq.size > 0 ){
showqueue(&readyq);
}
else{
printf("\tNo process in ready\n");
}
//the blocked process
printf("the blocked process!\n");
if(blockedq.size > 0){
showqueue(&blockedq);
}
else{
printf("\tNo process in blocked\n");
}
printf("\n\n\n");
}
/***********Main Soource**************/
int main()
{
char key;
initprocess();
do{
showinfo();//show the process infomation
printf("b/B——block; w/W——wakeup; q/Q——quit; orthers——timeout\n");
printf("Input commandyou may input\n>");
key=getchar(); getchar();
switch(key){
case 'b' :
case 'B' :
printf("Blocked process...\n");
block();
break;
case 'w' :
case 'W' :
printf("Wakeup process...\n");
wakeup();
break;
case 'q' :
case 'Q' :
printf("Quit\n\n\n");
break;
default :
timeout();
break;
}//end_switch
}while(key!='Q' && key!='q');
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -