Programming Abstractions in C阅读笔记:p293-p302

《Programming Abstractions in C》学习第73天,p293-p302总结,总计10页。

一、技术总结

1.时间复杂度

(1)quadratic time(二次时间)

p293, Algorithms like selection sort that exhibit O(N^2) performance are said to run in quadratic time。

2.线性查找(linear search)

p293, Because the for loop in the implementation executes n time, you expect the performance of LinearSearch——as its name implies——to be O(N)。时间复杂度为O(N)的查找称为线性查找。

3.归并排序(merge sort)

书中并为对归并排序做严格的定义。p298, The merge operation, combined with recursive decomposition, gives rise to a new sorting algorithm called merge sort, which you can implement in straightforward way. The basic idea of the algorithm can be outlined as follows:

  1. Check to see if the array is empty or has only one element. If so, it must alread be sorted, and the function can return without doing any work. This condition defines the simple case for the recursion.
  2. Divide the array into two new subarrays, each of which is half the size of the original.
  3. Sort each of the subarrasy recursively.
  4. Merge the two subarrays back into the original one.
/*
 * 伪代码
 */
static void Merge(int array[], int arr1[], int n1, int arr2[], int n2);
static int *CopySubArray(int array[], int start, int n);
void SortIntegerArray(int array[], int n);
/*
 * Function: SortIntegerArray
 * Usage: SortIntegerArray(array, n);
 * ----------------------------------
 * This function sorts the first n elements of array into
 * increasing numerical order using the merge sort algorithm,
 * which requires (1) dividing the array into two halves,
 * (2) sorting each half, and (3) merging the halves together.
 */
void SortIntegerArray(int array[], int n) {
    int n1, n2, *arr1, *arr2;

    if (n <= 1) {
        return;
    }
    n1 = n / 2;
    n2 = n - n1;

    arr1 = CopySubArray(array, 0, n1);
    arr2 = CopySubArray(array, 0, n2);
    SortIntegerArray(arr1, n1);
    SortIntegerArray(arr1, n2);
    Merge(array, arr1, n1, arr2, n2);
    FreeBlock(arr1);
    FreeBlock(arr2);
}

/*
 * Function: Merge
 * Usage: Merge(array, arr1, n1, arr2, n2);
 * ----------------------------------------
 * This function merges two sorted arrays (arr1 and arr2) into a
 * single output array. Because the input arrays are sorted, the
 * implementation can always select the first unused element in
 * one of the input arrays to fill the next position in array.
 */

static void Merge(int array[], int arr1[], int n1, int arr2[], int n2) {
    int p, p1, p2;

    p = p1 = p2 = 0;
    while (p1 < n1 && p2 < n2) {
        if (arr1[p1] < arr2[p2]) {
            array[p++] = arr1[p1++];
        } else {
            array[p++] = arr2[p2++];
        }
    }
    while (p1 < n1) {
        array[p++] = arr1[p1++];
    }
    while (p2 < n2) {
        array[p++] = arr2[p2++];
    }
}

/*
 * Function: CopySubArray
 * Usage: CopySubArray(array, arr1, n1, arr2, n2);
 * -----------------------------------------------
 * This function makes a copy of a subset of an integer array
 * and returns a pointer to a new dynamic array containing the
 * new elements. The array begins at the indicated start
 * position in the original array and continues for n elemnts.
 */

static int *CopySubArray(int array[], int start, int n) {
    int i, *result;

    result = NewArray(n, int);
    for (i = 0; i < n; i++) {
        result[i] = array[start + i];
    }
    return result;
}

二、英语总结

1.quadratic是什么意思?

答:In mathematics by 1660s;the algebraic quadratic euqation(二次方程, 1680s) are so called because they involve the square(x^2) and no higher power of x。这里要注意quarter是四分之一,但是quadratic是二次(x^2)的意思。

2.sophistication是什么意思?

答:

(1)sophist > sophiticate > sophistication。

(2)sophist: one who makes use of fallacious arguments(诡辩者)。

(3)sophistication: u. the quality of being sophisticated(老练, 复杂度)。

p293, The problem, howerver is that average-case analysis is much more difficult to carry out and typically requires considerable mathematical sophistication。这里比较重要的一点是虽然这里用的是名词sophistication,但是因为翻译的时候需要转一下,mathematical sophistication(复杂的数学知识)。

3.average用法总结

答:

(1)vt. to reach a particular amount as an average。主语可以是物也可以是人。翻译的时候需要灵活转变。

主语是物:Inquires to our office average 1000 calls a month.

主语是人:p294, From a practical point of view, it is often useful to consider how well an algorithm performs if you average its behavior over all possible sets of input data

三、其它

本书中作者讲解归并排序(merge sort)和其它书还是有些不同的,从选择排序算法展开,从而引出归并排序算法,可以和其它书进行对比阅读。

四、参考资料

1. 编程

(1)Eric S.Roberts,《Programming Abstractions in C》:https://book.douban.com/subject/2003414

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridage Dictionary:https://dictionary.cambridge.org

在这里插入图片描述

欢迎搜索及关注:编程人(a_codists)

root@ecm-e00f:/# systemctl status apparmor × apparmor.service - Load AppArmor profiles Loaded: loaded (/lib/systemd/system/apparmor.service; disabled; preset: enabled) Active: failed (Result: exit-code) since Wed 2025-06-25 17:05:34 CST; 45s ago Docs: man:apparmor(7) https://gitlab.com/apparmor/apparmor/wikis/home/ Process: 338199 ExecStart=/lib/apparmor/apparmor.systemd reload (code=exited, status=1/FAILURE) Main PID: 338199 (code=exited, status=1/FAILURE) CPU: 39ms Jun 25 17:05:34 ecm-e00f apparmor.systemd[338206]: AppArmor parser error for /etc/apparmor.d in profile /etc/ap> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338208]: AppArmor parser error for /etc/apparmor.d in profile /etc/ap> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338228]: AppArmor parser error for /etc/apparmor.d/lsb_release in pro> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338232]: AppArmor parser error for /etc/apparmor.d/nvidia_modprobe in> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338236]: AppArmor parser error for /etc/apparmor.d/sbin.dhclient in p> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338242]: AppArmor parser error for /etc/apparmor.d/usr.bin.man in pro> Jun 25 17:05:34 ecm-e00f apparmor.systemd[338199]: Error: At least one profile failed to load Jun 25 17:05:34 ecm-e00f systemd[1]: apparmor.service: Main process exited, code=exited, status=1/FAILURE Jun 25 17:05:34 ecm-e00f systemd[1]: apparmor.service: Failed with result 'exit-code'. Jun 25 17:05:34 ecm-e00f systemd[1]: Failed to start apparmor.service - Load AppArmor profiles.
06-26
<think>我们正在处理AppArmor加载配置文件失败的问题,错误信息提示解析错误(parsererrors)。根据引用[1],我们可以检查AppArmor的状态和已加载的配置文件。引用[2][3]提到了Python模块相关的错误,但我们的问题更可能是配置文件语法错误。引用[4]提到了pyflakes,这是一个Python代码检查工具,但可能配置文件语法检查无关。引用[5]是关于硬件操作的,不相关。解决步骤:1.**检查AppArmor状态**:使用`sudoaa-status`查看当前状态,但问题可能出现在启动时加载配置文件失败。2.**检查日志**:使用`journalctl-uapparmor-xe`查看详细错误日志,定位具体是哪个配置文件出错。3.**测试所有配置文件语法**:使用`apparmor_parser`工具检查配置文件的语法:```bashsudoapparmor_parser-QT/etc/apparmor.d/*```这将测试所有配置文件的语法,并报告错误位置。4.**修复或禁用错误配置文件**:-如果发现具体哪个文件出错,可以尝试修复该文件(根据错误信息调整语法)。-如果暂时无法修复,可以移动该文件到备份目录(如`/etc/apparmor.d/disable/`),然后重新加载AppArmor:```bashsudomv/etc/apparmor.d/problem_profile/etc/apparmor.d/disable/sudosystemctlrestartapparmor```5.**重新加载配置文件**:修复后,重新加载配置文件:```bashsudosystemctlreloadapparmor```6.**验证**:再次检查状态和日志:```bashsudoaa-statussystemctlstatusapparmor```**注意**:如果多个文件出错,可能需要逐个修复。常见错误包括语法错误(如缺少括号、错误的规则格式)或包含不存在的文件。如果问题涉及Python模块(如引用[2][3]中的错误),可能是`aa-genprof`等工具的问题,但这不影响AppArmor服务本身。但若配置文件是由这些工具生成且包含错误,则仍需修复配置文件。**引用标识**:在回答中引用相关段落时,在末尾添加引用标识,例如:AppArmor配置文件存储在`/etc/apparmor.d/`目录下[^1]。生成相关问题:1.如何调试AppArmor配置文件语法错误?2.AppArmor服务启动失败的其他常见原因有哪些?3.如何临时禁用AppArmor配置文件?根据以上思路,编写回答。</think>### 解决 AppArmor 配置文件加载失败问题(Parser Errors) 当 AppArmor 服务因配置文件解析错误而启动失败时,根本原因是配置文件存在语法或结构问题。以下是系统化的解决步骤: #### 步骤 1:定位错误配置文件 ```bash sudo journalctl -u apparmor -xe --since "1 hour ago" | grep "AppArmor parser error" ``` 检查输出中明确指出的错误文件和行号,例如: ``` Profile /etc/apparmor.d/usr.sbin.mysqld failed to load: Syntax error at line 15 ``` #### 步骤 2:批量检查配置文件语法 ```bash # 测试所有配置文件的语法(-Q=安静模式,-T=显示错误) sudo apparmor_parser -QT /etc/apparmor.d/* ``` 输出示例: ``` Error: Could not open 'abstractions/missing_file' (No such file or directory) in profile /etc/apparmor.d/nginx Syntax error at line 22 in /etc/apparmor.d/custom_profile ``` #### 步骤 3:修复常见错误类型 | 错误类型 | 修复方法 | 示例 | |---------|----------|------| | **包含文件缺失** | 创建缺失文件或注释包含行 | `sudo touch /etc/apparmor.d/abstractions/missing_file` | | **语法错误** | 按规范修正规则 | 错误:`/data/** rw,` → 修正:`/data/** rw,` | | **规则冲突** | 删除重复规则 | 删除重复的 `capability net_bind_service` | | **路径错误** | 验证路径是否存在 | `/var/run/nonexist.sock` → 改为实际路径 | #### 步骤 4:临时禁用问题配置(紧急恢复) ```bash # 移动错误配置文件到隔离目录 sudo mkdir -p /etc/apparmor.d/disable/ sudo mv /etc/apparmor.d/faulty_profile /etc/apparmor.d/disable/ # 重新加载服务 sudo systemctl restart apparmor ``` #### 步骤 5:深度修复配置文件 1. **使用调试模式**: ```bash sudo apparmor_parser -d -T /etc/apparmor.d/problem_profile ``` 2. **检查依赖抽象层**: ```bash grep 'include' /etc/apparmor.d/problem_profile | while read line; do echo "Checking: ${line#include <}"; ls "/etc/apparmor.d/${line#include <}" done ``` 3. **重建配置文件**(参考引用[2]): ```bash sudo aa-genprof /path/to/application # 需要安装apparmor-utils ``` #### 步骤 6:验证修复结果 ```bash # 检查服务状态 sudo systemctl status apparmor # 确认配置文件加载 sudo aa-status | grep "profiles are loaded" ``` 成功输出应显示: ``` 15 profiles are loaded. ``` > **重要提示**:操作前备份配置: > ```bash > sudo cp -a /etc/apparmor.d{,.bak_$(date +%F)} > ``` ### 常见错误原因 1. **手动编辑错误**:直接修改配置文件导致语法错误 2. **抽象层缺失**:`#include <abstractions/...>` 指向不存在的文件[^1] 3. **工具链问题**:`aa-genprof` 因 Python 模块缺失生成错误配置(引用[2][3]) 4. **版本不兼容**:旧版配置文件在新版 AppArmor 中失效
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值