1.page repalcement algorithm
#include <stdio.h>
#include <stdlib.h>
#define maxpagenum 100
typedef struct page
{
int pagenum;
struct page *next;
}Page;
int FIFO(Page *block,int pages[maxpagenum],int pagenum,int blocknum)
{
int i = 0,j = 0;
int time = 0;
Page *old = block,*check = NULL;
while(i<pagenum){
check = block;
j = 0;
while(j < blocknum){
if(pages[i]==check->pagenum)// 命中
break;
check = check->next;
j++;
}
if(j == blocknum){//没有命中,替换
old->pagenum = pages[i];
old = old->next;
time += 1; //缺页次数+1
}
i++;
}
return time;
}
Page* creatblock(int blocknum)
{
int i;
Page *head = NULL,*cur = NULL,*tail = NULL;
for(i = 0;i < blocknum;i++){
if(!head){
cur = (Page*)malloc(sizeof(Page));
cur->pagenum = -1;
cur->next = NULL;
head = tail = cur;
}
else{
cur = (Page*)malloc(sizeof(Page));
cur->pagenum = -1;
tail->next = cur;
tail = cur;
}
}
tail->next = head;
return head;
}
int main()
{
int time;
int pages[maxpagenum];
int i,blocknum,pagenum;
Page *head=NULL;
printf("请输入块号:\n");
scanf("%d",&blocknum);
head = creatblock(blocknum);
printf("请输入待访问的页面数量:\n");
scanf("%d",&pagenum);
printf("请输入待访问的页面号:\n");
for(i=0;i<pagenum;i++){
scanf("%d",&pages[i]);
}
time = FIFO(head,pages,pagenum,blocknum);
printf("缺页次数:%d,中断率:%.2f%%\n",time,time*1.0/pagenum*100);
return 0;
}
2.LRU replacemnet page algorithm
#include <iostream>
#include <list>
#include <unordered_map>
class LRUCache {
private:
std::list<int> cache; // Doubly linked list to store the page numbers
std::unordered_map<int, std::list<int>::iterator> map; // Map to store the page number and its position in the list
int capacity; // Capacity of the cache
public:
LRUCache(int size) {
capacity = size;
}
void refer(int page) {
if (map.find(page) == map.end()) { // Page not present in the cache
if (cache.size() == capacity) {
int last = cache.back(); // Get the least recently used page
cache.pop_back(); // Remove it from the cache
map.erase(last); // Erase it from the map
}
}
else {
cache.erase(map[page]); // Erase the page from its current position in the list
}
cache.push_front(page); // Add the page to the front of the list
map[page] = cache.begin(); // Update its position in the map
}
void display() {
for (auto it = cache.begin(); it != cache.end(); ++it) {
std::cout << (*it) << " ";
}
std::cout << std::endl;
}
};
int main() {
LRUCache cache(3); // Create a cache with capacity 3
cache.refer(1); // Page 1 is referenced
cache.refer(2); // Page 2 is referenced
cache.refer(3); // Page 3 is referenced
cache.display(); // Display the current state of the cache: 3 2 1
cache.refer(4); // Page 4 is referenced, causing 1 to be replaced
cache.display(); // Display the current state of the cache: 4 3 2
return 0;
}
3.appromatix LRU replacement algorithm
#include <stdio.h>
#include <stdlib.h>
// 定义双向链表节点结构
struct Node {
int key;
int value;
struct Node* prev;
struct Node* next;
};
// 定义LRU缓存结构
struct LRUCache {
int capacity; // 缓存容量
struct Node* head; // 头节点
struct Node* tail; // 尾节点
struct Node** map; // 哈希表,用于快速查找节点
};
// 创建双向链表节点
struct Node* createNode(int key, int value) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node)); // 分配内存
node->key = key; // 初始化键
node->value = value; // 初始化值
node->prev = NULL; // 初始化前驱指针
node->next = NULL; // 初始化后继指针
return node;
}
// 初始化LRU缓存
struct LRUCache* createLRUCache(int capacity) {
struct LRUCache* cache = (struct LRUCache*)malloc(sizeof(struct LRUCache)); // 分配内存
cache->capacity = capacity; // 初始化容量
cache->head = createNode(0, 0); // 创建头节点
cache->tail = createNode(0, 0); // 创建尾节点
cache->head->next = cache->tail; // 头节点指向尾节点
cache->tail->prev = cache->head; // 尾节点指向头节点
cache->map = (struct Node**)malloc(sizeof(struct Node*) * 10000); // 哈希表数组,初始化为10000个节点
for (int i = 0; i < 10000; i++) {
cache->map[i] = NULL; // 初始化哈希表中的每个节点为空
}
return cache;
}
// 从LRU缓存中获取值
int get(struct LRUCache* cache, int key) {
if (cache->map[key] != NULL) {
struct Node* node = cache->map[key]; // 从哈希表中获取节点
// 将访问的节点移到链表尾部,表示最近访问
node->prev->next = node->next; // 调整前驱节点的后继指针
node->next->prev = node->prev; // 调整后继节点的前驱指针
node->prev = cache->tail->prev; // 调整节点的前驱指针
node->next = cache->tail; // 调整节点的后继指针
cache->tail->prev->next = node; // 调整尾节点的前驱节点的后继指针
cache->tail->prev = node; // 调整尾节点的前驱指针
return node->value; // 返回节点的值
}
return -1; // 如果哈希表中不存在该节点,则返回-1
}
// 向LRU缓存中插入值
void put(struct LRUCache* cache, int key, int value) {
if (cache->map[key] != NULL) {
// 更新节点的值,并移到链表尾部
printf("xxxxxxxxxxxxxxxxxxxxxxxxx\n");
struct Node* node = cache->map[key]; // 从哈希表中获取节点
node->value = value; // 更新节点的值
node->prev->next = node->next; // 调整前驱节点的后继指针
node->next->prev = node->prev; // 调整后继节点的前驱指针
node->prev = cache->tail->prev; // 调整节点的前驱指针
node->next = cache->tail; // 调整节点的后继指针
cache->tail->prev->next = node; // 调整尾节点的前驱节点的后继指针
cache->tail->prev = node; // 调整尾节点的前驱指针
} else {
if (cache->capacity == 0) {
// 缓存已满,移除链表头部节点
printf("xxxxxxxxxxxxxxxxyyyy\n");
struct Node* temp = cache->head->next; // 获取头节点的后继节点
cache->head->next = temp->next; // 调整头节点的后继指针
temp->next->prev = cache->head; // 调整头节点的后继节点的前驱指针
cache->map[temp->key] = NULL; // 从哈希表中移除节点
free(temp); // 释放节点内存
} else {
printf("xxxxxzzzzzzzzzzzzzzzzzz\n");
cache->capacity--; // 缓存容量减一
}
// 创建新节点,并插入链表尾部
struct Node* node = createNode(key, value); // 创建新节点
node->prev = cache->tail->prev; // 调整新节点的前驱指针
node->next = cache->tail; // 调整新节点的后继指针
cache->tail->prev->next = node; // 调整尾节点的前驱节点的后继指针
cache->tail->prev = node; // 调整尾节点的前驱指针
cache->map[key] = node; // 更新哈希表
}
}
int main() {
struct LRUCache* cache = createLRUCache(2); // 初始化LRU缓存,容量为2
put(cache, 1, 1); // 插入键值对1:1
put(cache, 2, 2); // 插入键值对2:2
printf("%d\n", get(cache, 1)); // 获取键1对应的值,返回1
put(cache, 3, 3); // 插入键值对3:3,移除键2
printf("%d\n", get(cache, 2)); // 获取键2对应的值,返回-1
put(cache, 4, 4); // 插入键值对4:4,移除键1
printf("%d\n", get(cache, 1)); // 获取键1对应的值,返回-1
printf("%d\n", get(cache, 3)); // 获取键3对应的值,返回3
printf("%d\n", get(cache, 4)); // 获取键4对应的值,返回4
return 0;
}
4.Linked_Queue enqueue
// 入队操作
void enqueue(LinkedQueue* queue, TypeData value) {
// 创建新节点
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
// 队列为空,新节点成为队头
queue->front = newNode;
queue->rear = newNode;
} else {
// 将新节点插入到队尾
queue->rear->next = newNode;
queue->rear = newNode;
}
}
5.second-chance page replacement algorithm
// 初始化一个循环队列作为内存页面的缓冲区
int buffer[BUF_SIZE];
int hand = 0; // 指向当前检查的页面
void pageFaultHandler(int pageNumber) {
while (true) {
if (buffer[hand].accessed == 0) {
// 选择此页面进行置换
buffer[hand].pageNumber = pageNumber;
buffer[hand].accessed = 1; // 置换页面并标记为访问过
hand = (hand + 1) % BUF_SIZE; // 移动手指到下一个页面
break;
} else {
buffer[hand].accessed = 0; // 清除访问位
hand = (hand + 1) % BUF_SIZE; // 移动手指到下一个页面
}
}
}
6. acyclic graph
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node* next;
};
void addEdge(struct Node* vertices[], int src, int dest) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = dest;
newNode->next = vertices[src];
vertices[src] = newNode;
}
void printGraph(struct Node* vertices[], int V) {
for (int i = 0; i < V; ++i) {
struct Node* temp = vertices[i];
printf("Vertex %d\n", i);
while (temp) {
printf(" -> %d", temp->value);
temp = temp->next;
}
printf("\n");
}
}
int main() {
int V = 5; // Number of vertices
struct Node* vertices[V];
for (int i = 0; i < V; ++i) {
vertices[i] = NULL;
}
addEdge(vertices, 0, 1);
addEdge(vertices, 0, 2);
addEdge(vertices, 1, 3);
addEdge(vertices, 2, 4);
printGraph(vertices, V);
return 0;
}
7 Bit vector
#include <stdio.h>
#include <stdint.h>
#define SET_BIT(v, n) ((v) |= (1 << (n))) // 设置指定位置的位
#define CLEAR_BIT(v, n) ((v) &= ~(1 << (n))) // 清除指定位置的位
#define TEST_BIT(v, n) ((v) & (1 << (n))) // 查询指定位置的位
int main() {
uint8_t bitVector = 0; // 初始化一个8位的位向量
// 将第3和第5位设置为1
SET_BIT(bitVector, 3);
SET_BIT(bitVector, 5);
// 检查第3位是否被设置
if (TEST_BIT(bitVector, 3)) {
printf("Bit 3 is set\n");
} else {
printf("Bit 3 is not set\n");
}
// 清除第5位
CLEAR_BIT(bitVector, 5);
// 检查第5位是否被清除
if (TEST_BIT(bitVector, 5)) {
printf("Bit 5 is set\n");
} else {
printf("Bit 5 is not set\n");
}
return 0;
}