c语言 获取可用内存大小,C语言实现获取内存信息并输出的实例

C语言实现获取内存信息并输出的实例

实现实例代码:

headfile.h

#include

#include

#include

#define TRUE 1

#define FALSE 0

#define MAX 10000

typedef int KeyType;

typedef int OtherType;

typedef struct

{

KeyType key;

OtherType other_data;

}RecordType;

seek.cpp

#include "stdafx.h"

#include "headfile.h"

#include "windows.h"

#include "conio.h "

#include"WinBase.h"

#include "Psapi.h"

#pragma once

#pragma message("Psapi.h --> Linking with Psapi.lib")

#pragma comment(lib,"Psapi.lib")

int Data[MAX]={0};

void produceData(int a[],int length) //给数组生成数据,用于随即查找

{

time_t t;

srand(time(&t));

for (int i=0;i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的银行家算法的实现示例,使用 C 语言编写: ```c #include <stdio.h> #include <stdbool.h> #define NUM_RESOURCES 3 #define NUM_PROCESSES 5 int available[NUM_RESOURCES] = {3, 3, 2}; int max_need[NUM_PROCESSES][NUM_RESOURCES] = { {7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}, }; int allocated[NUM_PROCESSES][NUM_RESOURCES] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}, }; // 计算已分配资源和需要资源 void calculate_need(int need[NUM_PROCESSES][NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) { for (int i = 0; i < NUM_PROCESSES; i++) { for (int j = 0; j < NUM_RESOURCES; j++) { need[i][j] = max_need[i][j] - allocated[i][j]; } } } // 判断一个进程是否需要的资源是否小于等于可用资源 bool check_process(int process, int need[NUM_PROCESSES][NUM_RESOURCES], int available[NUM_RESOURCES]) { for (int i = 0; i < NUM_RESOURCES; i++) { if (need[process][i] > available[i]) { return false; } } return true; } // 判断当前状态是否安全 bool check_safe(int available[NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) { int need[NUM_PROCESSES][NUM_RESOURCES]; calculate_need(need, allocated, max_need); bool finish[NUM_PROCESSES] = {false}; int work[NUM_RESOURCES]; for (int i = 0; i < NUM_RESOURCES; i++) { work[i] = available[i]; } int safe_seq[NUM_PROCESSES]; int count = 0; while (count < NUM_PROCESSES) { bool found = false; for (int i = 0; i < NUM_PROCESSES; i++) { if (!finish[i] && check_process(i, need, work)) { finish[i] = true; for (int j = 0; j < NUM_RESOURCES; j++) { work[j] += allocated[i][j]; } safe_seq[count] = i; count++; found = true; } } if (!found) { return false; } } return true; } // 请求资源 bool request_resources(int process, int request[NUM_RESOURCES], int available[NUM_RESOURCES], int allocated[NUM_PROCESSES][NUM_RESOURCES], int max_need[NUM_PROCESSES][NUM_RESOURCES]) { int need[NUM_PROCESSES][NUM_RESOURCES]; calculate_need(need, allocated, max_need); for (int i = 0; i < NUM_RESOURCES; i++) { if (request[i] > need[process][i]) { return false; } if (request[i] > available[i]) { return false; } } for (int i = 0; i < NUM_RESOURCES; i++) { available[i] -= request[i]; allocated[process][i] += request[i]; need[process][i] -= request[i]; } if (!check_safe(available, allocated, max_need)) { for (int i = 0; i < NUM_RESOURCES; i++) { available[i] += request[i]; allocated[process][i] -= request[i]; need[process][i] += request[i]; } return false; } return true; } // 打印状态 void print_status() { printf("可用资源:"); for (int i = 0; i < NUM_RESOURCES; i++) { printf(" %d", available[i]); } printf("\n最大需求:\n"); for (int i = 0; i < NUM_PROCESSES; i++) { printf("进程 %d:", i); for (int j = 0; j < NUM_RESOURCES; j++) { printf(" %d", max_need[i][j]); } printf("\n"); } printf("已分配资源:\n"); for (int i = 0; i < NUM_PROCESSES; i++) { printf("进程 %d:", i); for (int j = 0; j < NUM_RESOURCES; j++) { printf(" %d", allocated[i][j]); } printf("\n"); } } // 测试代码 int main() { print_status(); if (check_safe(available, allocated, max_need)) { printf("当前状态安全\n"); } else { printf("当前状态不安全\n"); } int request[NUM_RESOURCES] = {1, 0, 2}; if (request_resources(1, request, available, allocated, max_need)) { printf("请求资源成功\n"); print_status(); } else { printf("请求资源失败\n"); } return 0; } ``` 运行上述代码,将输出以下结果: ``` 可用资源: 3 3 2 最大需求: 进程 0: 7 5 3 进程 1: 3 2 2 进程 2: 9 0 2 进程 3: 2 2 2 进程 4: 4 3 3 已分配资源: 进程 0: 0 1 0 进程 1: 2 0 0 进程 2: 3 0 2 进程 3: 2 1 1 进程 4: 0 0 2 当前状态安全 请求资源成功 可用资源: 2 3 0 最大需求: 进程 0: 7 5 3 进程 1: 3 2 2 进程 2: 9 0 2 进程 3: 2 2 2 进程 4: 4 3 3 已分配资源: 进程 0: 0 1 0 进程 1: 3 0 2 进程 2: 3 0 2 进程 3: 2 1 1 进程 4: 0 0 2 ``` 可以看到,初始状态下当前状态是安全的,请求资源成功后仍然是安全状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值