Are process IDs non-negative on Linux?

I implement syscall, which is called in user space, say foo. Syscall calls foo task_struct (through the current current pointer), prints its name and pid, then goes to the parent process foo, the parent parent foo, etc. Prints all its names and pids up to the init process. >

pid = 1 is reserved for init, pid = 0 is reserved for swapper.

According to swapper task_struct the parent process itself.

Swapper (or sched) always has pid = 0 and is always the parent init process?

Are all pids non-negative? Is it normal for me to make this assumption?

+6
source share
3 answers

To more accurately answer your questions:

  • IBM The Linux boot process describes the swapper process because the process has a PID value of 0. At startup, this process starts /sbin/init (or another process specified as a parameter by the bootloader), which will usually be a process with PID 1.
  • On Unix systems, PID values ​​are allocated sequentially from the first process to the maximum value specified by /proc/sys/kernel/pid_max . Therefore, you can safely rely on the fact that all valid PIDs have a positive value, while negative values ​​are reduced to error values, etc.
  • A good idea would also be to consider zombie processes, as they can receive a “special treatment” in the process tree when / if they are accepted by init.
+4
source

It is always positive or 0. Kernel sources determine its type pid_t , which is considered afaik unknown (although it is defined as signed to be able to make calls like fork return negative numbers in case of errors).

+1
source

Yes, they are in fact always positive.

You can verify this with a few POSIX system calls, such as wait, that use negative values ​​to represent things like all native child processes or the like, and only positive values ​​are valid PIDs.

Example: http://linux.die.net/man/2/wait

0
source

Source: https://habr.com/ru/post/910074/


All Articles