#include <stdio.h>
#include <stdlib.h>
struct Jincheng{
int pid;
int priority;
int size;
int state;
char info[20];
struct Jincheng *next;
};
int index = 0;
Jincheng *yunxing,*guaqi;
void printfPid(Jincheng *list){
Jincheng *p = list->next;
printf("(");
while(p != NULL){
printf("%d ",p->pid);
p=p->next;
}
printf(")\n");
}
Jincheng *getElem(Jincheng *list,int pid){
Jincheng *p = list;
while (p->next != NULL) {
if (p->next->pid == pid) {
return p;
}
p = p->next;
}
return NULL;
}
int existInYunxing(Jincheng *yunxing,int pid){
int result = 0;
Jincheng *s = yunxing->next;
while (s != NULL) {
if (s->pid == pid) {
result = 1;
break;
}
s = s->next;
}
return result;
}
int existInGuaqi(Jincheng *guaqi,int pid){
int result = 0;
Jincheng *s = guaqi->next;
while (s != NULL) {
if (s->pid == pid) {
result = 1;
break;
}
s = s->next;
}
return result;
}
void create(Jincheng *yunxing){
if(index >= 20){
printf("内存已满,需挂起或杀死进程");
return;
}else{
Jincheng *p = (Jincheng*)malloc(sizeof(Jincheng));
printf("请输入进程的ID\n");
scanf("%d",&p->pid);
if(existInYunxing(yunxing,p->pid)){
printf("已存在该pid");
return;
}else{
printf("请输入进程的优先级\n");
scanf("%d",&p->priority);
printf("请输入进程的大小\n");
scanf("%d",&p->size);
printf("请输入进程的内容\n");
scanf("%s",&p->info);
p->state = 1;
p->next=NULL;
Jincheng *q = yunxing;
while(q->next != NULL){
q = q->next;
}
q->next = p;
index = index + 1;
}
}
}
void run(Jincheng *yunxing){
Jincheng *q = yunxing->next;
if(q == NULL){
printf("没有正在运行的进程");
return;
}
while(q!=NULL){
printf("pid:%d ",q->pid);
printf("priority:%d ",q->priority);
printf("size:%d ",q->size);
printf("state:%d ",q->state);
printf("info:%s\n",q->info);
q = q->next;
}
}
void huanchu(Jincheng *yunxing,Jincheng *guaqi){
Jincheng *q = yunxing->next;
Jincheng *p = guaqi;
if (index == 0) {
printf("无可换出的进程\n");
return;
}
printf("可以换出的进程有: ");
printfPid(yunxing);
int h;
printf("请输入需要换出进程的id: ");
scanf("%d",&h);
if (existInYunxing(yunxing, h)) {
Jincheng *s = getElem(yunxing,h);
s->next->state = 2;
while (p->next != NULL) {
p = p->next;
}
p->next = s->next;
s->next = s->next->next;
p->next->next = NULL;
index = index - 1;
printf("成功换出\n");
}else{
printf("该线程不存在或已处于挂起状态\n");
}
}
void kill(Jincheng *yunxing){
int pid;
if (index == 0) {
printf("没有运行的进程可供杀死\n");
return;
}
printf("可以杀死的进程有: ");
printfPid(yunxing);
printf("请输入你想杀死进程id: ");
scanf("%d",&pid);
if(existInYunxing(yunxing,pid)){
Jincheng *p = getElem(yunxing,pid);
Jincheng *q = p->next;
p->next = p->next->next;
index = index - 1;
free(q);
printf("进程已经杀死");
}else{
printf("该进程不存在或者已经挂起");
}
}
void jihuo(Jincheng *yunxing,Jincheng *guaqi){
int pid;
Jincheng *p = yunxing;
Jincheng *g = guaqi;
if(g->next == NULL){
printf("没有进程可以激活");
}else{
printf("可以激活进程有: ");
printfPid(guaqi);
printf("请输入你想激活的进程: ");
scanf("%d",&pid);
if(existInGuaqi(guaqi,pid)){
Jincheng *s = getElem(guaqi,pid);
s->next->state = 1;
while (p->next != NULL) {
p = p->next;
}
p->next = s->next;
s->next = s->next->next;
p->next->next = NULL;
index = index + 1;
printf("进程已经激活");
}else{
printf("没有进程可激活");
}
}
}
int main(){
yunxing = (Jincheng*)malloc(sizeof(Jincheng));
yunxing->next = NULL;
guaqi = (Jincheng*)malloc(sizeof(Jincheng));
guaqi->next = NULL;
int n = 1;
int num;
while(n){
printf("\n****************************************");
printf("\n* 进程管理系统 *");
printf("\n****************************************");
printf("\n 1.创建新的进程 2.查看运行进程 ");
printf("\n 3.换出某个进程 4.杀死运行进程 ");
printf("\n 5.激活某个进程 6.退出系统 ");
printf("\n****************************************");
printf("\n 请选择(1-6) \n");
scanf("%d",&num);
switch(num){
case 1: create(yunxing);break;
case 2: run(yunxing);break;
case 3: huanchu(yunxing,guaqi);break;
case 4: kill(yunxing);break;
case 5: jihuo(yunxing,guaqi);break;
case 6: exit(0);
default:n=0;
}
}
return 0;
}
|