`

读书笔记 -《Advanced Programming in the UNIX Env》- (1)

阅读更多

1. Standards

The proliferation of different versions of Unix during the 1980s has been tempered by the various international standards that were started during the late 1980s. These include the ANSI standard for the C programming language, the IEEE POSIX family (still being developed), and the X/Open portability guide.

 

ANSI C

Please refer to http://en.wikipedia.org/wiki/ANSI_C

 

IEEE POSIX

Please refer to http://en.wikipedia.org/wiki/POSIX

 

X/Open Portability Guide

Please refer to http://en.wikipedia.org/wiki/X/Open

 

2. Architecture of the UNIX operating system

Kernal ------>> System Call(POSIX API) ------>> Shell         ------>> Application

                                                                       ------>> Libraray Routines    ------>> Application

                                                                       ------>> Application    

 

For more vavid diagram, please see the attachment #1.

3. Standard Input, Standard Output, and Standard Error

Unbuffered I/O

Header: unistd.h

 

Constants: STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO

 

Functions:  open, read, write, lseek, and close

 

Standard I/O

Header: stdio.h

 

Constants: stdin, stdout, stderr, EOF

 

Functions: printf, getc and putc

 

4. fork and execlp

  • We call fork to create a new process, which is a copy of the caller. We say that the caller is the parent and that the newly created process is the child. Then fork returns the non-negative process ID of the new child process to the parent, and returns 0 to the child. Because fork creates a new process, we say that it is called onceby the parentbut returns twicein the parent and in the child.

 

  • In the child, we call execlp to execute the command that was read from the standard input. This replaces the child process with the new program file. The combination of a fork, followed by an exec, is what some operating systems call spawning a new process. In the UNIX System, the two parts are separated into individual functions. We'll have a lot more to say about these functions in Chapter 8.

 

  • Because the child calls execlp to execute the new program file, the parent wants to wait for the child to terminate. This is done by calling waitpid, specifying which process we want to wait for: the pid argument, which is the process ID of the child. The waitpid function also returns the termination status of the childthe status variablebut in this simple program, we don't do anything with this value. We could examine it to determine exactly how the child terminated.

 

#include "apue.h" #include <sys/wait.h>
int
main(void)
{
    char    buf[MAXLINE];   /* from apue.h */
    pid_t   pid;
    int     status;
    printf("%% ");  /* print prompt (printf requires %% to print %) */
    while (fgets(buf, MAXLINE, stdin) != NULL) {
        if (buf[strlen(buf) - 1] == "\n")
            buf[strlen(buf) - 1] = 0; /* replace newline with null */
        if ((pid = fork()) < 0) {  /*Fork starts a new process, the new process is a copy of the current process. Then fork returns the non-negative process ID of the new child process to the parent, and returns 0 to the child.*/
            err_sys("fork error");
        } else if (pid == 0) {      /* child */ /*The child process gets the pid == 0, so goes here.*/
            execlp(buf, buf, (char *)0);
            err_ret("couldn't execute: %s", buf); /*If execlp runs successfully, it exit the process. If not, it goes here.*/
            exit(127);
        }
        /* parent */ /*The parent process gets the pid > 0, so goes here.*/
        if ((pid = waitpid(pid, &status, 0)) < 0)
            err_sys("waitpid error");
        printf("%% ");
    }
    exit(0);
} 
 

 

5. Error processing function

char *strerror(int errnum);

This function maps errnum, which is typically the errno value, into an error message string and returns a pointer to the string.

 

void perror(const char *msg);

It outputs the string pointed to by msg, followed by a colon and a space, followed by the error message corresponding to the value of errno, followed by a newline.

 

 

int main(int argc, char *argv[])
{
    fprintf(stderr, "EACCES: %s\n", strerror(EACCES));
    errno = ENOENT;
    perror(argv[0]);
    exit(0);
}

 

6. How to process signal?

 

    int signal(SIGINT, sig_int)

 

    SIGINT: Interupt Signal by Ctrl + C/Delete.

 

    void sig_int(int signo)

 

    sig_int is a function to receive the signal.

 

 

7. Time

Clock 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.

User CPU time

The user CPU time is the CPU time attributed to user instructions.

 

System CPU time

The system CPU time is the CPU time attributed to the kernel when it executes on behalf of the process.

 

8. The relation between System Calls and Library Functions.

For more vavid diagram, please see the attachment #2.

 

  • 描述: This is a vavid diagram to describe the architecture of the UNIX.
  • 大小: 11.8 KB
  • 大小: 9.4 KB
分享到:
评论

相关推荐

    cross-env-7.0.3.zip

    npx cross-env VAR1=value1 VAR2=value2 ``` 4. **高级用法** `cross-env`还支持一些高级特性,如`--shell`选项,可以指定命令行解析器,这对于某些特定场景非常有用。另外,`cross-env`也提供了`--no-shell`...

    yii-advanced-app-2.0.10.tgz

    Yii 2 Advanced 应用程序框架是用于构建大型、复杂的 Web 应用程序的强大工具。这个版本是“yii-advanced-app-2.0.10.tgz”,它是一个压缩包,包含了 Yii 2 框架的高级应用模板,版本号为 2.0.10。这个版本可能包含...

    cross-env-7.0.2.zip

    在Windows系统中,环境变量的设置与Unix或Linux系统有很大区别,cross-env通过抽象这些差异,使得开发者可以编写无须关心平台差异的脚本。 2. **简单易用**:使用cross-env,只需要在npm脚本中添加`cross-env`前缀...

    apache-doris-build-env-for-2.0.a

    doris 编译镜像 apache-doris-build-env-for-2.0

    前端开源库-rollup-plugin-env

    在源代码中,你可以使用`process.env`来访问这些变量,如`process.env.API_URL`。 在运行Rollup构建时,你可以通过命令行参数`--environment`或`-e`来指定当前的环境,比如: ```bash npx rollup -c -e ...

    前端开源库-babel-preset-pob-env

    **前端开源库-babel-preset-pob-env** 前端开发领域中,JavaScript的语法发展迅速,新的语言特性层出不穷。为了确保代码能在不同的浏览器或环境中运行,开发者通常会借助工具进行代码转换,这就是`Babel`的作用。`...

    cross-env-设置环境变量跨平台

    在提供的压缩包文件`kentcdodds-cross-env-739fd62`中,可能包含了`cross-env`项目的源代码。这个版本可能是由kentcdodds维护的一个分支或特定提交。通常,这样的代码包会包含源码、测试文件、配置文件以及可能的...

    env-test.rar_c env

    标题 "env-test.rar_c env" 暗示这是一个与C编程环境测试相关的压缩包,其中包含了一个名为 "env-test.c" 的源代码文件。描述提到 "GLIB - Library of useful routines for C programming",GLIB 是一个流行且广泛...

    前端开源库-is-ali-env

    【前端开源库-is-ali-env】是一个专门为前端开发者设计的开源工具库,其主要功能是检测当前运行环境是否属于阿里巴巴的内部环境。这个库在开发过程中尤其有用,可以帮助开发者判断应用是在阿里内部还是外部环境运行...

    vite-plugin-html-env:一个重写HTML的Vite插件

    vite-plugin-html-env 一个用于重写html的Vite插件用法npm install --save-dev vite-plugin-html-env# oryarn add vite-plugin-html-env -D // vite.config.jsimport VitePluginHtmlEnv from 'vite-plugin-...

    apache-doris-build-env-for-2.0.b

    doris 编译镜像 apache-doris-build-env-for-2.0

    idf-env:idf-env工具有助于设置和管理ESP-IDF安装

    idf-env 维护ESP-IDF环境的工具。 快速开始 在Windows上为ESP板安装串行驱动程序。 在PowerShell中执行以下命令: Invoke-WebRequest 'https://dl.espressif.com/dl/idf-env/idf-env.exe' -OutFile .\idf-env.exe;...

    shargs-example-sync-deepthought-config-env-argv:shargs-example-sync-deepthought-config-env-argv是shargs的示例应用程序

    shargs-example-sync-deepthought-config-env-argv 是的示例应用程序 :shark: 。 有关更多详细信息,请参阅! 设置 $ git clone https://github.com/Yord/shargs-example-sync-deepthought-config-env-argv.git $ ...

    nix-env:我的nix env配置

    1. **Nix和nix-env**:Nix是一个包管理器,提供可重复的构建和部署环境,`nix-env`是其用户界面,用于管理个人环境中的软件包。 2. **Vim配置管理**:通过`git clone`将远程Vim配置仓库克隆到本地,然后使用`install...

    yo-env:通过NODE_ENV更好地构建npm脚本

    通过NODE_ENV更好地构建npm脚本 安装 npm install yo-env 什么? 您的npm脚本的跨平台补充。 如何? // package.json { "scripts": { "start": "yo-env", /* --- invoked when `NODE_ENV=development` (default...

    rollup-plugin-inject-process-env:使用Rollup在process.env中注入环境变量

    在浏览器汇总包中注入process.env环境变量。 为什么 ? 因为通常在一种情况下用rollup-plugin-replace字符串是rollup-plugin-replace : console . log ( process . env . NODE_ENV ) ; ...但并非在所有其他情况...

Global site tag (gtag.js) - Google Analytics