0

《Unix/Linux编程实践教程》笔记(6)──进程和程序:编写命令解释器sh

 2 years ago
source link: https://houye.xyz/2018-08/uup7/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
《Unix/Linux编程实践教程》笔记(6)──进程和程序:编写命令解释器sh

《Unix/Linux编程实践教程》笔记(7)──进程和程序:编写命令解释器sh

shell是如何运行程序的――shell用fork建立新进程,用exec在新进程中运行用户指定的程序,最后shell用wait等待新进程结束。

执行文件 execvp(const char * file, const char * argvp[]) 创建进程 fork(void) 等待进程结束 wait (int * statusptr)

FILE有缓冲,所以会输出4条记录。可以在父进程中fflush(fp)强制清空缓冲区,这样就只有三条记录了。

当有多个子进程同时退出时,wait会同时收到退出信号。这里可以用waitpid()来解决。

可以在父进程中屏蔽 SIGINT , SIGQUIT

signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);

在子进程中恢复对 SIGINT , SIGQUIT 的默认处理

signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);

在这章,学会了简单的fork子进程,让子进程调用系统接口去执行命令,一个最最简单的shell就是这样。

Created: 2018-08-29 Wed 10:00

Emacs 24.5.1 (Org mode 8.2.10)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK