TLPI-Chapter 12系統和進程信息

“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.
*/




<div class="se-preview-section-delimiter"></div>

#ifdef __linux__




<div class="se-preview-section-delimiter"></div>

#define _GNU_SOURCE




<div class="se-preview-section-delimiter"></div>

#endif




<div class="se-preview-section-delimiter"></div>

#include <sys/utsname.h>




<div class="se-preview-section-delimiter"></div>

#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);




<div class="se-preview-section-delimiter"></div>

#ifdef _GNU_SOURCE
    printf("Domain name: %s\n", uts.domainname);




<div class="se-preview-section-delimiter"></div>

#endif
    exit(EXIT_SUCCESS);
}

程序比較簡單,我們可以man uname 參考手冊中的內容與程序輸出進行比較。

發佈了128 篇原創文章 · 獲贊 40 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章