“The /proc file system exposes a range of kernel information to application programs. Each /proc/PID subdirectory contains files and subdirectories that provide information about the process whose ID matches PID. Various other files and directories under /proc expose system-wide information that programs can read and, in some cases, modify.
The uname() system call allows us to discover the UNIX implementation and the type of machine on which an application is running.”
摘录来自: Michael Kerrisk. “The Linux Programming Interface”。 iBooks.
proc文件系统向应用程序揭露了一系列内核信息。每个/proc/PID子目录都包含许多文件和子目录,为进程ID为PID的进程提供相关信息。/proc目录下其它文件和目录揭露了应用程序可以读取在某些情况下甚至修改。
使用uname()系统调用,能够获取UNIX的实现信息以及应用程序锁运行的机器类型。
在阅读Linux/UNIX系统编程手册时,中文部分阅读不明白,在iBooks中阅读英文原著发现竟出奇的好理解。

#include <fcntl.h>
#include "tlpi_hdr.h"

#define MAX_LINE 100

int
main(int argc, char *argv[])
{
    int fd;
    char line[MAX_LINE];
    ssize_t n;

    fd = open("/proc/sys/kernel/pid_max", (argc > 1) ? O_RDWR : O_RDONLY);
    if (fd == -1)
        errExit("open");

    n = read(fd, line, MAX_LINE);
    if (n == -1)
        errExit("read");

    if (argc > 1)
        printf("Old value: ");
    printf("%.*s", (int) n, line);

    if (argc > 1) {
        if (lseek(fd, 0, SEEK_SET) == -1)
            errExit("lseek");

        if (write(fd, argv[1], strlen(argv[1])) != strlen(argv[1]))
            fatal("write() failed");

        system("echo /proc/sys/kernel/pid_max now contains "
               "`cat /proc/sys/kernel/pid_max`");
    }

    exit(EXIT_SUCCESS);
}

该程序展示了如何读取和修改一个/proc/sys/kernel/pid_max文件的内容。如果提供多余两个参数,则用第二个参数取代,如果参数不合法则写入失败,合法性判断是由linux系统所判断,如果只输入./procfs_pidmax则时显示该数值。
我们可以尝试输入 echo invalid > /proc/sys/kernel/pid_max,则会提示-bash: echo: write error: Invalid argument

系统标识

uname系统调用】

功能描述:
获取当前内核名称和其它信息。
用法:

#include <sys/utsname.h>
/* Put information about the system in NAME.  */
extern int uname (struct utsname *__name) __THROW;
参数: 
__name:指向存放系统信息的缓冲区,原型如下
/* Structure describing the system and machine. */
struct utsname
  {
    /* Name of the implementation of the operating system. */
    char sysname[_UTSNAME_SYSNAME_LENGTH];   //当前操作系统名
    /* Name of this node on the network. */
    char nodename[_UTSNAME_NODENAME_LENGTH]; //网络上的名称
    /* Current release level of this implementation. */
    char release[_UTSNAME_RELEASE_LENGTH];   //当前发布级别
    /* Current version level of this release. */
    char version[_UTSNAME_VERSION_LENGTH];   //当前发布版本
    /* Name of the hardware type the system is running on. */
    char machine[_UTSNAME_MACHINE_LENGTH];   //当前硬件体系类型
  };
  ``` 

``` C
/*************************************************************************\


*                  Copyright (C) Michael Kerrisk, 2015.                   *
*                                                                         *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the   *
* Free Software Foundation, either version 3 or (at your option) any      *
* later version. This program is distributed without any warranty.  See   *
* the file COPYING.gpl-v3 for details.                                    *

\*************************************************************************/

/* Listing 12-2 */

/* t_uname.c

   Demonstrate the use of the uname() system call, which returns various
   identifying information about the system.
*/
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include <sys/utsname.h>
#include "tlpi_hdr.h"

int
main(int argc, char *argv[])
{
    struct utsname uts;

    if (uname(&uts) == -1)
        errExit("uname");

    printf("Node name:   %s\n", uts.nodename);
    printf("System name: %s\n", uts.sysname);
    printf("Release:     %s\n", uts.release);
    printf("Version:     %s\n", uts.version);
    printf("Machine:     %s\n", uts.machine);
#ifdef _GNU_SOURCE
    printf("Domain name: %s\n", uts.domainname);
#endif
    exit(EXIT_SUCCESS);
}

程序比较简单,我们可以man uname 参考手册中的内容与程序输出进行比较。