<span style="font-family:Arial, Helvetica, sans-serif;">今天学操作系统老师留的作业,用C语言写一个进程管理,要求实现进程创建,查看运行的进程,杀死进程问题</span>
<span style="font-family:Arial, Helvetica, sans-serif;">咳咳,我就查了一些资料,参考了一下,就写了这么多</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">#include"stdio.h"</span>
#include"stdlib.h"
#define ready 1
#define run 2
struct pcb
{
char name[10];
int priority; /*进程的优先级*/
int state; /*进程的状态:可以有run、ready、finish(可有可无)*/
int needtime; /*进程需要运行的时间*/
int runtime;
int time; /*进程已运行的时间*/
struct pcb *next; /*指向下一个进程PCB的指针*/
};
typedef struct pcb PCB;
PCB *head=NULL;
/*此函数用于创建进程队列*/
void create(void)
{ PCB *p, *q;
int n,i;
printf("Enter the number of the process:");
scanf("%d",&n); /*输入要创建的进程的数量*/
head=(PCB *)malloc(sizeof(PCB)); /*创建一个表头结点*/
p=head;
for(i=1;i<=n;i++) /*用循环来创建指定个结点*/
{ q=(PCB*)malloc(sizeof(PCB));
p->next=q;
p=q;
printf("\nenter the NO.%d name of process:",i);
scanf("%s",&p->name);
printf("enter the priority of process:");
scanf("%d",&p->priority);
printf("enter the time need:");
scanf("%d",&p->needtime);
p->state=ready;
p->runtime=0;
p->next=NULL;
}
}
/*删除执行完毕的进程*/
void delete(PCB *head,PCB *p)
{PCB *q;
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
free(p);
}
/*找出执行时间最短的进程*/
PCB *getminneedtime(PCB *head)
{PCB *p,*q;
p=head->next;
q=p->next;
while(q)
{if(p->needtime>q->needtime)
p=q;
q=q->next;
}
return(p);
}
/*找出优先级最高的进程*/
PCB *getpriority(PCB *head)
{PCB *p,*q;
p=head->next;
q=p->next;
while(q)
{if (p->priority>q->priority)
p=q;
}
return (p);
}
/*时间片轮转*/
void RR (void)
{ PCB *p,*q,*r;
int time;
printf("input the time:\n ");
scanf("%d",&time);
for(p=head->next;p->next;p=p->next)
{r=p;}
while(head->next)
{p=head->next; /*选出就绪队列的第一个进程*/
p->state=run;
printf("\n***Now the running process is:\n"); /*输出该进程的信息*/
printf("%s\t",p->name);
printf("state:run\t");
printf("needtime: %d\t",p->needtime);
printf("runtime: %d\n",p->needtime);
q=head->next;
if(p->needtime-p->runtime<=p->time) /*时间片内,进程运行结束否*/
{p->runtime=p->needtime;
printf("runtime:%d\n",p->runtime);
}
else
{p->runtime=p->runtime+p->time;
printf("runtime:%d\n",p->runtime);
}
q=p->next;
if(q!=NULL) /*输出就绪队列中的进程信息*/
printf("***Now the ready quenue is:\n");
else printf("***Now the ready quenue is NONE!");
while(q)
{
printf("%s\t",q->name);
printf("state:ready\t");
printf("needtime:%d\t",q->needtime);
printf("runtime:%d\t",q->runtime);
printf("\n");
q=q->next;
}
if(p->runtime==p->needtime)
{delete(head,p);
}
else
{head->next=p->next;
r->next=p;
r=p;
r->state=ready;
r->next=NULL;
}}
}
/*优先权调度算法*/
void HPF (void)
{PCB *p,*q;
int flag=1;
while(head->next)
{ if(flag==1)
{p=getpriority(head); /*调用“找出执行时间最短的进程”函数*/
p->state=run;
} /*将选出的进程的状态改为“run”*/
(p->runtime)++;
(p->priority)--;
printf("\n***Now the running process is:\n"); /*输出该进程的信息*/
printf("%s\t",p->name);
printf("state:run\t");
printf("needtime: %d\t",p->needtime);
printf("runtime: %d\n",p->runtime);
q=head->next;
if(q->next!=NULL) /*输出就绪队列中的进息*/
printf("***Now the ready quenue is:\n");
else printf("***Now the ready quenue is NONE!");
while(q)
{if(q!=p)
{ printf("%s\t",q->name);
printf("state:ready\t");
printf("needtime:%d\t",q->needtime);
printf("runtime:%d\t",q->runtime);
printf("\n");
}
q=q->next;
}
if(p->runtime==p->needtime)
{ delete(head,p);
}
else
{for(q=head->next;q;q=q->next)
if(p->priority>=q->priority)
{flag=0;
}
else { flag=1;
p->state=ready;
}
}
/*将该运行结束的进程删除掉*/
}
}
/*短作业优先*/
void SJF (void)
{PCB *p,*q;
while(head->next)
{ p=getminneedtime(head); /*调用“找出执行时间最短的进程”函数*/
p->state=run; /*将选出的进程的状态改为“run”*/
printf("\n***Now the running process is:\n"); /*输出该进程的信息*/
printf("%s\t",p->name);
printf("state:run\t");
printf("needtime: %d\t",p->needtime);
printf("runtime: %d\n",p->needtime);
q=head->next;
if(q->next!=NULL) /*输出就绪队列中的进程信息*/
printf("***Now the ready quenue is:\n");
else printf("***Now the ready quenue is NONE!");
while(q)
{if(q!=p)
{ printf("%s\t",q->name);
printf("state:ready\t");
printf("needtime:%d\t",q->needtime);
printf("runtime:%d\t",q->runtime);
printf("\n");
}
q=q->next;
}
delete(head,p); /*将该运行结束的进程删除掉*/
}
}
/*输出菜单*/
void printfunc(void)
{printf(" *******************\n");
printf(" * 1-create *\n");
printf(" * 2-SJF *\n");
printf(" * 3-HPF *\n");
printf(" * 4-RR *\n");
printf(" * 0-exit *\n");
printf(" *******************\n");
}
/*主函数*/
main()
{int choice;
printfunc();
do
{printf("please input the number:");
scanf("%d",&choice);
getchar();
switch (choice)
{case 1:
create();
printfunc();
break;
case 2:
if(head==NULL || head->next==NULL)
printf("就绪队列没有进程,请先创建进程\n");
else
{SJF();
printfunc();}
break;
case 3:
if(head==NULL || head->next==NULL)
printf("就绪队列没有进程,请先创建进程\n");
else
{HPF();
printfunc();}
break;
case 4 :
if(head==NULL || head->next==NULL)
printf("就绪队列没有进程,请先创建进程\n");
else
{RR();
printfunc();}
break;
case 0:
exit(0);
}
}
while(1);
}