基本信息
源码名称:c++ 实现进程同步的问题
源码大小:1.96KB
文件格式:.rar
开发语言:C/C++
更新时间:2017-12-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int id; //进程标识数
int priority; //进程优先数,优先数越大优先级越高
int cputime; //进程已占用的CPU时间
int alltime; //进程还需占用的CPU时间
int startblock; //进程的阻塞时间
int blocktime; //进程被阻塞的时间
char state[10]; //进程状态
struct node *next; //队列指针
}PCB;
PCB *CreatQueue(int num) //创建一个就绪队列
{
int i; //i为循环计数器
PCB *head, *temp1, *temp2, *temp3; //head为就绪队列的头指针,temp1为创建进程结点的指针,temp2、temp3分别为比较结点的前驱结点和比较结点
for(i=0; i<num; i ) //根据进程的个数创建结点并按从大到小的顺序进行排序
{
temp1=(PCB *)malloc(sizeof(PCB));
printf("输入第%d个进程的(id…state)\n",i);
scanf("%d%d%d%d%d%d%s",&temp1->id,&temp1->priority,&temp1->cputime,&temp1->alltime,&temp1->startblock,&temp1->blocktime,temp1->state);
if(i==0) //如果创建的是第一个结点
{
head=temp1;
head->next=NULL;
continue;
}
if(head->priority < temp1->priority) //如果创建结点中所保存的数比头结点所保存的数要大,则直接把该结点插入到头结点之前
{
temp1->next=head;
head=temp1;
continue;
}
temp2=head; //temp2为比较结点的直接前驱结点
temp3=temp2->next; //temp3为比较的结点
while(temp3!=NULL && temp3->priority>=temp1->priority) //实现查找的功能
{
temp2=temp3;
temp3=temp2->next;
}
temp2->next=temp1;
temp1->next=temp3;
}
return head;
}
PCB *InsertQueue(PCB *head,PCB *run) //在就绪队列中插入一个结点
{
PCB *temp1,*temp2; //temp1和temp2分别为比较结点的前驱和比较结点
if(head==NULL) //如果就绪队列为空
{
head=run;
head->next=NULL;
}
else if(head->priority < run->priority) //如果插入结点中所保存的数比头结点所保存的数要大,则直接把该结点插入到头结点之前
{
run->next=head;
head=run;
}
else
{
temp1=head; //temp1为比较结点的直接前驱结点
temp2=temp1->next; //temp2为比较的结点
while(temp2!=NULL && temp2->priority>=run->priority) //实现查找的功能
{
temp1=temp2;
temp2=temp1->next;
}
temp1->next=run;
run->next=temp2;
}
return head;
}
main()
{
int num; //num为进程的个数
int alltime=0; //用来保存所有进程需要占用的CPU时间
PCB *head; //head为就绪队列的头指针
PCB *run=NULL; //run为执行进程结点的指针
PCB *block=NULL; //block为阻塞进程的结点
PCB *temp;
printf("请输入进程的个数:");
scanf("%d",&num);
head=CreatQueue(num);
getchar();
temp=head;
while(temp!=NULL)
{
alltime =temp->alltime;
temp=temp->next;
}
while(alltime > 0)
{
if(head!=NULL)
{
run=head; //把就绪队列中的第一个进程取出来执行
head=head->next; //就绪队列的头指针指向下一个结点
strcpy(run->state,"run"); //状态改为执行
run->next=NULL;
/*显示状态*/
printf("RUNNING PROG:%d\n",run->id); //显示执行进程
printf("READY_QUEUE:"); //显示就绪进程
temp=head;
while(temp!=NULL)
{
printf("->%d",temp->id);
temp=temp->next;
}
printf("\n");
printf("BLOCK_QUEUE:"); //显示阻塞进程
if(block!=NULL)
{
printf("%d",block->id);
}
printf("\n");
printf("============================================================================\n");
printf("ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE\n");
printf("%d %d %d %d %d %d %s\n",run->id,run->priority,run->cputime,run->alltime,run->startblock,run->blocktime,run->state);
temp=head;
while(temp!=NULL)
{
printf("%d %d %d %d %d %d %s\n",temp->id,temp->priority,temp->cputime,temp->alltime,temp->startblock,temp->blocktime,temp->state);
temp=temp->next;
}
if(block!=NULL)
{
printf("%d %d %d %d %d %d %s",block->id,block->priority,block->cputime,block->alltime,block->startblock,block->blocktime,block->state);
}
printf("\n");
printf("============================================================================\n");
/*显示状态*/
/*改变优先数*/
run->priority-=3; //执行进程的优先数减3
temp=head;
while(temp!=NULL) //就绪进程的优先数加1
{
temp->priority =1;
temp=temp->next;
}
/*改变优先数*/
/*改变执行进程的有关参数*/
run->cputime =1; //执行进程的已占用CPU时间加1
run->alltime-=1; //还需要的CPU时间减1
if(run->alltime!=0)
{
if(run->startblock > 0) //如果该进程会被阻塞
{
run->startblock-=1; //执行完一个时间片后,开始阻塞的时间减1
if(run->startblock==0) //如果阻塞的时间到了
{
block=run; //执行转阻塞
strcpy(block->state,"b"); //状态转阻塞
alltime--;
printf("\n");
continue;
}
}
strcpy(run->state,"r"); //状态转就绪
head=InsertQueue(head,run); //执行转就绪
run=NULL;
}
/*改变执行进程的有关参数*/
alltime--;
}
else
{
/*显示状态*/
printf("RUNNING PROG:\n"); //显示执行进程
printf("READY_QUEUE:\n"); //显示就绪进程
printf("BLOCK_QUEUE:"); //显示阻塞进程
if(block!=NULL)
{
printf("%d",block->id);
}
printf("\n");
printf("============================================================================\n");
printf("ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE\n");
if(block!=NULL)
{
printf("%d %d %d %d %d %d %s",block->id,block->priority,block->cputime,block->alltime,block->startblock,block->blocktime,block->state);
}
printf("\n");
printf("============================================================================\n");
/*显示状态*/
}
/*改变阻塞进程的有关参数*/
if(block!=NULL) //如果有阻塞进程
{
block->blocktime-=1; //被阻塞的时间减1
if(block->blocktime==0) //如果被阻塞的时间到了
{
strcpy(block->state,"r"); //状态转就绪
head=InsertQueue(head,block); //阻塞转就绪
block=NULL;
}
}
/*改变阻塞进程的有关参数*/
getchar();
}
}