proc file system

Understanding the Linux Filesystem Hierarchy Standard (FHS) | LinkedIn

The /proc file system is a virtual file system in Linux that provides detailed information about the system's hardware, processes, and other runtime information. It is a dynamically generated file system that exists only in memory and allows processes to read and write information about themselves and other processes.

In Linux, the virtual file system (VFS) is a layer that sits on top of the actual file system and presents a unified interface for interacting with various file systems. It provides a way for different file systems to coexist on the same system without requiring application developers to worry about the specific details of each file system.

The proc file system is a virtual file system that is created on-the-fly by the kernel and contains information about the current state of the system. The contents of the proc file system are not stored on disk but are generated dynamically by the kernel when they are accessed. The proc file system provides a convenient way to access various kernel data structures and system information, including process information, hardware configuration, and network statistics, among others.

The proc file system is created as a virtual file system because its contents are generated dynamically and do not correspond to any physical storage device. Instead, the proc file system is a way for the kernel to expose various system data structures and information to user-space programs in a convenient and standardized manner. This allows system administrators and developers to easily access and manipulate system information without having to write custom kernel modules or drivers.

The /proc file system provides a variety of information about the system, including information about processes, kernel modules, network connections, and hardware information such as CPU and memory usage. Each directory in the /proc file system represents a specific system resource, and the files within the directory contain information about that resource.

One of the most important directories in the /proc file system is /proc/[pid], which provides information about individual processes running on the system. Each running process is represented by a directory with a name that corresponds to its process ID (PID). Within each process directory, there are several files that contain information about the process, including its status, memory usage, and file descriptors.

Another important directory in the /proc file system is /proc/sys, which provides access to system configuration and runtime parameters. This directory contains files that can be used to adjust various system settings, such as the maximum number of open files or the size of the file cache.

The /proc file system is a powerful tool for system administrators and developers who need to monitor and debug their systems. It provides detailed information about the system's inner workings that can be used to diagnose problems and optimize system performance. However, it is important to use the /proc file system with caution, as it contains sensitive information that can be accessed by unauthorized users.

When a new process is created in Linux, it gets assigned a new process ID (PID) and a new directory is created for it in the /proc filesystem with the name /proc/<pid>. This directory contains a number of files and subdirectories that provide information about the process, such as its status, memory usage, open files, and more.

The /proc/<pid>/status file contains information about the process, including its PID, parent PID, state, memory usage, and more. This file can be useful for monitoring the state of the process and diagnosing issues.

In addition to the /proc/<pid>/status file, there are several other files in the /proc/<pid> directory that provide information about the process. For example, the /proc/<pid>/cmdline file contains the command-line arguments that were used to start the process, and the /proc/<pid>/fd directory contains information about the process's open file descriptors.

When a process terminates, its directory is removed from the /proc filesystem. This means that any information about the process that was stored in the /proc filesystem will be lost.

The content of the /proc file system is mainly made up of virtual files that contain information about the system, including processes, hardware, and kernel configuration. Here are some of the most significant files and their content:

  1. /proc/cpuinfo: This file contains information about the CPU(s) installed on the system, including its architecture, vendor, model name, and clock speed.
  2. /proc/meminfo: This file contains information about the system's memory usage, including the total amount of memory, free memory, and used memory.
  3. /proc/mounts: This file contains a list of all mounted file systems on the system, including their device name, mount point, and file system type.
  4. /proc/net: This directory contains information about network protocols and connections, including TCP, UDP, and raw sockets.
  5. /proc/sys: This directory contains information about system settings, including kernel parameters and configuration options.
  6. /proc/<pid>: This directory contains information about each process running on the system, identified by its process ID (pid). It includes files such as cmdline (command line arguments), status (current state of the process), and mem (memory usage).

The significance of the /proc file system is that it provides a way for users and system administrators to access and modify system information dynamically without needing to reboot the system. This makes it a powerful tool for monitoring and managing system resources, debugging performance issues, and troubleshooting problems.

Who monitors the process creation and deletion?

Daemon process called kthread in the Linux kernel that listens to new process creation and deletion events. It is implemented as a kernel thread that runs in the background and continuously monitors the process list. When a new process is created or deleted, the kthread daemon is notified and it updates the necessary data structures in the kernel to reflect the changes.

The kthread daemon is implemented using the kernel's task_struct data structure, which represents a process in the system. The kthread daemon is scheduled to run periodically by the kernel's scheduler, and it checks the process list to detect any changes. When a new process is created, the kernel initializes the task_struct data structure for the new process and adds it to the process list. Similarly, when a process is deleted, the kthread daemon removes the corresponding task_struct data structure from the process list.

