📄 process.cpp
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <process.h>
#include <conio.h>
typedef struct process
{
int cputime;
int p; //进程现在的优先数
int count; //执行次数
char name[10]; //进程的名字
int nt; //进程执行需要的时间
int state; //进程状态 0:就绪 1:运行 2:完成
struct process *next; //指向下一道进程
}PCB;
int round = 0;
void wait()
{
printf("\n\n按任意键继续\n");
getch();
}
void Print(PCB *head)
{
PCB *p=head->next;
if(!p)
printf("NULL");
else
{
printf("\n输入内容为:\n");
while(p!=NULL)
{
printf("%s %d\n",p->name,p->nt);
p=p->next;
}
}
}
PCB *InsertNode(PCB *head,PCB *p)
{//在非递增的有序表中作插入
PCB *p1,*p2,*p3;
p1=head->next;
p2=p3=head;
while(p1)
{
if(p1->p < p->p)
{
p2->next=p;
p->next=p1;
break;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
if(p1==NULL)
{
p2->next=p;
p->next=p1;
}
return head;
}
PCB *input(int mode)
{
PCB *p1,*p2,*head;
head = (PCB *)malloc(sizeof(PCB));
p1 = (PCB *)malloc(sizeof(PCB));
printf("INPUT NAME AND NEEDTIME:\n");
head->next = NULL;
p2 = head;
if(mode == 2)
{
for(int i = 0; i < 5; i++)
{
scanf("%s %d",&p1->name,&p1->nt);
p1->state = 0; //将初始状态全部置为就绪状态
p1->count = p1->cputime = 0;
p2->next = p1; //p2是p1的前驱
p2 = p1;
p1 = (PCB *)malloc(sizeof(PCB));
}
p2->next = NULL;
}
if(mode == 1)
{
for(int j = 0; j < 5; j++)
{//在初始时将进程按照优先级排好队列,为在就绪队列中挑选优先数大的进入运行队列提供方便
scanf("%s %d",&p1->name,&p1->nt);
p1->state = 0; //将初始状态全部置为就绪状态
p1->cputime = 0;
p1->p = 50 - p1->nt; //初始优先数为50-NEEDTIME
head = InsertNode(head,p1);
p1 = (PCB *)malloc(sizeof(PCB));
}
}
Print(head);
return head;
}
PCB *mynote(PCB *L)
{//将链表中第一个节点放到链表尾部
PCB *p,*q;
if (L->next && L->next->next)
{
q = L->next;
L->next = L->next->next;
p = L->next;
while(p->next)
p = p->next;
p -> next = q;
q->next = NULL;
}
return L;
}
void printstate1(PCB *head)
{
PCB *q;
printf("\nOUTPUT OF PRIORITY:\n");
printf("\tNAME CPUTIME NEEDTIME PRIORTY STATE\n");
q = head->next;
while(q)
{
printf("\t %s %d %d %d %d\n",q->name,q->cputime,q->nt,q->p,q->state);
q = q->next;
}
}
void priority(PCB *head)
{
int pri = 3; //执行一次后要减少的优先数
PCB *p,*q;
p = head->next;
while(p!=NULL)
{
p->state = 1;
p->p = p->p - 3;
(p->nt)--;
(p->cputime)++;
printstate1(head);
if(p->nt == 0)//已完成的进程
{
p->state = 2;
printstate1(head);
if(p == head->next)
{
head->next = p->next;
p = p->next;
}
else
{
q = head->next;
while(q->next != p)
q = q->next;
q->next = p->next;
p = p->next;
}
}
else
{
if(p->next != NULL)
{
if(p->p > p->next->p)
{
p->state = 1;
printstate1(head);
}
else
{
p->state = 0;
printstate1(head);
p = p->next;
}
}
else
{
if(p->p > head->next->p)
{
p->state = 1;
printstate1(head);
}
else
{
p->state = 0;
printstate1(head);
p = head->next;
}
}
}
}
}
void printstate2(PCB *head)
{
PCB *q;
printf("\nOUTPUT OF ROUNDROBIN:\n");
printf("\tNAME CPUTIME NEEDTIME COUNT ROUND STATE\n");
q = head->next;
while(q)
{
printf("\t %s %d %d %d %d %d\n",q->name,q->cputime,q->nt,q->count,round,q->state);
q = q->next;
}
}
void roundrobin(PCB *head)
{
PCB *p;
int tp = 2; //采用时间片轮转时的固定时间片
int ct = 2; //进程每执行一次,cpu时间的增加数
p = head->next;
char n[10];
strcpy(n,p->name);
while(p!=NULL)
{
p->state = 1; //置状态为运行
p->nt = (p->nt) - tp;
if(p->nt<0)//该进程已经完成,无再需要的时间了。可以把还需要的时间置为0
p->nt = 0;
p->cputime = (p->cputime) + ct;
printstate2(head);
if(strcmp(head->next->name,n)==0)
round++;
if(p->nt>0)
{
head = mynote(head);//执行未完,排到队列尾部
p-> state = 0;//重新转为就绪状态
(p->count)++;
p = head->next;
}
else
{//执行完毕删除该节点
p->state = 2;
printstate2(head);
head->next = p->next;
p = p->next;
}
printstate2(head);
}
}
int main()
{
char p[100];
PCB *hp,*hr;
printf("ABOUT STATE: 0 STANDS FOR READY,1 STANDS FOR RUN,2 STANDS FOR FINISH\n");
printf("\nTYPE THE ALGORITHM:(PRIORITY/ROUNDROBIN):\n");
gets(p);
if(strcmp(p,"PRIORITY") == 0)
{
hp = input(1);
priority(hp);
}
else if(strcmp(p,"ROUNDROBIN") == 0)
{
hr = input(2);
roundrobin(hr);
}
wait();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -