3 * Copyright (c) International Business Machines Corp., 2009
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * get_max_pids(): Return the maximum number of pids for this system by
23 * reading /proc/sys/kernel/pid_max
25 * get_free_pids(): Return number of free pids by subtracting the number
26 * of pids currently used ('ps -eT') from max_pids
32 #include <sys/types.h>
37 int get_max_pids(void)
44 f
= fopen("/proc/sys/kernel/pid_max", "r");
46 tst_resm(TBROK
, "Could not open /proc/sys/kernel/pid_max");
49 if (!fgets(buf
, BUFSIZE
, f
)) {
51 tst_resm(TBROK
, "Could not read /proc/sys/kernel/pid_max");
62 int get_free_pids(void)
65 int rc
, used_pids
, max_pids
;
67 f
= popen("ps -eT | wc -l", "r");
69 tst_resm(TBROK
, "Could not run 'ps' to calculate used "
73 rc
= fscanf(f
, "%i", &used_pids
);
76 if (rc
!= 1 || used_pids
< 0) {
77 tst_resm(TBROK
, "Could not read output of 'ps' to "
78 "calculate used pids");
82 max_pids
= get_max_pids();
87 return max_pids
- used_pids
;