File: proclist.c

package info (click to toggle)
libgtop2 2.38.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,944 kB
  • sloc: ansic: 25,286; sh: 7,044; makefile: 416; perl: 268; xml: 179; sed: 16
file content (59 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <glibtop.h>
#include <glibtop/proclist.h>
#include <glibtop/procstate.h>
#include <glibtop/proctime.h>
#include <glibtop/procuid.h>

#include <glib.h>

#include <stdio.h>

#include <unistd.h>
#include <sys/types.h>


static void print_pids(guint64 which, guint64 arg)
{
	pid_t *pids;
	unsigned i;
	glibtop_proclist buf;

	pids = glibtop_get_proclist(&buf, which, arg);

	for (i = 0; i < buf.number; ++i) {
		glibtop_proc_time ptime;
		glibtop_proc_uid puid;
		glibtop_proc_state pstate;

		pid_t p = pids[i];

		glibtop_get_proc_time(&ptime, p);
		glibtop_get_proc_uid(&puid, p);
		glibtop_get_proc_state(&pstate, p);

		/* almost equivalent to ps -x -o user,pid,time,usertime,systime,start,command */
		printf("%u pid=%u real=%.2f user=%.2f sys=%.2f start=%lu %s (%lx)\n",
		       puid.uid,
		       p,
		       (double)ptime.rtime / ptime.frequency,
		       (double)ptime.utime / ptime.frequency,
		       (double)ptime.stime / ptime.frequency,
		       (unsigned long)ptime.start_time,
		       pstate.cmd,
		       (long)ptime.flags);
	}

	g_free(pids);
}


int main()
{
	glibtop_init();

	print_pids(GLIBTOP_KERN_PROC_UID, getuid());

	glibtop_close();

	return 0;
}