The Linux Programming Interface

(nextflipdebug5) #1

596 Chapter 28


In the output, we see one line for each process that was created in the shell session.
The ulimit and echo commands are shell built-in commands, so they don’t result in
the creation of new processes. Note that the entry for sleep appeared in the account-
ing file after the cat entry because the sleep command terminated after the cat
command.
Most of the output is self-explanatory. The flags column shows single letters
indicating which of the ac_flag bits is set in each record (see Table 28-1). Section 26.1.3
describes how to interpret the termination status values shown in the term. status
column.

Listing 28-2: Displaying data from a process accounting file
–––––––––––––––––––––––––––––––––––––––––––––––––––––– procexec/acct_view.c
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/acct.h>
#include <limits.h>
#include "ugid_functions.h" /* Declaration of userNameFromId() */
#include "tlpi_hdr.h"

#define TIME_BUF_SIZE 100

static long long /* Convert comp_t value into long long */
comptToLL(comp_t ct)
{
const int EXP_SIZE = 3; /* 3-bit, base-8 exponent */
const int MANTISSA_SIZE = 13; /* Followed by 13-bit mantissa */
const int MANTISSA_MASK = (1 << MANTISSA_SIZE) - 1;
long long mantissa, exp;

mantissa = ct & MANTISSA_MASK;
exp = (ct >> MANTISSA_SIZE) & ((1 << EXP_SIZE) - 1);
return mantissa << (exp * 3); /* Power of 8 = left shift 3 bits */
}

int
main(int argc, char *argv[])
{
int acctFile;
struct acct ac;
ssize_t numRead;
char *s;
char timeBuf[TIME_BUF_SIZE];
struct tm *loc;
time_t t;

if (argc != 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s file\n", argv[0]);

acctFile = open(argv[1], O_RDONLY);
if (acctFile == -1)
errExit("open");
Free download pdf