复习
本次课回答的问题
本次课主要内容
整个计算机系统世界的 “构建”
我们需要一个 “用户能直接操作” 的程序管理操作系统对象。
这就是 Shell (内核 Kernel 提供系统调用;Shell 提供用户接口)
“终端” 时代的伟大设计
“Unix is user-friendly; it's just choosy about who its friends are.”
支持的功能
ls
ls > a.txt
ls | wc -l
ls &
(echo a ; echo b) | wc -l
cmd > file < file 2> /dev/null
cmd1; cmd2
, cmd1 && cmd2
, cmd1 || cmd2
cmd1 | cmd2
$()
, <()
Job control
在 “自然语言”、“机器语言” 和 “1970s 的算力” 之间达到优雅的平衡
ls > a.txt | cat
(bash/zsh)$ echo hello > /etc/a.txt
bash: /etc/a.txt: Permission denied
$ sudo echo hello > /etc/a.txt
bash: /etc/a.txt: Permission denied
Open question: 我们能否从根本上改变命令行的交互模式?
Shell 连接了用户和操作系统
已经看到的一些方向
为什么 Ctrl-C 可以退出程序?
为什么有些程序又不能退出?
为什么 fork-printf.c 会在管道时有不同表现?
为什么 Tmux 可以管理多个窗口?
终端是 UNIX 操作系统中一类非常特别的设备!
首先,我们可以 “使用” tmux
然后,我们也可以把 tmux “打开”
-o
) 可以看到一些关键的系统调用 (以及 man 7 pty)为什么 fork-printf
能识别 tty 和管道?
#include <stdio.h>
int main() {
printf("Hello, World\n");
}
大家熟悉的 Segmentation Fault/Floating point exception (core dumped)
UNIX (System V) 信号其实是有一些 dark corners 的
SIGSEGV
里再次 SIGSEGV
?sigaction(2)
, which provides explicit control of the semantics when a signal handler is invoked; use that interface instead of signal()
. RTFM: setpgid/getpgid(2),它解释了 process group, session, controlling terminal 之间的关系
——你神奇地发现,读手册不再是障碍了!
./a.out &
创建新的进程组 (使用 setpgid)SIGINT
), that signal is sent to the foreground process group.SIGTTIN
signal, which suspends it.cat &
时你看到的 “suspended (tty input)”setpgid()
and getpgrp()
calls are used by programs such as bash(1) to create process groups in order to implement shell job control.ps -eo pid,pgid,cmd
可以查看进程的 pgid本次课回答的问题
Take-away messages