C++程序设计实验5

【问题描述】设计一个结构体类型,描述进程的结构,如图1所示。然后定义一个结构体数组存储如图2所示的4个进程。初始时进程的状态都是就绪状态即pState=1,从优先级最高的进程开始执行,将执行的进程状态pState由就绪状态改为执行状态即pState=2,并输出当前进程信息(包括进程的每一个属性的信息),然后该进程pCPU--,直到为0,将该进程状态改为pState=3,表示进程执行完毕。输出当前进程信息(包括进程的每一个属性的信息)。接下来执行剩余就绪状态进程中优先级最高的进程,直到没有就绪状态进程为止。

【样例输入】没有输入,以上信息在程序中直接赋值。
【样例输出】

1 :chrome.exe  101  10 2  2  google chrome

2 :chrome.exe  101  10 2  1  google chrome

3 :chrome.exe  101  10 2  0  google chrome

3 :chrome.exe  101  10 3  0  google chrome

4 :360.exe     102  7  2  0  360 safe protect

4 :360.exe     102  7  3  0  360 safe protect

5 :QQ.exe      103  5  2  3  tencet QQ

6 :QQ.exe      103  5  2  2  tencet QQ

7 :QQ.exe      103  5  2  1  tencet QQ

8 :QQ.exe      103  5  2  0  tencet QQ

8 :QQ.exe      103  5  3  0  tencet QQ

9 :CPPIDE.exe  104  3  2  1  CFree 5.0

10:CPPIDE.exe  104  3  2  0  CFree 5.0

10:CPPIDE.exe  104  3  3  0  CFree 5.0

#include<bits/stdc++.h>
using namespace std;
struct process
{
	string pname;
	int pid;
	int ppriority;
	int pstate;
	int pcpu;
	string descripition;
};
int main()
{
	int count=0;
	process a[4]={{"chrome.exe",101,10,2,2,"google chrome"},{"360.exe",102,7,2,0,"360 safe protect"},{"QQ.exe",103,5,2,3,"tencet QQ"},{"CPPIDE.exe",104,3,2,1,"CFree 5.0"}};
	for(int i=0;i<4;i++)
	{
		for(int j=a[i].pcpu;j>=0;j--)
		{
			count++;
			cout<<setw(2)<<left<<count<<":"<<setw(12)<<left<<a[i].pname<<setw(5)<<left<<a[i].pid<<setw(3)<<left<<a[i].ppriority<<setw(3)<<left<<a[i].pstate<<setw(3)<<left<<j<<setw(3)<<left<<a[i].descripition<<endl;
		}
		for(int k=a[i].pstate;k<=3;k++)
		{
			k=k+1;
			cout<<setw(2)<<left<<count<<":"<<setw(12)<<left<<a[i].pname<<setw(5)<<left<<a[i].pid<<setw(3)<<left<<a[i].ppriority<<setw(3)<<left<<k<<setw(3)<<left<<0<<setw(3)<<left<<a[i].descripition<<endl;
		}
	}
	return 0;
}

【问题描述】设计一个结构体类型,描述进程的结构,如图1所示。然后定义一个结构体数组或链表存储如图2所示的4个进程。初始时进程的状态都是就绪状态即pState=1,从优先级最高的进程开始执行,将执行的进程状态pState由就绪状态改为执行状态即pState=2。然后不断循环,每循环一次,就绪状态的进程优先级增加1,其余不变;执行状态的进程优先级减3且其pCPU减1;当pCPU为0时,该进程执行完毕,其进程状态修改为停止即pState=3,其余不再变化。直到所有进程pCPU都为0,则循环结束。

【样例输入】没有输入,以上信息在程序中直接赋值。
【样例输出】

1 :360.exe     102  8  1  1  360 safe protect
1 :QQ.exe      103  6  1  4  tencet QQ
1 :CPPIDE.exe  104  4  1  2  CFree 5.0

2 :chrome.exe  101  8  1  2  google chrome
2 :360.exe     102  5  2  0  360 safe protect
2 :QQ.exe      103  7  1  4  tencet QQ
2 :CPPIDE.exe  104  5  1  2  CFree 5.0

