How to determine the maximum pid_t value?

How can I confidently determine the maximum value of type pid_t ? My system does not have a constant PID_MAX.

(Note: I mean the maximum value allowed by the data type, not the actual maximum value that the system will assign to processes.)

Use Case:. I am converting a user-supplied string specification pid to pid_t and want user input not to exceed input capacity. p>

+6
source share
4 answers

POSIX (2008) says :

blksize_t, pid_t and ssize_t must be signed with integer types.

and

An implementation must support one or more programming environments in which the widths blksize_t, pid_t, size_t, ssize_t and suseconds_t are no greater than the width of the long type.

So, you can convert custom strings to long , and then check for overflow for type pid_t with long pid; .. pid == (pid_t)pid long pid; .. pid == (pid_t)pid .

+2
source

What I have done sometimes in the past uses a larger data type, and then when I convert to my smaller type, immediately convert it back to a larger type and make sure the value has not changed.

For example, let's say you used int64_t instead, then you might have something like:

 int64_t my_pid64; /* ... parse string value into my_pid64 ... */ pid_t my_pid = (pid_t) my_pid64; if ((int64_t) my_pid != my_pid64) /* check that value was not out of range of pid_t */ { /* ... handle error ... */ } 

There is not much opportunity to use a larger data type. "long" used to be the largest primitive data type, but this is not true for some common compilers / architectures, even Linux (see comments below). Meanwhile, the intmax_t type has poor library support. As a result, int64_t is sometimes more useful in practice.

But basically your options for a larger data type are probably long, int64_t and intmax_t.

+6
source

Stephen's answer is a good approach.

But if you really want to determine the max pid_t value without relying on undefined behavior, I think your best bet is:

 #include <sys/types.h> #include <limits.h> #include <stdlib.h> static inline pid_t get_max_pid_t() { if (sizeof(pid_t) == sizeof(short)) return SHRT_MAX; if (sizeof(pid_t) == sizeof(int)) return INT_MAX; if (sizeof(pid_t) == sizeof(long)) return LONG_MAX; #if defined(LLONG_MAX) // C99 if (sizeof(pid_t) == sizeof(long long)) return LLONG_MAX; #endif abort(); } 

POSIX ensures that pid_t is a signed integral type. This code assumes that the size of the signed integral type uniquely determines this type. I think this is a great guess, but I'm not sure if this guarantees a standard.

Any worthy compiler will be built-in and constantly extend all this to non-existence, so performance is not a concern.

(Besides this: in C ++ you write std::numeric_limits<pid_t>::max() and do with it.)

+1
source

In one of your headers:

 #ifndef PID_MAX #define PID_MAX INT_MAX // or whatever value you see fit #endif 

you can also do this based on a server / OS dependent definition based on environment variables.

See: Related Posts: Maximum PID on Linux

0
source

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


All Articles