referer: http://osxbook.com/book/bonus/chapter11/procfs/

Processes as Files

The process file system ("procfs" for brevity, or simply "/proc", because that's where it is usually mounted) has become a common entity on Unix-like systems. For example, Solaris, Linux, and the various modern BSDs all have procfs. In general, procfs uses the file metaphor to provide both a view of currently running processes and perhaps an interface to control them. However, some procfs implementations export so many types of information that they are nothing short of the proverbial kitchen sink. Implementations can also differ in the format they use to export such information. For example, Solaris procfs favors the binary format philosophy, assuming that developers will write interesting user-space tools that use that "raw" information. In contrast, Linux favors formatted text to export information through procfs, allowing end users and administrators to simply view that information using familiar file utilities.

Tom J. Killian created the first implementation of a process file system. It was for Eighth Edition UNIX. See T. J. Killian, "Processes as Files," USENIX Summer Conference Proceedings, Salt Lake City, UT, USA (June 1984).

/proc on Mac OS X (not)

Mac OS X does not provide a process file system. It does provide alternative interfaces such as sysctl(3) and the now obsolete kvm(3). The sysctl(3) interface provides read and write access to a management information base (MIB) whose contents are various categories of kernel information, such as information related to processes, file systems, virtual memory, networking, and debugging in general. When the /dev/kmem device is available, the kvm(3) interface provides access to raw kernel memory. Besides, the I/O Kit programming interfaces and tools such as ioreg and IORegistryExplorer.app allow user-space inspection of a variety of kernel information on Mac OS X.

The sysctl() system call was introduced in 4.4BSD as a safe, reliable, and portable (across kernel versions) way to perform user-kernel data exchange.

In 2003, shortly after I was introduced to Mac OS X, I began a quick-and-dirty implementation of procfs for Mac OS X. Meant as nothing more than a "random experiment", my approach was to take procfs and pseudofs source from FreeBSD and morph them to work on Mac OS X. Shortly after I had the thing mounting, I ran into a kernel panic. (Writing in-kernel file systems is very conducive to this behavior.) Before I could begin two-machine debugging, one of the two Macintosh computers I had access to had catastrophic hardware failure. Apple gave me extremely poor repair experience (taking weeks to fix one problem and returning the machine with something else broken; repeat this several times over). So much so that by the time the machine finally worked, I had no interest left in debugging procfs for Mac OS X.

A MacFUSE-Based procfs

Fast forward to late 2006. I was wrapping up MacFUSE, a Mac OS X implementation of the FUSE (File System in User Space) mechanism. The first Mac OS X-specific example file system I wrote for MacFUSE was procfs—this time as a user-space file system, of course. I've been meaning to release it as an open source MacFUSE example, but didn't find the time to package it up until now.

The MacFUSE version of procfs makes heavy use of the Mach programming interfaces. Moreover, the implementation uses a set of macros that depend on the C++ version of the pcre (Perl Compatible Regular Expressions) library. The macros are meant to make it easier to extend the file system.

Detailed information on understanding and using the Mach interfaces used by procfs can be found in the book Mac OS X Internals. In particular, refer to Chapters 6 (The xnu Kernel), 7 (Processes), 8 (Virtual Memory), and 9 (Interprocess Communication).

Compiling and Using procfs

Since procfs depends on the pcre libraries, you will first need to download, compile, and install pcre. Note that if you install pcre in a location other than /usr/local/, you will need to modify the Makefile in procfs source. The Makefilealso assumes that you have the MacFUSE libraries installed under /usr/local/.

$ tar -jxvf pcre-<version>.tar.bz2 ... $ cd pcre-<version> $ CFLAGS="-O -g -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk" \ CXXFLAGS="-O -g -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk" \ LDFLAGS="-arch i386 -arch ppc" \ ./configure --prefix=/usr/local --disable-dependency-tracking $ make $ sudo make install ...

Assuming you have the MacFUSE Core package installed and have the MacFUSE source tree checked out under /work/macfuse/, you can compile procfs as follows.

$ cd /work/macfuse/filesystems/procfs $ make ...

Having successfully compiled procfs, you can mount it, say, on the /proc directory, as follows.

$ sudo mkdir /proc $ sudo chown root:wheel /proc $ sudo ./procfs /proc $ mount ... procfs on /proc (read-only, synchronous)

Now you can explore the process file system on Mac OS X.

$ ls -F /proc 0/ 151/ 212/ 35/ 47/ 1/ 165/ 215/ 36/ 51/ 1166/ 187/ 218/ 38/ 5338/ 1187/ 194/ 221/ 39/ 54/ 1196/ 195/ 23/ 3954/ 59/ 1216/ 196/ 27/ 3976/ 62/ 131/ 200/ 28807/ 40/ 64/ 140/ 201/ 28902/ 42/ 67/ 145/ 202/ 32/ 43/ 867/ 146/ 204/ 33/ 45/ 95/ 15094/ 206/ 34/ 46/ hardware/

There is a directory under /proc for each process on the system, with the numeric process ID (pid) being the directory name. pid 0 corresponds to the kernel, pid 1 is launchd, and so on.

$ cd /proc/867 $ ls -l total 0 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 . 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 .. 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 carbon 0 -r--r--r-- 1 root wheel 0 May 7 23:44 cmdline 0 -r--r--r-- 1 root wheel 0 May 7 23:44 jobc 0 -r--r--r-- 1 root wheel 0 May 7 23:44 paddr 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 pcred 0 -r--r--r-- 1 root wheel 0 May 7 23:44 pgid 0 -r--r--r-- 1 root wheel 0 May 7 23:44 ppid 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 task 0 -r--r--r-- 1 root wheel 0 May 7 23:44 tdev 0 -r--r--r-- 1 root wheel 0 May 7 23:44 tpgid 0 dr-xr-xr-x 1 root wheel 0 May 7 23:44 ucred 0 -r--r--r-- 1 root wheel 0 May 7 23:44 wchan

For a given pid, the /proc/pid/ directory has some files containing certain BSD-level information. The carbon/ subdirectory contains additional information for processes that are also Carbon processes. The pcred/ and ucred/subdirectories contains files exporting process and user credentials, respectively.

$ pwd /proc/867 $ ls -F carbon name psn $ cat carbon/name carbon/psn Safari 0:12451841 $ cat cmdline /Applications/Safari.app/Contents/MacOS/Safari -psn_0_12451841 $ ls -F ucred groups uid $ cat ucred/groups ucred/uid 501(singh) 81(appserveradm) 79(appserverusr) 80(admin) 501(singh)

The task/ subdirectory contains a variety of Mach task information: scheduling data, virtual memory regions, Mach ports, threads, register state, and so on.

$ pwd /proc/867 $ cd task $ ls -F absolutetime_info/ ports/ tokens/ basic_info/ role vmmap events_info/ thread_times_info/ mach_name threads/ $ cat role BACKGROUND_APPLICATION $ cat vmmap 00000000-00001000 4K ---/--- COPY - DEFAULT uwir=0 sub=0 00001000-000dc000 876K r-x/rwx COPY - DEFAULT uwir=0 sub=0 ... 15a54000-15ada000 536K r--/rwx COPY - DEFAULT uwir=0 sub=0 15ade000-15ede000 4096K rw-/rwx COPY - DEFAULT uwir=0 sub=0 $ ls -F basic_info policy suspend_count user_time resident_size system_time virtual_size $ ls -F events_info cow_faults messages_received syscalls_mach csw messages_sent syscalls_unix faults pageins $ ls -F ports 1003/ 1f03/ 2f03/ 4003/ 507/ 6423/ 7b03/ 8e03/ a887/ d093/ ... 1e03/ 2e03/ 3f03/ 5003/ 62b7/ 79f7/ 8d03/ a7c7/ d03/ $ ls -F ports/1003 msgcount qlimit seqno sorights task_rights $ cat ports/1003/task_rights RECEIVE $ ls -F threads 7b03/ 7c03/ 7d03/ 7e03/ 7f03/ 8003/ 8103/ 8203/ $ ls -F threads/7b03 basic_info/ states/ $ ls -F threads/7b03/basic_info cpu_usage policy sleep_time system_time flags run_state suspend_count user_time $ ls -F threads/7b03/states debug/ exception/ float/ thread/ $ ls -F threads/7b03/states/thread cs eax ebx edi eflags es esp gs ds ebp ecx edx eip esi fs ss $ cat threads/7b03/states/thread/esp bfffef6c

The hardware/ subdirectory contains subdirectories for several pieces of hardware on the machine, allowing certain hardware data to be read from virtual files.

In a subsequent version of procfs, the /hardware/ subdirectory was moved to appear under the /system/ subdirectory, which in turn now contains hardware/ and firmware subdirectories.

$ ls -F /proc/hardware cpus/ lightsensor/ motionsensor/ mouse/ tpm/ $ ls -F /proc/hardware/cpus 0/ 1/ $ ls -F /proc/hardware/cpus/0 data $ cat /proc/hardware/cpus/0/data slot 0 (master), running type 7, subtype 4 6751211 ticks (user 91036 system 141938 idle 6517257 nice 980) cpu uptime 18h 45m 12s

In particular, if the machine has a sudden motion sensor or an ambient light sensor, you can retrieve "live" readings from these devices through procfs files.

$ cat /proc/hardware/motionsensor/data -7 -1 0 $ cat /proc/hardware/motionsensor/data -37 -1 0 $ cat /proc/hardware/lightsensor/data 839 895 $ cat /proc/hardware/lightsensor/data 49 893

Compatibility

Although there's no universal standard for either the hierarchy of file system objects in procfs or the contents of these objects, some implementations are rather popular because of the prevalence of their underlying operating systems. For example, FreeBSD contains linprocfs, a process file system that emulates a subset of Linux procfs. linprocfs was necessary for Linux binary emulation to work completely. This procfs implementation doesn't attempt to conform to any other implementation. For somebody with enough determination and time, it could be an interesting project to create a Linux-compatible (or Solaris-compatible, or whatever) version of MacFUSE-based procfs for Mac OS X.

A MacFUSE-Based Process File System for Mac OS X的更多相关文章

  1. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  2. 让Mac OS X中的PHP支持GD

    GD库已经是近乎于是现在主流PHP程序的标配了,所以也必须让Mac OS X中的PHP支持GD.在网上搜索了好多,最终按照这个方式成功实现,如何让Mac OS X支持PHP,请查看<让PHP跑在 ...

  3. Mac OS X 下安装使用 Docker

    它依赖于 LXC(Linux Container),能从网络上获得配置好的 Linux 镜像,非常容易在隔离的系统中运行自己的应用.也因为它的底层核心是个 LXC,所以在 Mac OS X 下需要在 ...

  4. Mac OS 下安装mysql环境

    传送门:Mac下安装与配置MySQL   mac 上怎么重置mysql的root的密码? 一.下载mysql 进入官方下载地址:https://www.mysql.com/downloads/ 1.找 ...

  5. 5105 pa3 Distributed File System based on Quorum Protocol

    1 Design document 1.1 System overview We implemented a distributed file system using a quorum based ...

  6. filebench - File system and storage benchmark - 模拟生成各种各样的应用的负载 - A Model Based File System Workload Generator

    兼容posix 接口的文件系统中我们不仅要测试 posix 接口是否兼容.随机读,随机写,顺序读,顺序写等读写模式下的性能.我们还要测试在不同工作负载条件下的文件系统的性能的情况:Filebench ...

  7. 部署ActiveMQ的Share File System Master-Slave

    之前在项目里用MQ是用单节点,因为业务量不大没有主从.这样风险很大,会有单点问题.新项目起来了,需要一个高可用的MQ,故研究了下AMQ的几种master-slave方式: 1.Pure Master- ...

  8. Supervisor: A Process Control System

    Supervisor: 进程控制系统 概述:Supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序. 它与launch ...

  9. Sharing The Application Tier File System in Oracle E-Business Suite Release 12.2

    The most current version of this document can be obtained in My Oracle Support Knowledge Document 13 ...

随机推荐

  1. MySQL数据库 介绍,安装,基本操作

    - 数据库介绍: 1.随意存放在一个文件中的数据,数据的格式千差万别 tank|123 jason:123 sean~123 2.软件开发目录规范 - Project: - conf - bin - ...

  2. Django项目中出现的错误及解决办法(ValueError: Dependency on app with no migrations: customuser)

    写项目的时候遇到了类似的问题,其实就是没有生成迁移文件,执行一下数据库迁移命令就好了 ValueError: Dependency on app with no migrations: customu ...

  3. leetcode2. 两数相加

    使用迭代的方式 class Solution{ public: ListNode *addTwoNumbers(ListNode* l1,ListNode *l2) { ListNode *res=) ...

  4. Python面向对象 | 鸭子方法

    鸭子类型 如果看起来像.叫声像而且走起路来像鸭子,那么它就是鸭子’.python程序员通常根据这种行为来编写程序.例如,如果想编写现有对象的自定义版本,可以继承该对象,也可以创建一个外观和行为像,但与 ...

  5. jq form表单渲染单选框内容渲染

    单选框赋值 单选按钮赋值的主要就在于一个value值和name值对应,但是单选的选中状态按钮是input标签的check(选中)属性 当newattr的属性是1的时候为true,或者当newattr的 ...

  6. flask回顾

    pip install flask from flask import Flask app = Flask(__name__) # 命令行启动,用manager,访问会变的非常慢 pip instal ...

  7. 6-ESP8266 SDK开发基础入门篇--操作系统入门使用

    了解了8266的串口了,这一节咱就自己写程序,处理一下数据,如果接收到 0xaa 0x55 0x01  就控制指示灯亮 0xaa 0x55 0x00  就控制指示灯灭 注意哈,我是用的假设没有操作系统 ...

  8. 【CFGym102059G】Fascination Street(思维DP)

    点此看题面 大致题意: 有\(n\)个路灯,每个路灯有一定的建造费用,且建成后可照亮自身及周围距离为\(1\)的两个格子.你可以交换\(k\)次两个路灯的建造费用,求照亮所有格子的最小费用. 题意转换 ...

  9. Spring Cloud Gateway 之 AddRequestHeader GatewayFilter Factory

    今天我们来学习下GatewayFilter Factory,中文解释就是过滤器工厂. 官方文档对GatewayFilter Factory的介绍: Route filters allow the mo ...

  10. LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (最少翻转次数将二进制矩阵全部置为0)

    给一个矩阵mat,每个格子都是0或1,翻转一个格子会将该格子以及相邻的格子(有共同边)全部翻转(0变为1,1变为0) 求问最少需要翻转几次将所有格子全部置为0. 这题的重点是数据范围,比赛结束看了眼数 ...