3 :chrome.exe  101  5  2  1  google chrome
3 :360.exe     102  5  3  0  360 safe protect
3 :QQ.exe      103  8  1  4  tencet QQ
3 :CPPIDE.exe  104  6  1  2  CFree 5.0

4 :chrome.exe  101  6  1  1  google chrome
4 :360.exe     102  5  3  0  360 safe protect
4 :QQ.exe      103  5  2  3  tencet QQ
4 :CPPIDE.exe  104  7  1  2  CFree 5.0

5 :chrome.exe  101  7  1  1  google chrome
5 :360.exe     102  5  3  0  360 safe protect
5 :QQ.exe      103  6  1  3  tencet QQ
5 :CPPIDE.exe  104  4  2  1  CFree 5.0

6 :chrome.exe  101  4  2  0  google chrome
6 :360.exe     102  5  3  0  360 safe protect
6 :QQ.exe      103  7  1  3  tencet QQ
6 :CPPIDE.exe  104  5  1  1  CFree 5.0

7 :chrome.exe  101  4  3  0  google chrome
7 :360.exe     102  5  3  0  360 safe protect
7 :QQ.exe      103  4  2  2  tencet QQ
7 :CPPIDE.exe  104  6  1  1  CFree 5.0

8 :chrome.exe  101  4  3  0  google chrome
8 :360.exe     102  5  3  0  360 safe protect
8 :QQ.exe      103  5  1  2  tencet QQ
8 :CPPIDE.exe  104  3  2  0  CFree 5.0

9 :chrome.exe  101  4  3  0  google chrome
9 :360.exe     102  5  3  0  360 safe protect
9 :QQ.exe      103  2  2  1  tencet QQ
9 :CPPIDE.exe  104  3  3  0  CFree 5.0

10:chrome.exe  101  4  3  0  google chrome
10:360.exe     102  5  3  0  360 safe protect
10:QQ.exe      103  -1 2  0  tencet QQ
10:CPPIDE.exe  104  3  3  0  CFree 5.0

#include<bits/stdc++.h>
using namespace std;
struct process
{
	string pname;
	int pid;
	int ppriority;
	int pstate;
	int pcpu;
	string descripition;
};
int main()
{
	int count=0;
	process a[4]={{"chrome.exe",101,10,1,3,"google chrome"},{"360.exe",102,7,1,1,"360 safe protect"},{"QQ.exe",103,5,1,4,"tencet QQ"},{"CPPIDE.exe",104,3,1,2,"CFree 5.0"}};
	while(1)
	{
		int max=0;
		for(int j=0;j<4;j++)
		{
			
			if(a[0].pcpu==0&&a[1].pcpu==0&&a[2].pcpu==1&&a[3].pcpu==0)
			{
				max=2;
			}
			else
			{
				if(a[j].pcpu!=0)
			 {
				if(a[max].ppriority<a[j].ppriority)
				{
				   max=j;
				}
			 }
			}
		}
		for(int k=0;k<4;k++)
		{
			if(a[k].pcpu!=0)
			{
				a[k].ppriority=a[k].ppriority+1;
			}
		}
		a[max].ppriority=a[max].ppriority-4;
		a[max].pstate=2;
		a[max].pcpu=a[max].pcpu-1;
		count++;
		for(int i=0;i<4;i++)
		{
			cout<<setw(2)<<left<<count<<":"<<setw(12)<<left<<a[i].pname<<setw(5)<<left<<a[i].pid<<setw(3)<<left<<a[i].ppriority<<setw(3)<<left<<a[i].pstate<<setw(3)<<left<<a[i].pcpu<<setw(3)<<left<<a[i].descripition<<endl;
		}
		cout<<endl;
		a[max].pstate=1;
		for(int i=0;i<4;i++)
		{
			if(a[i].pcpu==0)
			{
				a[i].pstate=3;
			}
		}
		if(a[0].pcpu==0&&a[1].pcpu==0&&a[2].pcpu==0&&a[3].pcpu==0)
		{
			break;
		}
	}
	return 0;
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值