Programming Abstractions in C阅读笔记:p338-p346

《Programming Abstractions in C》学习第80天,p338-p346,总计9页。

一、技术总结

栈的实现包括入栈、出栈、判断栈是否为满,判断栈是否为空等。作者结合RPN计算器来实现,稍显无聊。

/*
 * File: rpncalc.c
 * ---------------
 * This program simulates an electronic calculator that uses
 * reverse Polish notation, in which the operators com after
 * the operands to which they apply.
 */

#include <stdio.h>
#include <ctype.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "stack.h"

/* Private function prototypes */

static void ApplyOperator(char op, stackADT operandStack);

static void HelpCommand(void);

static void ClearStack(stackADT operandStack);

static void DisplayStack(stackADT operandStack);

/* Main program */
int main() {
    stackADT operandStack;
    string line;
    char ch;

    printf("RPN Calculator Simulator (type H for help)\n");
    operandStack = NewStack();
    while (TRUE) {
        printf("> ");
        line = GetLine();

        ch = toupper(line[0]);
        switch (ch) {
            case 'Q':
                exit(0);
            case 'H':
                HelpCommand();
                break;
            case 'C':
                ClearStack(operandStack);
                break;
            case 'S':
                DisplayStack(operandStack);
                break;
            default:
                if (isdigit(ch)) {
                    Push(operandStack, StringToReal(line));
                } else {
                    ApplyOperator(ch, operandStack);
                }
                break;

        }
    }

    return 0;
}

/* Private functions */

/*
 * Function: ApplyOperator
 * Usage: ApplyOperator(op, operandStack);
 * ---------------------------------------
 * This function applies the operator to the top two elements on
 * the operand stack. Because the elements on the stack are
 * popped in reverse order, the right operand is popped before
 * the left operand.
 */

static void ApplyOperator(char op, stackADT operandStack) {
    double lhs, rhs, result;

    rhs = Pop(operandStack);
    lhs = Pop(operandStack);
    switch (op) {
        case '+':
            result = lhs + rhs;
            break;
        case '-':
            result = lhs - rhs;
            break;
        case '*':
            result = lhs * rhs;
            break;
        case '/':
            result = lhs / rhs;
            break;
        default:
            Error("Illegal operator %c", op);
    }
    printf("%g\n", result);
    Push(operandStack, result);
}

/*
 * Function: HelpCommand
 * Usage: HelpCommand();
 * ---------------------
 * This function generate a help message for the user;
 */

static void HelpCommand(void) {
    printf("Enter expression in Reverse Polish Notation, \n");
    printf("in which operators follow the operands to which\n");
    printf("they apply. Each line consists of a number, an\n");
    printf("operator, or one of the following commands: \n");
    printf("  Q -- Quit the program\n");
    printf("  H -- Display this help message\n");
    printf("  C -- Clear the calculator stack\n");
    printf("  S -- Display all values in the stack\n");
}


/*
 * Function: ClearStack
 * Usage: ClearStack(stack);
 * -------------------------
 * This function clears the stack by popping elements until it is
 * empty.
 */
static void ClearStack(stackADT stack) {
    while (!StackIsEmpty(stack)) {
        (void)Pop(stack);
    }
}

/*
 * Function: DisplayStack
 * Usage: DisplayStack(stack);
 * ---------------------------
 * This function displays the contents of a stack.
 */

static void DisplayStack(stackADT stack) {
    int i, depth;

    printf("Stack: ");
    depth = StackDepth(stack);
    if (depth == 0) {
        printf("empty\n");
    } else {
        for (i = depth - 1; i >= 0; i--) {
            if (i < depth - 1) {
                printf(", ");
            }
            printf("%g", GetStackElement(stack, i));
        }
    }
}

完整代码见:https://github.com/codists/Programming-Abstractions-In-C/tree/main/chapter8

二、英语总结

1.insulation是什么意思?

答:

(1)insulate > insulation

(2)insulate: from insula“island”(isle)。 vt. to cover and surround sth with materials。

(3)insulation: u. the substance used in insulating。

p345, By using StackDepth(stack) form, you provide a layer of insulation between the client and the implementation.

2.auxiliary是什么意思?

答:*aug-“to increase”。adj. giving help, especially to a more import person(辅助的)。

三、其它

今日没有什么想说的。

四、参考资料

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、付费专栏及课程。

余额充值