【笔记】Linux 综合训练笔记

版权声明:欢迎评论交流,转载请注明原作者。 https://blog.csdn.net/m0_37809890/article/details/86101555

参考资料
查看版本信息
网络接口配置文件
各发行版比较
makefile

1. linux版本信息

查看发行版本信息:cat /etc /issue

2. 网络接口配置文件

  1. redhat系列:/etc/sysconfig/network-scripts目录
  2. debian系列:/etc/network/interfaces文件(我使用的是debian/ubuntu,下同)
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo # 表示lo接口会在系统启动时被自动配置
iface lo inet loopback # 表示将lo接口设置为一个本地环回地址

3. 用户与组操作

  1. 新建用户useradd username
  2. 配置用户密码passwd username
  3. 新建组groupadd username
  4. 把用户添加到组中gpasswd -a username groupname
  5. 切换用户su username
  6. 查看当前用户whoami

4. makefile基础

  1. 基础语法
target...: prerequisites...
	command
  1. 修饰符号
    换行符前可以加/
    命令前加tab
    只有单行注释,以#开头
  2. 变量
    定义变量 name = value
    使用变量 $(varname)
  3. 隐晦规则
    每个o文件会自动推导相应名称的c文件
  4. clean
    	.PHONY: clean
    	clean:
    	-rm edit $(objects)
    
  5. 示例
    	objects = main.o kbd.o command.o display.o \
    	insert.o search.o files.o utils.o
    	edit : $(objects)
    		cc -o edit $(objects)
    	main.o : defs.h
    	kbd.o : defs.h command.h
    	command.o : defs.h command.h
    	display.o : defs.h buffer.h
    	insert.o : defs.h buffer.h
    	search.o : defs.h buffer.h
    	files.o : defs.h buffer.h command.h
    	utils.o : defs.h
    	.PHONY : clean
    	clean :
    		rm edit $(objects)
    
  6. 使用方法:保存文件名makefile,在相应目录下调用命令make编译,调用make clean卸载

5. CPU 调度

调度方式

  1. FCFS先来先服务:队列
  2. SJF短作业优先:调度最短的进程,优先队列
  3. PS优先权调度:调度优先级最高(整数最小)的进程,优先队列

评估方式
4. 等待时间:进程在就绪队列中等待调度的时间片综合
5. 周转时间:进程从提交到运行结束的全部时间
6. 以上的平均值

6. 内存页面调度

  1. FIFO(First In First Out) 先进先出置换算法
  2. LRU(Least Recently Used) 最近最少使用
    可以理解为:最远的没使用的那个。
  3. NUR(Never Used Recently) 最近未使用
    可以理解为:在一个周期内没有使用的一个。
  4. OPT(Optimal Replacement) 最佳算法:被置换的页面将是之后最长时间不被使用的页
#include <bits/stdc++.h>
using namespace std;
const int N = 10, M = 3;
struct Page
{
	bool in_memory;
}pages[N];

struct FIFO_memory
{
	deque<int> q;
	void print()
	{
		for(auto x:q)
			printf("%d ",x );
		printf("\n");
	}
	int deal(int pid)
	{
		if(pages[pid].in_memory) return 1;

		if(q.size() == M)
		{
			int trash = q.front(); q.pop_back();
			pages[trash].in_memory = 0;
		}

		q.push_front(pid);
		pages[pid].in_memory = 1;

		return 0;
	}
};
struct LRU_memory
{
	struct Node {
		Node *prev, *next;
		int id;
	}*head,*tail;
	unordered_map<int,Node*> mp;

	void move_to_head(Node *nod)
	{
		if(nod->prev != head)
		{
			nod->next->prev = nod->prev;
			nod->prev->next = nod->next;

			nod->prev = head;
			nod->next = head->next;
			head->next->prev = nod;
			head->next = nod;
		}
	}
	void add(int pid)
	{
		Node *nod = new Node;
		mp[pid] = nod;
		nod->id = pid;

		nod->prev = head;
		nod->next = head->next;
		head->next->prev = nod;
		head->next = nod;
	}
	void pop()
	{
		Node *nod = tail->prev;
		pages[nod->id].in_memory = 0;
		mp.erase(nod->id);

		nod->next->prev = nod->prev;
		nod->prev->next = nod->next;
		delete nod;
	}
	LRU_memory()
	{
		head = new Node;
		tail = new Node;
		head->next = tail;
		tail->prev = head;
	}
	~LRU_memory()
	{
		delete head;
		delete tail;
		for(auto &x:mp) delete x.second;
	}
	void print()
	{
		for(Node *p=head->next; p!=tail; p=p->next)
			printf("%d ",p->id);
		printf("\n");
	}
	int deal(int pid)
	{
		if(pages[pid].in_memory)
		{
			move_to_head(mp[pid]);
			return 1;
		}

		pages[pid].in_memory = 1;
		if(mp.size()==M)
		{
			pop();
		}
		add(pid);
		return 0;
	}
};
struct NUR_memory
{
	int period,cnt;
	unordered_map<int,int> uses;
	void pop()
	{
		for(auto x:uses)
		{
			if(x.second==0)
			{
				pages[x.first].in_memory = 0;
				uses.erase(x.first);
				return;
			}
		}
		pages[uses.begin()->first].in_memory = 0;
		uses.erase(uses.begin());
	}
	void print()
	{
		for(auto x:uses)
			printf("%d ",x.first );
		printf("\n");
	}
	NUR_memory(int p=5)
	{
		period = p;
		cnt = p;
	}
	int deal(int pid)
	{
		if(pages[pid].in_memory)
		{
			++uses[pid];
			return 1;
		}
		if(uses.size()==M)
		{
			pop();
		}
		uses[pid]=1;
		pages[pid].in_memory = 1;

		--cnt;
		if(cnt==0)
		{
			cnt = period;
			for(auto &x:uses)
				x.second = 0;
			printf("NUR period reset\n");
		}
		return 0;
	}
};
struct OPT_memory
{
	int cnt;
	vector<int> next;
	map<int,int> mp; //time,pid

	OPT_memory(const vector<int> &vc)
	{
		next = vc;
		int last[M]={};
		for(int i=vc.size()-1;i>=0;--i)
		{
			next[i] = last[vc[i]] ? last[vc[i]] : vc.size();
			last[vc[i]] = i;
		}
		cnt = 0;
		printf("?\n");

	}
	void pop()
	{
		// mp.erase(--mp.end());
	}
	void add(int pid)
	{
		// mp[next[cnt]] = pid;
	}
	void print()
	{
		// for(auto x:mp)
		// 	printf("%d ",x.second );
		// printf("\n");
	}
	int deal(int pid)
	{
		if(pages[pid].in_memory)
		{
			mp.erase(mp.begin());
			mp[next[cnt]] = pid;
			return 1;
		}
		if(mp.size()==M)
		{
			pop();
		}
		pages[pid].in_memory = 1;
		add(pid);

		++cnt;
		return 0;
	}
};
template<class T>
void test(T &memory, const vector<int> &input,const string &type)
{
	memset(pages,0,sizeof(pages));
	printf("%s Algorithm\n",type.c_str() );
	int all = 0, hit = 0; 
	for(auto x:input)
	{
		++all;
		hit += memory.deal(x);
		memory.print();
	}
	printf("all=%d hit=%d\n",all,hit );
	printf("hit rate = %d%%\n",hit*100/all );
	printf("\n");
}
int main(void)
{
	const vector<int> input[2]={
		{1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5},
		{7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}
	};
	for(int i=0;i<2;++i)
	{
		//FIFO_memory fifo;
		//LRU_memory lru;
		//NUR_memory nur;
		OPT_memory opt(input[i]);
		printf("??\n");
		//test(fifo, input[i], "FIFO");
		//test(lru, input[i], "LRU");
		//test(nur, input[i], "NUR");
		test(opt, input[i], "OPT");
		
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37809890/article/details/86101555