The kthread daemon plays an important role in maintaining the consistency and integrity of the process list and other related data structures in the kernel. It ensures that the kernel always has an up-to-date view of the processes running in the system, which is crucial for many system-level tasks and utilities.

kthreads can update the proc file system. In fact, many kernel threads in Linux are responsible for updating the proc file system, including the kthread that monitors new process creations and deletions.

When a new process is created or an existing process is deleted, the kernel generates an event and notifies the kernel thread responsible for monitoring process events. This thread then updates the relevant entries in the proc file system to reflect the changes.

The update is typically done by modifying the in-memory representation of the proc file system, which is maintained by the kernel. The modified data is then written back to the appropriate files in the proc file system as needed.

The exact details of how the update is handled can vary depending on the specific implementation of the kernel and the version of the operating system being used. However, in general, the kernel employs a combination of locking mechanisms, data structures, and algorithms to ensure that updates to the proc file system are handled correctly and efficiently.

the ps and tree, top commands are implemented by reading information from the /proc file system. The /proc file system provides a way for processes to access information about themselves and other processes in the system.

When you run the ps command, it reads information about running processes from the /proc file system and displays it in a user-friendly format. Similarly, the tree command also reads information from the /proc file system to display a hierarchical view of processes.

Both commands use the process ID (PID) and parent process ID (PPID) information from the /proc file system to construct the process tree. They also read other information such as the process state, command line arguments, and memory usage from various files in the /proc/<pid> directory.

The top command is an interactive process monitor that continuously updates information about the system's processes in real-time. It is implemented using the same mechanism as ps command, which is by reading the information from the proc filesystem.

However, top uses a different approach compared to ps command. Top runs in interactive mode and refreshes the displayed process list at a set interval, typically every few seconds. The process list is then sorted based on various criteria such as CPU usage, memory usage, process name, etc.

Top command continuously monitors the system's performance and displays information about CPU usage, memory usage, process IDs, process names, and other system information in real-time. The user can interact with the command by entering keyboard commands to change the sort order or filter the displayed processes.

Meaning of command usind for ps -elf :

  • UID: User ID of the process owner.
  • PID: Process ID.
  • PPID: Parent process ID.
  • C: Processor utilization percentage.
  • STIME: Start time of the process.
  • TTY: Terminal or pseudo-terminal associated with the process.
  • TIME: Total CPU time used by the process.
  • CMD: Command used to start the process.

The top command is used to display real-time information about the system's performance and the processes running on it. The meaning of each field in the default view is as follows:

  • PID: Process ID.
  • USER: User who owns the process.
  • %CPU: CPU usage percentage.
  • %MEM: Memory usage percentage.
  • TIME+: CPU time used by the process.
  • COMMAND: Command used to start the process.

Both ps and top commands display information about running processes, but top provides a real-time view of the system's performance and process usage. The fields in each command can be customized to display additional information as well.

Thanks for reading till end, please comment in the comment section if you have any!

要查看或添加评论,请登录

Amit Nadiger的更多文章

  • Rust modules

    Rust modules

    Referance : Modules - Rust By Example Rust uses a module system to organize and manage code across multiple files and…

  • List of C++ 17 additions

    List of C++ 17 additions

    1. std::variant and std::optional std::variant: A type-safe union that can hold one of several types, useful for…

  • List of C++ 14 additions

    List of C++ 14 additions

    1. Generic lambdas Lambdas can use auto parameters to accept any type.

    6 条评论
  • Passing imp DS(vec,map,set) to function

    Passing imp DS(vec,map,set) to function

    In Rust, we can pass imp data structures such as , , and to functions in different ways, depending on whether you want…

  • Atomics in C++

    Atomics in C++

    The C++11 standard introduced the library, providing a way to perform operations on shared data without explicit…

    1 条评论
  • List of C++ 11 additions

    List of C++ 11 additions

    1. Smart Pointers Types: std::unique_ptr, std::shared_ptr, and std::weak_ptr.

    2 条评论
  • std::lock, std::trylock in C++

    std::lock, std::trylock in C++

    std::lock - cppreference.com Concurrency and synchronization are essential aspects of modern software development.

    3 条评论
  • std::unique_lock,lock_guard, & scoped_lock

    std::unique_lock,lock_guard, & scoped_lock

    C++11 introduced several locking mechanisms to simplify thread synchronization and prevent race conditions. Among them,…

  • Understanding of virtual & final in C++ 11

    Understanding of virtual & final in C++ 11

    C++ provides powerful object-oriented programming features such as polymorphism through virtual functions and control…

  • Importance of Linux kernal in AOSP

    Importance of Linux kernal in AOSP

    The Linux kernel serves as the foundational layer of the Android Open Source Project (AOSP), acting as the bridge…

    1 条评论