XV6 0x10
lab10mmap(hard)系统调用的添加过程这里就不赘述了,首先,我们先添加vma结构体。 1234567891011121314151617181920struct VMA{ uint64 addr; //起始地址 uint64 size; //大小 uint64 prot; //权限 int fd; //文件描述符 int used ; //引用计数 int offset; //偏移 struct file* file; //文件名 int flag; //标志};// Per-process statestruct proc { struct spinlock lock;... struct VMA vma[16];};//在进程创建初始化memset(&p->vma, 0,...
贪心
贪心TODO:畜栏预定、雷达设备 区间问题贪心题的要点: 思考、证明通过该方法得到的解是最优解,可用res <= ans,res >= ans ——> res == ans。 acwing905 我们通过重载运算符,设置为通过右端点从小到大排序,获取第一个右端点最大值为max值,若其他线段的左端点小于该max值,则continue。若大于则更新max值,res++。 12345678910111213141516171819202122232425262728293031#include <algorithm>#include <iostream>using namespace std;const int N = 100010;struct st { int lef; int rig; bool operator<(const st &tmp) const { return this->rig < tmp.rig; }} st[N];int...
Codeforces Round 960 (Div. 2)
A 这题在一次操作中,玩家可以执行以下操作: 选择一个索引 iii (1 ≤ i ≤ n),使得 a[i] ≥ mx,并将 mx 设为 a[i]。然后,将 a[i] 设为 0。 Alice先手,判断 Alice 是否有必胜策略。 刚开始我兴冲冲取最大值,判断是否为奇数,然后就WA了。 其实这题属于博弈论,只要有一个数的数量为奇数,则Alice获胜 Alice选择最大的奇数,取走一个数,该数的数量变为偶数。 此时该数后的数后的数的数量都为偶数,则该问题转变为对手的问题,我们易推断出对手必输。 1234567891011121314151617181920212223242526272829303132333435363738#include <algorithm>#include <iostream>using namespace std;const int N = 105;int main() { int t; cin >> t; while (t--) { int arr[N]; for...
2024_7_17暑期集训
序言还得是黄神!比赛直接AC四题!挽救了我们队! 2488: Reposts 这题有点像食物链,即第二人转发消息(repost)到第一个人,再以此类推。要求求最大的传播链长度,由于我们不需要考虑该网民不知道消息转发的情况。因此可以采用队列来存储传递信息,用map存储食物链 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#include <algorithm>#include <iostream>#include <queue>#include <string>#include <unordered_map> #include <utility> //pairusing namespace std;typedef pair<string, string> PII; //二元组queue<PII>...
XV6 0x9
lab9文件系统文件系统API 文件描述符必须与文件名无关(修改文件名后仍然可以通过文件描述符操作文件) 记录offset(当前读写位置) 友好的文件名 在用户/进程间共享文件 持久性 inode满足以下两种条件时可以删除文件 link count == 0 open fd count ==...
Codeforces Round 957 (Div. 3)
A 题目大意是对输入的a,b,c三个数,每个数可以自增,但是总自增值不得大于5。求$abc$的最大值。 这题直接使用暴力做法 1234567891011121314151617181920212223242526272829#include <iostream>#include <algorithm>using namespace std;int num[3][1005];int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> num[0][i] >> num[1][i] >> num[2][i]; } for (int i = 0; i < n; i++) { int maxnum = 0; for (int a = 0; a <= 5; a++) { for (int...
2024_7_11暑期集训
3900: 两袋面包 我们只需要按照题意解出即可。 12345678910111213141516171819202122#include <iostream>using namespace std;#define int long longsigned main() { int y, k, n; while (cin >> y >> k >> n) { bool flag = false; for (int i = 1; i < n - y; i++) { if ((i + y) % k == 0) { flag = true; cout << i << " "; } } if (!flag) cout << "-1" << endl; else cout <<...
XV6 0x8
lab8多进程coordination ——sleep/wake pipes disk read wait broken sleep:在无锁状态下,中断处理提前调用了 wakeup(void *chan),当 sleep(void* chan, struct spinlock *lk) 执行时,wakeup(void *chan) 已经执行完毕,导致进程持续睡眠。因此sleep(void* chan, struct spinlock *lk)是原子的,在释放锁的同时将进程睡眠。 进程结束,父进程为什么要wait: 因为子进程无法释放自己的堆栈。 父进程可以获取子进程的退出码,并根据退出码决定接下来的操作。例如,如果子进程出现错误,父进程可能需要重新启动子进程或执行一些错误处理逻辑。 方便进行进程同步 Memory allocator(moderate)我们将内存分配器的锁分配给每个CPU,同时,初始化锁。 12345678910111213141516struct { struct spinlock lock; struct run...
XV6 0x7
lab7mutex锁的好处: 锁可以避免更新丢失 使多步操作成为原子操作 锁帮助维护不变量 错误使用锁的问题: 死锁(deadlock) ——改用顺序获取释放锁 模块化(modularity) 性能(performance) ——拆分数据结构 锁实现: 通过硬件实现锁获取的原子性 关闭中断,例如当线程获取锁进入临界区,在临界区产生中断,而中断处理程序也需要获取该,从而产生死锁 _sync_lock_test_and_set 是 GNU Compiler Collection (GCC) 提供的一种原子操作函数 123456789101112acquire:while(_sync_lock_test_and_set(&lk_>locked, 1) != 0) ;在locked原子地写入1,但是返回之前的值,若之前的值等于0(没人拿到过锁),代表该进程成功获得锁,退出循环__sync_synchronize();... ...
XV6 0x6
lab6interrupt 先设置初始化中断,在每个cpu运行进程时启用该cpu的中断。当需要向设备写入或读取数据时,调用函数判断该是否有缓冲区写入数据,如果有,则发生数据;如果没有,则该进程休眠,进行上下文切换,执行其他进程。 cpu接受到中断,由用户模式切换为监督者模式,进入usertrap()函数,判断为哪类中断。跳转到对应中断处理函数。 对于以太网等能以极高频率产生中断的程序(极大的中断开销),通常采用轮询(polling)的方式来处理中断。 Implement copy-on write (hard)太菜了没能搞成实验,搞了很久还是不行,内核编程太痛苦了。有时间把这个补充以下。 //TODO 总结一下原因: 将重复、可明确区分的操作划分为函数操作,方便调试和区分。 增加动手前的思考时间,减少中途的改动。 要有耐心。