📄 progress.cpp
字号:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
int pi,ui;
typedef struct Lnode
{
int pid;
int uid;
struct Lnode *next;
}Lnode,*LinkList;
typedef struct
{
LinkList front;
LinkList rear;
}queuenode,*LinkQueue;
int InitQueue(LinkQueue Q)
{
Q->front=(LinkList)malloc(sizeof(Lnode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return 1;
}
else
return 0;
}
int EnterQueue(LinkQueue &Q)
{
LinkList p;
p=(LinkList)malloc(sizeof(Lnode));
if(p!=NULL)
{
p->pid=pi;
p->uid=ui;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
else return 0;
}
int DeleteQueue(LinkQueue &Q)
{
LinkList p;
if(Q->front==Q->rear)return 0;
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)Q->rear=Q->front;
pi=p->pid;
ui=p->uid;
free(p);
return 1;
}
void Fork(LinkQueue &Q)/*建立进程队列*/
{
LinkList temp,s,r;
char t;
int p,u;
temp=(LinkList)malloc(sizeof(Lnode));
(Q)->front=temp;
r=temp;
do
{
printf("input the pid,uid\n");
scanf("%d,%d",&p,&u);
getchar();
s=(LinkList)malloc(sizeof(Lnode));
s->pid=p;
s->uid=u;
r->next=s;
r=s;
printf("Fork a progress programing,y/n\n");
t=getchar();
}while(t=='y');
r->next=NULL;
(Q)->rear=r;
}
void Kill(LinkQueue &wait)/*按给定的进程的标实浮从等待队列中撤消一个进程*/
{
LinkList p,t;
int k;
p=wait->front;
printf("input the pid which you want to kill\n");
scanf("%d",&k);
while((p)!=NULL)
{
if(p->next!=NULL)/*删除非队尾元素*/
{
if(k==p->next->pid){t=p->next;p->next=p->next->next;free(t);printf("Kill Sucess\n");return;}
}
else
{
if(k==p->pid){free(p);printf("Kill Sucess\n");return;}/*删除队尾元素*/
}
p=p->next;
}
if(p==NULL)printf("there is no the %d pid",k);
}
void Block(LinkQueue &excute,LinkQueue &wait,LinkQueue &ready)/*封锁功能:把当前执行进程,连接到等待队列中,并从就绪队列中选择一个进程,防在执行队列中*/
{
DeleteQueue(excute);
EnterQueue(wait);
DeleteQueue(ready);
EnterQueue(excute);
}
void Wake(LinkQueue &wait,LinkQueue &ready)/*唤醒功能:从等待队列中把一个给定进程连接到就绪队列中*/
{
if(!DeleteQueue(wait)){printf("空队列\n");return ;}
EnterQueue(ready);
}
void View(LinkQueue t)
{
LinkList p;
p=t->front->next;
while(p!=NULL)
{
printf("pid:%d,uid:%d\n",p->pid,p->uid);
//printf("%d",p->next);
p=p->next;
}
}
int main()
{
LinkQueue excute,ready,wait;
int selection;
excute=(LinkQueue)malloc(sizeof(queuenode));
ready=(LinkQueue)malloc(sizeof(queuenode));
wait=(LinkQueue)malloc(sizeof(queuenode));
printf("Fork excute\n");
Fork(excute);
printf("Fork ready\n");
Fork(ready);
printf("Fork wait\n");
Fork(wait);
do
{
printf("\nplease select :\n");
printf("1.Kill,按给定的进程的标实浮从等待队列中撤消一个进程\n");
printf("2.Block,锁功能:把当前执行进程,连接到等待队列中,并从就绪队列中选择一个进程,放在在执行队列中\n");
printf("3.Wake,唤醒功能:从等待队列中把一个给定进程连接到就绪队列中\n");
printf("4.View\n");
printf("5.exit\n");
scanf("%d",&selection);
switch(selection)
{
case 1:printf("1.Kill\n");Kill(wait);break;
case 2:printf("2.Block\n");Block(excute,wait,ready);break;
case 3:printf("3.Wake\n");Wake(wait,ready);break;
case 4:printf("4.View\n");
printf("\nexcute:\n");
View(excute);
printf("\nready:\n");
View(ready);
printf("\nwait:\n");
View(wait);
break;
case 5:exit(1);break;
default:{printf("input error\n");continue ;}
}
}while(selection<5&&selection>0);
getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -