趣味英译文:APUE第三版
Posted 看,未来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了趣味英译文:APUE第三版相关的知识,希望对你有一定的参考价值。
1.9 Signals
Signals areatechnique used to notify a process that some condition has occurred. For example, if a process divides by zero, the signal whose name is SIGFPE (floating-point exception) is sent to the process. The process has three choices for dealing with the signal.
- Ignore the signal. This option isn’t recommended for signals that denote(预示) a hardware exception, such as dividing by zero or referencing memory outside the address space of the process, as the results are undefined.
- Let the default action occur. For a divide-by-zero condition, the default is to terminate the process.
- Provide a function that is called when the signal occurs (this is called ‘‘catching’’ the signal). By providing a function of our own, we’ll know when the signal occurs and we can handle it as we wish.
Many conditions generate(产生) signals. Two terminal keys, called the interrupt key— often the DELETE key or Control-C — and the quit key—often Control-backslash — are used to interrupt the currently running process. Another way to generate a signal is by calling the kill function. We can call this function from a process to send a signal to another process. Naturally, there are limitations: we have to be the owner of the other process (or the superuser) to be able to send it a signal.
信号,是一种用于通知进程某个条件已经被触发的技术。比如说:如果一个进程出现了除以0的事件,进程就会收到SIGFPE的信号。
进程处理信号一般有三种方法:
1、直接无视。对于表示硬件异常的信号,例如除以零或引用进程地址空间之外的内存,不建议你这么做,因为我也不知道这么做会导致什么结果。
2、让它自生自灭。还是那个例子,最终结果就是进程死翘翘了。
3、提供回调函数。前提有三:1、我们知道什么时候会出什么bug;2、那个bug的结果可控;3、给出可控bug的函数,就是回调函数。
能产生信号的场景还是蛮多的,比方说Ctrl-C、Ctrl-B(我咋一直用的是Z),用于中断当前正在运行的进程。
另外啊,生成信号的另一种方法是调用kill函数。我们可以从一个进程调用这个函数,向另一个进程发送信号。
当然,也有一些限制:我们必须是另一个进程的所有者(或超级用户)才能向它发送信号。
1.10 Time Values
Historically, UNIX systems have maintained two different time values:
-
Calendar time. This value counts the number of seconds since the Epoch: 00:00:00 January 1, 1970, Coordinated Universal Time (UTC). (Older manuals refer to UTC as Greenwich Mean Time.) These time values are used to record the time when a file was last modified, for example.
The primitive system data type time_t holds these time values. -
Process time. This is also called CPU time and measures the central processor resources used by a process. Process time is measured in clock ticks, which have historically been 50, 60, or 100 ticks per second. The primitive system data type clock_t holds these time values. (We’ll show how to obtain the number of clock ticks per second with the sysconf function in Section 2.5.4.)
When we measure the execution time of a process, as in Section 3.9, we’ll see that the UNIX System maintains three values for a process:
• Clock time
• User CPU time
• System CPU time
The clock time, sometimes called wall clock time, is the amount of time the process takes to run, and its value depends on the number of other processes being run on the system.
Whenever we report the clock time, the measurements are made with no other activities on the system.
The user CPU time is the CPU time attributed to user instructions. The system CPU time is the CPU time attributed to the kernel when it executes on behalf of the process.
For example, whenever a process executes a system service, such as read or write, the time spent within the kernel performing that system service is charged to the process.
The sum of user CPU time and system CPU time is often called the CPU time.
It is easy to measure the clock time, user time, and system time of any process: simply execute the time(1) command, with the argument to the time command being the command we want to measure. For example:
$ cd /usr/include
$ time -p grep _POSIX_SOURCE /.h > /dev/null
real 0m0.81s
user 0m0.11s
sys 0m0.07s
The output format from the time command depends on the shell being used, because some shells don’t run /usr/bin/time, but instead have a separate built-in function to measure the time it takes commands to run.
In Section 8.17, we’ll see how to obtain these three times from a running process.
The general topic of times and dates is covered in Section 6.10.
曾经,在UNIX系统有两个不同的时间值:
1.日历时间。此值统计自1970年1月1日协调世界时(UTC)00:00:00开始的秒数(较旧的手册将UTC称为格林威治标准时间。)例如,这些时间值用于记录文件上次修改的时间。
基本系统数据类型time_t保存这些时间值。
2.处理机时间。这也称为CPU时间,用于测量进程使用的中央处理器资源。进程时间是以时钟滴答声来度量的,曾经是每秒50、60或100滴答声。原始系统数据类型clock_t保存这些时间值。
当我们测量一个进程的执行时间时,我们将看到UNIX系统为一个进程维护三个时间值:
•时钟时间,有时称为挂钟时间,是进程运行所需的时间量,其值取决于系统上运行的其他进程的数量。
•用户CPU时间,归因于用户指令的CPU时间。
•系统CPU时间,内核代表进程执行时的CPU时间。
1.11 System Calls and Library Functions
All operating systems provide service points through which programs request services from the kernel. All implementations of the UNIX System provide a well-defined, limited number of entry points directly into the kernel called system calls. The system call interface has always been documented in Section 2 of the UNIX Programmer ’s Manual. Its definition is in the C language, no matter which implementation technique is actually used on any given system to invoke a system call.
This differs from many older operating systems, which traditionally defined the kernel entry points in the assembly language of the machine.
所有操作系统都提供服务点,程序通过这些服务点从内核请求服务。UNIX系统的所有实现都直接向内核提供定义良好、数量有限的入口点,称为系统调用。系统调用接口一直记录在UNIX程序员手册的第2节中。它的定义是C语言,不管在任何给定系统上实际使用哪种实现技术来调用系统调用(嘿嘿)。这不同于许多传统意义上用汇编语言做接口的旧的操作系统。
The technique used on UNIX systems is for each system call to have a function of the same name in the standardClibrary. The user process calls this function, using the standardCcalling sequence. This function then invokes the appropriate kernel service, using whatever technique is required on the system. For example, the function may put one or more of the C arguments into general registers and then execute some machine instruction that generates a software interrupt in the kernel.
Section 3 of the UNIX Programmer ’s Manual defines the general-purpose library functions available to programmers. These functions aren’t entry points into the kernel, although they may invoke one or more of the kernel’s system calls. For example, the printf function may use the write system call to output a string, but the strcpy (copy a string) and atoi (convert ASCII to integer) functions don’t involve the kernel at all.
每个系统调用在标准C库函数中都具有相同名称的函数。用户进程使用标准调用序列调用这些函数。然后,该函数使用系统上需要的任何技术调用适当的内核服务。例如,函数可以将一个或多个C参数放入通用寄存器,然后执行一些机器指令,在内核中生成软件中断。
UNIX程序员手册的第3节定义了程序员可用的通用库函数。这些函数不是内核的入口点,尽管它们可以调用内核的一个或多个系统调用。例如,printf函数可以使用write系统调用输出字符串,但是strcpy(复制字符串)和atoi(将ASCII转换为整数)函数根本不涉及内核。
以上是关于趣味英译文:APUE第三版的主要内容,如果未能解决你的问题,请参考以下文章