Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6911cc4b-5d54-800b-88ae-dc7ac6ff2d3d
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== 对话记录 === ==== 学习主题 ==== 《Head First C 嗨翻 C 语言》第 9 章:系统调用与进程管理 --- ===== 1. 系统调用的核心作用 ===== '''提问:''' 系统调用的核心作用是什么?为什么 C 标准库中的很多函数需要依赖系统调用? '''回答:''' 系统调用是C程序与操作系统内核进行通信的机制,是应用程序访问计算机硬件和系统资源(如进程、内存、文件、网络等)的唯一途径。 --- ===== 2. 内核管理三方面 ===== '''提问:''' 内核在计算机系统中主要负责哪三方面的管理?你能分别简单说明它们的作用吗? '''回答:''' 处理机、存储、设备 '''补充说明:''' * 内核负责管理 '''进程 (Processes)'''、'''内存 (Memory)''' 和 '''硬件设备 (Hardware)'''。 * 每一项作用: * 进程管理:加载程序、分配资源、监控状态。 * 内存管理:分配内存空间、使用虚拟内存。 * 硬件管理:通过驱动控制设备。 --- ===== 3. “为什么不是处理机” ===== '''提问:''' 我这里不明白为什么不是处理机,请讲解。 '''回答说明:''' * 内核管理的是 '''进程的执行''',而不是 CPU 硬件本身。 * 内核分配 CPU 时间片给进程,CPU 执行指令,但 CPU 硬件自身不受内核直接控制。 * 所以内核职责是 '''进程、内存、硬件''',不是“处理机”。 --- ===== 4. 系统调用图解 ===== '''提问:''' 可以画个图说明吗? '''图示:''' <syntaxhighlight>+---------------------------+ | 用户程序 | | (进程A、进程B……) | +---------------------------+ | | 系统调用 / 进程请求 CPU v +---------------------------+ | 内核 | | - 进程管理(调度、创建、终止) | | - 内存管理(分配、保护、虚拟内存)| | - 硬件管理(通过驱动控制设备) | +---------------------------+ | | 调度 CPU 执行指令 v +---------------------------+ | CPU | | 执行指令,处理数据 | +---------------------------+ </syntaxhighlight> --- ===== 5. system() 函数优缺点 ===== '''提问:''' <code>system()</code> 函数可以用来在 C 程序中运行其他程序。你觉得它有哪些优点和潜在的安全风险? '''回答:''' 优点:简便 缺点:shellcode注入风险 '''补充说明:''' * <code>system()</code> 调用 shell 执行命令,易受命令行注入攻击。 * 示例: <syntaxhighlight lang="c">char cmd[256]; snprintf(cmd, sizeof cmd, "ls %s", user_input); system(cmd); // 不安全,用户输入可包含 ; && 等 </syntaxhighlight> '' 安全替代:使用 <code>fork() + exec''()</code> 或 <code>posix_spawn()</code>,直接传参数数组,不通过 shell。 --- ===== 6. fork() 返回值 ===== '''提问:''' <code>fork()</code> 调用后如何通过返回值区分父进程和子进程? '''回答:''' * 0 → 子进程 * 非零正数 → 父进程(子进程 PID) * -1 → 创建失败 --- ===== 7. fork() 与 exec() 的配合 ===== '''提问:''' 为什么我们常把 <code>fork()</code> 和 <code>exec()</code> 配合使用,而不是直接在父进程中调用 <code>exec()</code>? '''回答:''' * <code>exec()</code> 会替换当前进程,如果直接在父进程调用,父进程消失,后续代码不再执行。 * 先 <code>fork()</code> 创建子进程,子进程执行 <code>exec()</code>,父进程继续运行,实现并发或连续操作。 --- ===== 8. 对话总结 ===== '''回答总结:''' # 系统调用理解正确。 # 内核管理三方面:进程、内存、硬件,概念比“处理机”更准确。 # <code>system()</code> 简便但易受命令注入攻击,推荐使用 <code>fork() + exec*()</code>。 # <code>fork()</code> 返回值理解正确,配合 <code>exec()</code> 实现子进程执行新程序而父进程继续运行。 '''建议:''' * 精确使用术语(进程、内存、硬件)。 * 写实验程序观察 <code>fork()</code> + <code>exec()</code> 的执行。 * 绘制内核与 CPU、父子进程流程图帮助记忆和理解。 <syntaxhighlight> </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)