Skip to content

Commit 27d6dc6

Browse files
committed
libutil: Improve kinfo_getproc(3) example and cross-references
Enhance the kinfo_getproc(3) manual page example: - Make example work without arguments by defaulting to current process when no PID is specified - Make memory output consistent by displaying all values in KB instead of mixing bytes and KB - Add compilation instructions showing that -lutil is required Improve struct documentation: - Add comment about ki_structsize and KINFO_PROC_SIZE macro for determining actual structure size - Direct readers to <sys/user.h> for full structure definition - Move ellipsis inside comment to make struct syntactically valid C Add cross-reference to kinfo_getproc(3) in libprocstat(3) manual page since it provides comprehensive documentation for struct kinfo_proc.
1 parent 4e2ca7b commit 27d6dc6

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/libprocstat/libprocstat.3

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2323
.\" SUCH DAMAGE.
2424
.\"
25-
.Dd April 3, 2022
25+
.Dd November 4, 2025
2626
.Dt LIBPROCSTAT 3
2727
.Os
2828
.Sh NAME
@@ -577,6 +577,7 @@ argument indicates an actual error message in case of failure.
577577
.Xr shm_open 2 ,
578578
.Xr socket 2 ,
579579
.Xr elf 3 ,
580+
.Xr kinfo_getproc 3 ,
580581
.Xr kvm 3 ,
581582
.Xr queue 3 ,
582583
.Xr sem_open 3 ,

lib/libutil/kinfo_getproc.3

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ struct kinfo_proc {
117117
struct rusage ki_rusage; /* process rusage statistics */
118118
struct rusage ki_rusage_ch; /* rusage of children processes */
119119
long ki_sflag; /* PS_* flags */
120-
... /* additional fields */
120+
/* ... additional fields, see <sys/user.h> for full definition.
121+
* Use ki_structsize or KINFO_PROC_SIZE for actual struct size. */
121122
};
122123
.Ed
123124
.Pp
@@ -228,7 +229,8 @@ On failure the
228229
function returns
229230
.Dv NULL .
230231
.Sh EXAMPLES
231-
The following example retrieves and displays process memory information:
232+
The following example retrieves and displays process memory information.
233+
If no argument is provided, it displays information for the current process:
232234
.Bd -literal -offset indent
233235
#include <sys/types.h>
234236
#include <sys/user.h>
@@ -245,10 +247,10 @@ main(int argc, char *argv[])
245247
int pagesize;
246248
pid_t pid;
247249

248-
if (argc != 2)
249-
errx(1, "usage: %s pid", argv[0]);
250+
if (argc > 2)
251+
errx(1, "usage: %s [pid]", argv[0]);
250252

251-
pid = atoi(argv[1]);
253+
pid = (argc == 2) ? atoi(argv[1]) : getpid();
252254
pagesize = getpagesize();
253255

254256
kp = kinfo_getproc(pid);
@@ -257,8 +259,8 @@ main(int argc, char *argv[])
257259

258260
printf("Process %d (%s) memory usage:\\n",
259261
kp->ki_pid, kp->ki_comm);
260-
printf(" Virtual size: %zu bytes\\n",
261-
(size_t)kp->ki_size);
262+
printf(" Virtual size: %zu KB\\n",
263+
(size_t)kp->ki_size / 1024);
262264
printf(" Resident size: %zu KB\\n",
263265
(size_t)(kp->ki_rssize * pagesize) / 1024);
264266
printf(" Text size: %zu KB\\n",
@@ -272,6 +274,11 @@ main(int argc, char *argv[])
272274
return (0);
273275
}
274276
.Ed
277+
.Pp
278+
The above example can be compiled with:
279+
.Bd -literal -offset indent
280+
cc -o procmem procmem.c -lutil
281+
.Ed
275282
.Sh SEE ALSO
276283
.Xr ps 1 ,
277284
.Xr getpagesize 2 ,

0 commit comments

Comments
 (0)