#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
struct PCB_type
{ char pid[100] //进程名输入用字符串健壮性比较好
int priority
int cputime
int state
} //缺少“}”
int shumu=0,pid_l
struct PCB_type neicun[20]
struct PCB_type hc[10]
int max=0
int number=0
void create()
void run()
void huanchu()
void kill()
int main()
{
int n,a
n=1
system("color 1d")
while(n==1)
{
system("cls")
printf("\n**********************************************")
printf("\n* 进程演示系统 *")
printf("\n**********************************************")
printf("\n 1.创建新的进程 2.查看运行进程")
printf("\n 3.换出某个进程 4.杀死运行进程") //新增了一个唤醒功能
printf("\n 5.退出系统 ")
printf("\n**********************************************")
printf("\n请选择(1~5):")
scanf("%d",&a)
switch(a)
{ case 1:
create( )
printf("\npress any key to go on~")
getch()
break
case 2 :
run()
printf("\npress any key to go on~")
getch()
break
case 3 :
huanchu()
printf("\npress any key to go on~")
getch()
break
case 4 :
kill()
printf("\npress any key to go on~")
getch()
break
case 5 :
exit(0)
default:
n=1
break
}
}
}
void create()
{
if(shumu>=20)
{
printf("\n内存已满,请先结束或换出进程\n")
}
else
携毁薯 {
shumu++
printf("\n请输入新进程的程序名\n")
scanf("%s",neicun[shumu-1].pid)
printf("\n请输入新进程的优先级(数字)\n")//
scanf("%d",&neicun[shumu-1].priority)
printf("\n请输入新进程的需要的运行时辩者间\n")//
scanf("%d",&neicun[shumu-1].cputime)
printf("\n创建进程时令其状态为就绪\n")
neicun[shumu-1].state=2 //1为等待,2就绪,3为运行
}
printf("\n创建进程成功!\n")
}
void run()
{
if(shumu<=0)//查看//判断是否存在进程
{
printf("当前状态无进程,按任意键继续创建进程\n")
return
}
int max=0
for(int i=0i<shumui++)//
if((neicun[i].state==2&&neicun[i].priority>=neicun[max].priority))
{
max=i //这里判断优先级,优先级高的进程优先执行。
}
if(neicun[max].state==2)
{
neicun[max].state=3 //进程运行,状态为3 余散
system("color 5F")
printf("/*********************当前已有进程%d个*************************/:\n",shumu)
for(int i=0i<shumui++){
printf("进程编号:%d",i+1)
printf("\n/***********正在运行进程程序名:%s*************************/\n",neicun[i].pid)
printf("\n/***********该进程的优先级:%d*****************************/\n",neicun[i].priority)
printf("\n/***********该进程的需要运行时间:%d***********************/\n",neicun[i].cputime)
printf("\n/***********该进程的状态:%d(1为等待,2就绪,3为运行)******/\n\n\n",neicun[i].state) } //这里增加显示当前运行的进程
}
}
/* 换出 */
void huanchu()
{
int k
if(shumu<=0)//判断是否存在进程
{
printf("当前进程数目为0,不能执行该 *** 作\n")
return
}
printf("当前已有进程%d个:\n",shumu)
for(int h=0h<shumuh++)//当前所有的进程
printf("序号:%d\t程序名:%s\t优先级:%d\t运行时间:%d\t状态:%d\t\n"
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state)
printf("请输入要换出程序的序号:")
scanf("%d",&k)
if(neicun[k].state==3)
{
neicun[k].state=1
printf("已被换出,进程名为:%s、状态为:[%d]",neicun[k].pid,neicun[k].state)
}
else
printf("无法换出,进程名为:%s的进程",neicun[k].pid) //换出结果提示
}
void kill()
{
if(shumu<=0)//对存在的进程进行判断
{
printf("当前进程数目为0,不能执行该 *** 作\n")
return
}
int k=0
printf("/******************当前已有进程%d个******************/:\n",shumu)
for(int h=0h<shumuh++)//当前所有的进程
printf("序号:%d\t程序名:%s\t优先级:%d\t运行时间:%d\t状态:%d\t\n"
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state)
printf("请输入要杀死程序的序号:")
scanf("%d",&k)
neicun[k].cputime=0
neicun[k].priority=0
neicun[k].state=0
if(k==(shumu-1))
shumu--
else{
for(int j=k+1j<shumuj++)
{
strcmp(neicun[j-1].pid,neicun[j].pid)
neicun[j-1].priority=neicun[j].priority
neicun[j-1].cputime=neicun[j].cputime
neicun[j-1].state=neicun[j].state
}
shumu--
}
printf("进程名为:%s已被杀死!\n",neicun[k].pid) //显示进程已被杀死
}
#include "stdio.h" #include <stdlib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { char name[10] char state int super int ntime int rtime struct pcb* link }*ready=NULL,*p typedef struct pcb PCB sort() { PCB *first, *second int insert=0 if((ready==NULL)||((p->super)>(ready->super))) { p->link=ready ready=p }else{ first=ready second=first->link while(second!=NULL){ if((p->super)>(second->super)){ p->link=second first->link=p second=NULL insert=1 }else{ first=first->link second=second->link } } if(insert==0) first->link=p } } input() { int i,num //clrscr() printf("\n 请输入进程数悉岁") scanf("%d",&num) for(i=0i<numi++){ printf("\n 进程号No.%d:\n",i) p=getpch(PCB) printf("\n 输入进程名:") scanf("%s",p->name) printf("\n 输入进程优先数:") scanf("%d",&p->super) printf("\n 输入进程运行时间:") scanf("%d",&p->ntime) printf("\n") p->rtime=0p->state='w' p->link=NULL sort() } } int space() { int l=0PCB* pr=ready while(pr!=NULL){ l++ pr=pr->link } return(l) } disp(PCB * pr){ printf("\n qname \t state \t super \t ndtime \t runtime \n") printf("|%s\t",pr->name) printf("|%c\t",pr->state) printf("|%d\t",pr->super) printf("|%d\t",pr->ntime) printf("|%d\t",pr->rtime) printf("\n") } check(){ PCB* pr printf("\n **** 当前正在运行的进程是:%s",p->name) disp(p) pr=ready printf("\n ****当前就绪基罩队列状态为:\n") while(pr!=NULL){ disp(pr) pr=pr->link } } destroy(){ printf("\n 进程 [%s] 已完成.\n",p->name) free(p) } running(){ (p->rtime)++ if(p->rtime==p->ntime) destroy() else{ (p->super)-- p->state='w' sort() } } main(){ int len,h=0 char ch input() len=space() while((len!=0)&&(ready!=NULL)){ ch=getchar() h++ printf("\n The execute number:%d \n",h) p=ready ready=p->link p->link=NULL p->state='R' check() running() printf("\搏陆闹n 按任一键继续......") ch=getchar() } printf("\n\n 进程已经完成.\n") ch=getchar() }给塌羡拿你参考下吧#include<stdio.h>
#include<stdlib.h>
#define READY 1
#define RUN 2
#define BLOCK 3
typedef struct pcbNode
{
int num
struct pcbNode *next
int cputime
int state
}pcb
pcb *head
pcb *run
pcb *CreatPCB(int n)
{
int i
pcb *p,*q
head=(pcb *)malloc(sizeof(pcb))
head->next=NULL
p=head
for(i=1i<=ni++)
{
q=(pcb *)malloc(sizeof(pcb))
q->num=i
q->next=NULL
q->cputime=rand()%100
q->state=READY
p->next=q
p=q
}
return head
}
void DeletePCB()
{
pcb *p
if(head->next!=NULL)
{
p=head->next
head->next=head->next->next
free(p)
}
}
void Display()
{
pcb *p
p=head->next
while(p)
{
printf(" %d",p->num)
p=p->next
}
printf("\n")
}
void control()
{
run=head->next
run->state=RUN
{ while(run->cputime)
run->cputime--
printf("正在执行的进程编号为: %d\n",run->num)
run->state=RUN
run=run->next
DeletePCB()
printf("执行进程后就绪队列中团搭的进程:")
Display()
printf("\n")
}
}
void main(void)
{
int n
int flag=1
printf("请输入要创建的进程的数量:")
scanf("%d",&n)
head=CreatPCB(n)
printf("就绪队列里的进程有:")
Display()
printf("\n")
while(flag)/*由flag的值判断是否继续执行control()函数*/
{
if(head->next)/*判断进程是否完成*/
control()
else flag=0
}
printf("\派孝n")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)