Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PID of a process is represented by opaque data type?

Tags:

linux

process

The pid of a process is defined as pid_t pid; whereas, pid_t is an opaque data type. If the process's id number can be represented by an int, why should not we declare it as an int family rather then hiding its data type from its users?

like image 490
ChandanK Avatar asked Feb 14 '12 09:02

ChandanK


1 Answers

That's not really an opaque type, but an alias to an integer type. For example, in my system, I find the following in different header files:

typedef __pid_t pid_t;
...
# define __STD_TYPE     typedef
__STD_TYPE __PID_T_TYPE __pid_t;    /* Type of process identifications.  */
...
#define __PID_T_TYPE        __S32_TYPE
...
#define __S32_TYPE      int

Hence, you're right in that pid_t is just an int. However, I'd say there are a couple of reasons to do this:

  • Readability: make clear that a variable is going to be used as a pid (wikipedia reference).
  • Maintainability: make sure that the type of all pid variables can be changed in the future if needed. For example, if pids need a wider data type later (such as long int), you just need to change the typedef, recompile and everything should work fine. In fact, I believe this already happens for different architectures.
like image 199
jcollado Avatar answered Oct 21 '22 05:10

jcollado