在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例常规C/C++编程 → c++ 实现进程同步的问题

c++ 实现进程同步的问题

常规C/C++编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:1.96KB
  • 下载次数:26
  • 浏览次数:643
  • 发布时间:2017-12-04
  • 实例类别:常规C/C++编程
  • 发 布 人:我又不乱来ii
  • 文件格式:.rar
  • 所需积分: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();
	}
}

标签: 进程 同步

实例下载地址

c++ 实现进程同步的问题

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警