3

I'm reading the /proc directory (or pseudo-fs) to find all processes. I'm getting the information I need from /proc/[pid]/status but there's something else I need. Is there any way to figure out which processes are critical to system? for example using parent-pid or UID of the process?

By system process, I mean processes that would otherwise exist on a fresh installation of the OS, and before installing any application or services. This might not mean kernel threads, or system processes at all, but to sum it up, I mean processes, that their termination, would disrupt the fundamental structure of the system.

PS. I'm working on an android app, but since this part is done using pure Linux file system I asked it here and I don't suppose that there would be any different.

2
  • Peep this stackoverflow.com/questions/12213445/identifying-kernel-threads
    – iyrin
    Commented Mar 21, 2015 at 11:11
  • 3
    I believe the term "system process" is ambiguous. I assumed this included processes such as kernel threads, but it often refers to any daemon or background process. For the latter, yes you can see if a process is a background process, but that didn't sound like what you are asking. Could you be more specific?
    – iyrin
    Commented Mar 21, 2015 at 12:01

3 Answers 3

6

If you have htop you can press Shift+k to toggle the display of kernel threads. If you press F5 for tree mode, they should all appear as children of kthreadd.

There are some visible differences between a kernel thread and a user-space thread:

  • /proc/$pid/cmdline is empty for kernel threads - this is the method used by ps and top to distinguish kernel threads.

  • The /proc/$pid/exe symbolic link has no target for kernel threads - which makes sense since they do not have a corresponding executable on the filesystem.

More specifically, the readlink() system call returns ENOENT ("No such file or directory"), despite the fact that the link itself exists, to denote the fact that the executable for this process does not exist (and never did).

Therefore, a reliable way to check for kernel threads should be to call readlink() on /proc/$pid/exe and check its return code. If it succeeds then $pid is a user process. If it fails with ENOENT, then an extra stat() on /proc/$pid/exe should tell apart the case of a kernel thread from a process that has just terminated.

  • /proc/$pid/status is missing several fields for most kernel threads - more specifically a few fields related to virtual memory.

The Above answer from Identifying kernel threads

Another way to distinguish kernel threads from other process is to run top -c. From the top manual:

3. COMMAND -- Command Name or Command Line
Display the command line used to start a task or the name of the associated program. You toggle between command line and name with 'c', which is both a command-line option and an interactive com‐ mand.

When you've chosen to display command lines, processes without a command line (like kernel threads) will be shown with only the program name in brackets, as in this example:
[ mdrecoveryd ]

Running ps aux also displays processes that were launched without a command in square brackets ( and will have an empty /proc/[pid]/cmdline file ).

Example:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        19  0.0  0.0      0     0 ?        S<   Mar02   0:00 [kworker/1:0H] 

See package procps-3.2.8 file /proc/readproc.h.

// Basic data structure which holds all information we can get about a process.
// (unless otherwise specified, fields are read from /proc/#/stat)
//
// Most of it comes from task_struct in linux/sched.h
8
  • Does this mean that kernel threads have a pid in /proc ?
    – Milad.N
    Commented Mar 21, 2015 at 11:21
  • They do have a pid as can be seen in htop or top or ps.
    – iyrin
    Commented Mar 21, 2015 at 11:28
  • So far so good. There's only one thing left. As I mentioned I want to determine that by reading the info in /proc, and as far as I know, top gets (most of) its info from there too. How does top determine the value for command?
    – Milad.N
    Commented Mar 21, 2015 at 11:59
  • I've been able to view the name in the status such as cat /proc/32673/status for the pid of 32673 which is a kworker.
    – iyrin
    Commented Mar 21, 2015 at 12:44
  • 1
    Thanks for the edit. Yes, the lack of an executed command seems to be one of the keys in determining what type of thread it is.
    – iyrin
    Commented Mar 23, 2015 at 7:24
1

You cannot. “System process” is not a well-defined notion. “Critical to system” is not a universal, yes-or-no property.

I'm writing this post on a desktop PC. It has Apache installed, but it is not “critical to system” on this machine — I only use it occasionally to test things. On the other hand, on a public- or enterprise-facing web server, Apache would be essential. Conversely, an X server is not critical on most servers, but on a workstation, it's essential.

There is no shortcut. If you want to know whether killing a process will break anything, you need to understand what the process is doing. If you don't know what a process is doing, assume that it is critical.

“Processes that would otherwise exist on a fresh installation of the OS, and before installing any application or services” is not a well-defined concept either. Services may be critical on a particular system even if they aren't part of the default installation (e.g. Apache). Conversely, services may be included in the default installation but not be critical on a particular system (e.g. a network management service on a system with no network connection).

On Android, which is not a Linux system (it's a different system using the Linux kernel), you can call anything running off /system a “system process”. This definition is somewhat meaningful on Android, unlike Unix, because it clearly separates the base system from user-installed applications. The base system includes preinstalled apps (phone, Gmail, etc.), with a different selection depending on the phone vendor (manufacturer or network operator). A twist to this definition is that apps running off /data but for which an entry in /system/app exists are also “system” apps by this definition, just ones that have been upgraded.

1
  • Your last paragraph had a really good point. I tried to read the /proc/[pid]/exe symlink, and see if that could be used, but unfortunately for any other process than the process of my own app, it requires root permissions.
    – Milad.N
    Commented Mar 22, 2015 at 7:44
-2

Yes, in /proc/[pid]/status there is UID entry. If entry is equal to:

UID:    0    0    0    0
UID: real UID, effective UID, saved set UID, file system UID

that means process belongs to root.

Furthermore most root processes have parent pid(PPid) in range from 0 to 2.

12
  • Thanks, could you please indicate what each column means?
    – Milad.N
    Commented Mar 21, 2015 at 10:45
  • @goldilocks Yes, there is no pid 0. But if PPid = 0 that means pid has no parent pid therefore it is system process. from ps -fe: root 1(pid) 0(ppid) .... /sbin/init
    – zuberuber
    Commented Mar 21, 2015 at 10:54
  • There is only one process with a ppid of 0, and that is init, because it is the only process without a parent. All other processes are descended from it and have a ppid >= 1.
    – goldilocks
    Commented Mar 21, 2015 at 10:57
  • 1
    Hence the title of the question, "Check if a process, is a system process". I understand that the OP doesn't understand and means this in an ambiguous way that could include kernel threads. However, you are not explaining things properly. If you want to refer to kernel threads, refer to kernel threads, but don't refer to them as processes, because this dilutes the more specific meaning of the word, which is important to understanding the system -- which is what the OP is trying to do.
    – goldilocks
    Commented Mar 21, 2015 at 11:13
  • 1
    If someone asks "Which floor of the building is Cleveland on?", you do not say, "You can see it from the 11th floor." You say, "Cleveland is not that kind of thing". The only critical entities are the kernel and init. Everything else is arbitrary and optional. If that's a "non answer", I give up. Believe whatever.
    – goldilocks
    Commented Mar 21, 2015 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .