CodeBlocks编译运行通过。
欢迎试用,提交BUG。O(∩_∩)O~
#include
#include
#include
#include
int id = 1;
typedef struct Node {
int pid;
int date;
struct Node *pNext;
} NODE, *PNODE;
int random(int a, int b)
{
int i;
srand((int)time(0));
return rand()%(b-a+1)+a;
}
int insert_list(PNODE pHead, int pos, int val) {
int i = 0;
PNODE p = pHead;
while(p != NULL && i < pos-1) {
p = p->pNext;
++i;
}
if(i > pos-1 || p == NULL)
return 0;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->pid = val;
pNew->pNext = p->pNext;
p->pNext = pNew;
return 1;
}
PNODE create_random_list(int n)
{
int i, len, val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
len = n;
PNODE pTail = pHead;
pTail->pNext = NULL;
for(i=0; ipid = id++;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
int length_list(PNODE pHead) {
int len = 0;
PNODE p = pHead->pNext;
while(p != NULL) {
++len;
p = p->pNext;
}
return len;
}
int delete_list(PNODE pHead, int pos, int *pVal) {
int i = 0;
PNODE p = pHead;
while(p->pNext != NULL && i < pos-1) {
p = p->pNext;
++i;
}
if(i > pos-1 || p->pNext == NULL)
return 0;
PNODE q = p->pNext;
*pVal = q->pid;
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return 1;
}
void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext;
while(p != NULL) {
printf("%d ", p->pid);
p = p->pNext;
}
printf("\n");
return;
}
void info(int a)
{
switch(a){
case 0: printf("Welcome to use Process Show System\n"); break;
case 1: printf("Succeed to Create a Process\n"); break;
case 2: printf("Kill the EXE Process and move a ready process to CPU\n"); break;
case 3: printf("Succeed to move a EXE process to ready process\n"); break;
case 4: printf("Succeed to move a EXE process to Block process\n"); break;
case 5: printf("Succeed to move a block process to ready process\n"); break;
case 6: printf("CPU is busy \n"); break;
case 7: printf("No Ready Process, Please Create Rrocess first!\n"); break;
case 8: printf("No Exe process! \n"); break;
case 9: printf("No Block process! \n"); break;
case 10: printf("Create a Ready Process, and move it to EXE \n"); break;
}
printf("===============================\n");
}
int menu(int i)
{
char ch;
printf("===============================\n");
printf(" 1, Create a Ready Process \n");
printf(" 2, Kill the EXE Process \n");
printf(" 3, Move EXE ---> Ready \n");
printf(" 4, Move EXE ---> Block \n");
printf(" 5, Move Block ---> Ready \n");
printf(" 6, Exit the System \n");
printf("===============================\n");
info(i);
printf(">");
ch = getchar();
return ch - '0';
}
void ShowProcess(PNODE pReady, PNODE pExe, PNODE pBlock)
{
printf("The Ready Process: ");
traverse_list(pReady);
printf("The EXE Process: ");
traverse_list(pExe);
printf("The Block Process: ");
traverse_list(pBlock);
}
int autotest(PNODE pReady, PNODE pExe, PNODE pBlock)
{
int i = 0;
int Val = 0;
if(length_list(pExe) == 0)
if(length_list(pReady)){
delete_list(pReady, length_list(pReady), &Val);
insert_list(pExe, 1, Val);
i = 2;
}
else {
insert_list(pReady, 1, id++);
delete_list(pReady, length_list(pReady), &Val);
insert_list(pExe, 1, Val);
i = 10;
}
else
i = 1;
return i;
}
int main(void)
{
system("title Process Management v1.0 for windows");
int sign = 1; //控制while循环
int Val = 0; //保存进程PID
int n = 0; //保存菜单返回选项
int i = 0; //记录操作状态,反馈
PNODE pReady, pExe, pBlock = NULL;
pReady = create_random_list(random(0, 6));
pExe = create_random_list(random(0, 1));
pBlock = create_random_list(random(0, 6));
autotest(pReady, pExe, pBlock);
ShowProcess(pReady, pExe, pBlock);
n = menu(0);
while(sign){
switch(n) {
case 1: insert_list(pReady, 1, id++);
i = 1;
i = autotest(pReady, pExe, pBlock);
getchar();
break;
case 2: delete_list(pExe, length_list(pExe), &Val);
i = autotest(pReady, pExe, pBlock);
getchar();
break;
case 3:if(length_list(pExe)) {
delete_list(pExe, length_list(pExe), &Val);
insert_list(pReady, 1, Val);
i = 3;
}
else
i = 8;
i = autotest(pReady, pExe, pBlock);
getchar();
break;
case 4: if(length_list(pExe)) {
delete_list(pExe, length_list(pExe), &Val);
insert_list(pBlock, 1, Val);
i = 4;
}
else
i = 8;
i = autotest(pReady, pExe, pBlock);
getchar();
break;
case 5:if(length_list(pBlock)) {
delete_list(pBlock, length_list(pBlock), &Val);
insert_list(pReady, 1, Val);
i = 5;
}
else
i = 9;
getchar();
break;
case 6: sign=0;break;
}
putchar('\n');
system("cls");
ShowProcess(pReady, pExe, pBlock);
n = menu(i);
}
return 0;
}