Effective Access Time

本文探讨了使用Translation Lookaside Buffer (TLB)的分页方案中,有效存取时间的计算方式。当TLB命中率为90%,且无页面错误时,有效存取时间为65纳秒。此外,文章还解释了当命中率为80%时的有效存取时间计算方法。

 

A paging scheme uses a Translation Loo-aside Buffer (TLB) A TLB access takes 10 ns and a main memory access takes 50 ns. What is the effective access time (in ns) if the TLB hit ratio is 90% and there is no page-fault?

a) 54

b) 60

c) 65

d) 75

 

 

 

Dear student,

the correct answer is c)65

Sol: (10+50)*0.9 +(10+50+50)*0.1=65

 

 


 

Effective Memory Access Time
The percentage of times that a page number is found in the associative registers is called the hit ratio. An 80-percent hit ratio means that we find the desired page number in the associative registers 80 percents of the time. If it takes 20 nanoseconds to search the associative registers, and 100 nanoseconds to access memory, then a mapped memory access takes 120 nanoseconds when the page number is in the associative registers. If we fail to find the page number in the associative registers (20 nanoseconds), then we must first access memory for the page table and frame number (100 nanoseconds), and then access the desired byte in memory (100 nanoseconds), for a total of 220 nanoseconds. To find the effective access time, we must weigh each case by it probability:

effective access time = 0.80 * 120 + 0.20 * 220 
= 140 nanoseconds.
FROM: http://www2.cs.uregina.ca/~hamilton/courses/330/notes/memory/paging.html

Page Fault

 

  • when a process tries to access an address whose page is not currently in memory
  • process must be suspended, process leaves the processor and the ready list, status is now ``waiting for main memory''
<think>好的,用户让我解释access()函数,我需要先回想一下这个函数的基本信息。记得在Linux系统编程中,access()是用来检查文件访问权限的。用户可能是在学习系统调用或者编写需要检查文件权限的代码时遇到了问题。 首先,我需要确认access()的功能,它检查调用进程对某个文件的权限,比如是否存在、可读、写、执行。函数原型应该是int access(const char *pathname, int mode),返回0表示成功,-1则失败,并设置errno。 接下来,用户可能想知道各个参数的具体含义,特别是mode参数的不同选项,比如F_OK、R_OK、W_OK、X_OK。需要详细解释每个选项的作用,以及它们如何组合使用。 然后,需要说明access()的使用场景,比如在打开文件前检查是否存在或者是否有权限,避免不必要的错误。但也要提醒用户注意access()的局限性,比如检查后文件可能被删除或权限更改,导致后续操作失败,存在安全风险,因此更推荐直接尝试操作并处理错误。 还要提到errno的常见错误代码,比如ENOENT文件不存在,EACCES权限不足等,帮助用户理解错误处理。 可能用户需要示例代码,所以应该提供一个简单的例子,比如检查文件是否存在和可读。同时,指出潜在的问题,比如时间窗口问题,即检查和实际操作之间可能发生的变化。 最后,用户可能想知道access()与其他系统调用的区别,比如open(),以及何时使用access()更合适。需要比较它们的优缺点,强调安全性和正确性方面的考虑。 总结时,要确保用户理解access()的基本用法、参数、返回值、错误处理,以及使用时的注意事项。同时,相关问题部分可以引导用户深入思考权限检查的最佳实践和替代方法。</think>### `access()` 函数介绍与解释 `access()` 是 Linux/Unix 系统提供的一个系统调用,用于检查调用进程对指定文件或路径是否具有特定的访问权限(如存在性、读、写、执行权限)。其核心作用是验证文件访问权限,而非实际操作文件内容。 --- #### **1. 函数原型** ```c #include <unistd.h> int access(const char *pathname, int mode); ``` - **参数说明**: - `pathname`:文件或路径的名称(以 NULL 结尾的字符串)。 - `mode`:要检查的访问权限类型,可选值如下: - `F_OK`:检查文件是否存在(默认行为)。 - `R_OK`:检查是否可读。 - `W_OK`:检查是否可写。 - `X_OK`:检查是否可执行。 - **组合方式**:通过按位或(`|`)组合多个权限(如 `R_OK | W_OK`)。 - **返回值**: - 成功(满足权限要求):返回 `0`。 - 失败:返回 `-1`,并设置 `errno` 错误码。 --- #### **2. 典型使用场景** 1. **权限验证**:在操作文件前检查权限(如读取配置文件前验证可读性)。 2. **存在性检查**:判断文件是否存在于指定路径(替代 `stat()` 的简化操作)。 3. **安全控制**:限制程序对敏感文件的访问(如防止越权操作)。 --- #### **3. 错误码(errno)** - `ENOENT`:文件或路径不存在。 - `EACCES`:权限不足(不满足 `mode` 指定的访问条件)。 - `EFAULT`:`pathname` 指针无效。 - `ENOMEM`:内存不足(路径名过长时)。 - `ETXTBSY`:尝试写入正在执行的二进制文件(特定系统行为)。 --- #### **4. 示例代码** ```c #include <unistd.h> #include <stdio.h> int main() { const char *filename = "example.txt"; // 检查文件是否存在且可读 if (access(filename, F_OK | R_OK) == 0) { printf("文件存在且可读。\n"); } else { perror("访问失败"); } return 0; } ``` --- #### **5. 注意事项** 1. **时间窗口问题(TOCTOU)**: - `access()` 仅检查当前时刻的权限,后续实际操作(如 `open()`)可能因文件权限变化而失败,存在安全风险。 - **推荐替代方案**:直接尝试操作文件并处理错误(如 `open()` 时设置 `O_CREAT` 并检查返回值)。 2. **权限检查机制**: - 基于进程的有效用户 ID(effective UID)和有效组 ID(effective GID)进行权限判断。 - 不触发文件访问时间(atime)更新(与 `open()` 不同)。 3. **符号链接处理**: - 若 `pathname` 是符号链接,`access()` 会检查链接指向的目标文件权限。 4. **性能开销**: - 相比直接操作文件,`access()` 需要一次额外的系统调用,频繁使用可能影响性能。 --- #### **6. 与 `open()` 的对比** | **特性** | `access()` | `open()` | |-------------------|------------------------------|-------------------------------------| | **目的** | 仅检查权限 | 打开文件并返回文件描述符 | | **权限验证** | 基于进程有效 UID/GID | 同样基于有效 UID/GID | | **副作用** | 无文件操作 | 可能修改文件状态(如创建文件) | | **安全性** | 存在 TOCTOU 风险 | 更安全(直接操作避免权限变化问题) | | **适用场景** | 快速检查权限/存在性 | 实际读写文件前的准备 | --- #### **7. 替代方案** - **`faccessat()`**:支持相对路径检查(通过文件描述符指定目录)。 - **`stat()`**:获取文件元信息(如权限、大小),但不验证实际访问权限。 - **直接尝试操作**:通过 `open()`、`read()` 等调用直接处理文件,失败后根据 `errno` 做响应。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值