Demo entry 6838863

检测系统安全性模块实现

   

Submitted by anonymous on Jun 12, 2019 at 03:52
Language: C. Code size: 1.4 kB.

	//通过用户输入确定各类资源可供的数量,引入变量work
	int work[MAX];
	printf("请输入各资源所能提供的数量:\n");
	for (j = 0; j < b; j++)
	{
		printf("请输入资源%d可供的数量:\n",j);
		scanf("%d", &work[j]);
	}
	//将不占资源的进程号放入顺序表中后,通过request与work进行比较,确定其中一个进程,释放其资源,修改work变量
	int request[MAX][MAX];//通过request数组记录各进程申请的资源数量
	int x[MAX] = { 0 };//用于对进程所申请的小于等于work的资源进行计数,若所申请的所有资源数都小于work,才能对资源分配图进行简化并且放入L表中
	printf("请输入各进程请求的资源数量:\n");
	int finish[a];
	for (i = 0;i < a;i++)
	{
	    finish[i] = 0;	
	}
	int m = 0; 
	for (i = 0; i < a; i++)
	{
		printf("请输入进程%d申请各资源的数量:\n", i);
		for (j = 0; j < b; j++)
		{			
			scanf("%d", &request[i][j]);
		}
	}
	for (i = 0; i < a; i++)
	{
		if(finish[i]==1)
		{
			continue;
		}
		else
		{
			for (j = 0; j < b; j++)
		   {
			if (request[i][j] > work[j])
			{
				break;
			}
		   }
			if (j == b)//若找到request<=work,则释放资源,修改变量work
			{
				for (j = 0; j < b; j++)
				{
					work[j] = work[j] + Allocation[i][j];
					printf("进程%d释放资源%d后,资源%d数量为%d:\n", i, j, j, work[j]);
				}
					finish[i] = 1;
					L1.SeqList[m++] = i;
					printf("进程%d已装入表L中\n", i);
					i = -1;
			}
			else
			{
				printf("进程%d不能装入表中!\n",i);
				continue;
			}
	    }
	    int f;
	    if(m==a)
	    {
	    	printf("该系统未产生死锁!\n");
			printf("该系统的安全序列为:");
			for (j = 0; j < a; j++)
			{
				f = L1.SeqList[j];
				printf("%d",f);
				if(j != a-1){
					printf("->");
				}
			} 
		}
	}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).