四个系统调用

《直观感受》章节已经提供了一个样例程序运行三个系统调用。下面还是再介绍一点。

fork系统调用

fork系统调用用于创建子进程,内核需要为子进程分配一块独立的内存,将父进程的地址空间、用户态堆栈完全拷贝至子进程的内存中(温馨提示,不要用结构体整体赋值!!!因为这是老版本c语言未定义行为),并为子进程分配独立的进程控制块,完成对子进程的进程控制块的设置。

若子进程创建成功,则对于父进程,该系统调用的返回值为子进程的 pid ,对于子进程,其返回值为 0 ;若子进程创建失败,该系统调用的返回值为 -1。

pid_t fork();

Exec系统调用

相比于fork,我们可能更少去用exec系统调用,但实际上他们常常配合使用。wikipedia是这样介绍exec的:

In computingarrow-up-right, exec is a functionality of an operating systemarrow-up-right that runs an executable filearrow-up-right in the context of an already existing processarrow-up-right, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-likearrow-up-right systems, although it exists elsewhere. As no new process is created, the process identifierarrow-up-right (PID) does not change, but the machine codearrow-up-right, dataarrow-up-right, heaparrow-up-right, and stackarrow-up-right of the process are replaced by those of the new program.

The exec call is available for many programming languagesarrow-up-right including compilablearrow-up-right languages and some scripting languagesarrow-up-right. In OS command interpretersarrow-up-right, the exec built-in commandarrow-up-right replaces the shell process with the specified program.[1]arrow-up-right

在Linux中,exec的实现比较复杂,在这里,我们实现一个简化版本。我们的exec把一个新的程序装载到当前进程中,替换掉当前进程

在我们的实验中,exec系统调用接受两个参数:secstartsecnum。第一个参数表明新程序在磁盘中的位置,第二个参数表明新程序在磁盘中占据的扇区数。

triangle-exclamation

sleep系统调用

sleep系统调用用于进程主动阻塞自身,内核需要将该进程由 RUNNING 状态转换为 BLOCKED 状态,设置 该进程的 SLEEP 时间片,并切换运行其他 RUNNABLE 状态的进程。

exit系统调用

exit系统调用用于进程主动销毁自身,内核需要将该进程由 RUNNING 状态转换为 DEAD 状态,回收分配 给该进程的内存、进程控制块等资源,并切换运行其他 RUNNABLE 状态的进程。我们实验中实现的是一个简化版本,不需要参数(你用C语言使用`exit()`的时候是需要一个参数的)。

circle-info

exercise8:请用fork,sleep,exit自行编写一些并发程序,运行一下,贴上你的截图。(自行理解,不用解释含义,帮助理解这三个函数)

Last updated