为什么进程的PID用不透明的数据类型表示? [英] Why PID of a process is represented by opaque data type?

查看:32
本文介绍了为什么进程的PID用不透明的数据类型表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进程的pid定义为pid_t pid;而pid_t是不透明的数据类型.如果进程的ID号可以用int表示,为什么我们不应该将其声明为int族,而不是向用户隐藏其数据类型?

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?

推荐答案

这并不是真正的不透明类型,而是整数类型的别名.例如,在我的系统中,我在不同的头文件中找到以下内容:

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

因此,您说对了, pid_t 只是一个 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:

  • 可读性:明确指出变量将用作pid(维基百科参考).
  • 可维护性:确保将来如有需要,可以更改所有pid变量的类型.例如,如果pid以后需要更宽的数据类型(例如 long int ),则只需更改 typedef ,重新编译,一切就可以正常工作.实际上,我相信这种情况已经在不同的体系结构上发生了.
  • 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.

这篇关于为什么进程的PID用不透明的数据类型表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