Cover V03, I04
Article
Figure 1
Figure 2
Figure 3
Figure 4
Listing 1
Sidebar 1
Table 1

jul94.tar


Listing 1: mon.c Source Code

1       /*
2        * mon.c
3        * Copyright 1992, Chris Hare
4        */
5
6       #include <sys/types.h>          /* system primitives
7       #include <sys/param.h>          /* system configuration parameters
8       #include <sys/page.h>           /* system memory management
9       #include <sys/immu.h>           /* system intel memory management
10      #include <sys/region.h>         /* system process region configuration
11      #include <sys/proc.h>           /* process table structure */
12      #include <sys/var.h>            /* system configuration */
13      #include <sys/dir.h>            /* File system header */
14      #include <sys/user.h>           /* u-area information */
15      #include <sys/sysi86.h>         /* SCO UNIX SPECIFIC - this header is used
16                                         to provide support for the sysi86
17                                         function which is used to retrive the
18                                         process u-area. */
19      #include <sys/file.h>           /* system file table structure */
20      #include <stdio.h>              /* standard libary header */
21      #include <fcntl.h>              /* file control header */
22      #include <nlist.h>              /* system namelist structure */
23      #include <pwd.h>
24      #include <string.h>             /* string functions */
25      #include <memory.h>             /* memory management functions */
26
27      struct nlist names[] = {
28              "proc", 0,      0,      0,      0,      0,
29              "v",    0,      0,      0,      0,      0,
30              "file", 0,      0,      0,      0,      0,
31              0,      0,      0,      0,      0,      0,
32      };
33
34      proc_t p;                       /* process structure */
35      struct var v;                   /* namelist structure */
36      struct file file_table;         /* file table structure */
37      int NOFILES;                    /* open file counter */
38
39      char *Usage = "Usage: mon\n";   /* error message */
40
41      main(argc, argv)
42              int argc;
43              char *argv[];
44      {
45
46              int k_fd;               /* kernel filedescriptor */
47
48              /* call nlist(S) to fill out values in names[]; */
49              nlist( "/unix", names );
50
51              /* open /dev/kmem, readonly;*/
52              k_fd = open( "/dev/kmem", O_RDONLY );
53              if ( k_fd < 0 )
54                      {
55                      perror("/dev/kmem");
56                      exit(1);
57                      }
58              /* call domon() to read the process table and print the results;*/
59              domon(k_fd);
60
61              close( k_fd );
62              exit(0);
63
64      }
65
66      /*
67       * domon()
68       *
69       * lseek, and then read the process table from /dev/kmem
70       */
71
72      domon(fd)
73      int fd;
74      {
75
76              int bytes_seek = 0;
77              int p_no = 0;
78              /*int p_used = 0;*/
79              char *Pp_used;
80              int p_used = 0;
81              struct proc p_entry;
82              long proc_table = names[0].n_value;
83              long var_table = names[1].n_value;
84
85              /* seek to the struct v, and read it; */
86              bytes_seek = lseek( fd, var_table, 0 );
87              read( fd, &v, sizeof(v) );
88              NOFILES = v.v_nofiles;
89              /*
90                 calculate the current extent of the process table, based
91                 on v.ve_proc;
92              */
93              p_used = ( ( (long)v.ve_proc - proc_table ) / sizeof(p_entry) );
94              /* seek to the process table; */
95              bytes_seek = lseek( fd, proc_table, 0 );
96              /*
97              print a header on the screen;
98              */
99              dohdr();
100
101             for( p_no = 0; p_no < p_used; p_no++ )
102                {
103                /* read the process table entry;*/
104                bytes_seek = read( fd, &p_entry, sizeof(p_entry) );
105
106                /*
107                   if p_stat is valid,
108                   call doproc() to display the proc entry;
109                */
110                if ( p_entry.p_stat > 0 )
111                   doproc( &p_entry );
112                }
113             printf( "--\nCONFIG : %d ALLOC : %d\n", v.v_proc, p_used );
114
115     }
116
117     /*
118      * dohdr()
119      *
120      * print column headers
121      */
122
123     dohdr()
124     {
125             printf( "STATUS FLAGS PID    PPID   USERID   PRI NI C  OF  TTY COMMAND\n");
126     }
127
128     /*
129      * doproc()
130      *
131      * print process table entry
132      */
133
134     doproc(pp)
135     proc_t *pp;
136     {
137             char *p_state[8] = { "", "SSLEEP", "SRUN",
"SZOMB",
138                                  "SSTOP", "SIDL", "SONPROC",
"SXBRK" };
139
140             struct passwd *getpwuid();    /* define the subroutine */
141             struct passwd *pw;    /* where to put the data */
142             struct user u_buf;
143             char pflag[6];
144             int x;
145             int ret;
146             char cmd[PSARGSZ];
147
148             /* get the real user name for this process */
149             pw = getpwuid( pp->p_uid );
150             /* get the u-block for the process and stuff it into a buffer */
151             ret = sysi86( RDUBLK, pp->p_pid, &u_buf, sizeof(u_buf) );
152             memset( cmd, 0x00, PSARGSZ );
153             if ( ret < 0 )
154                strcpy( cmd, "<defunct>" );
155             else
156                strcpy( cmd, u_buf.u_psargs );
157             /* find out how many files this process has open */
158             for (x = 0; x< NOFILES; x++ )
159                {
160                if ( u_buf.u_ofile[x] == (char *)0 )
161                   break;
162                }
163             /* is this process in ram, or on swap? */
164             memset( pflag, 0x00, sizeof(pflag) );
165             if ( ( pp->p_flag & 0x00000001 ) == 1 )
166                     strcpy( pflag, "SYS" );
167             else if ( ( pp->p_flag & 0x00000008 ) == 0x8 )
168                     strcpy( pflag, "NWAKE" );
169             else if ( ( pp->p_flag & 0x00000020 ) == 0x20 )
170                     strcpy( pflag, "LOCK" );
171             else if ( ( pp->p_flag & 0x00020000 ) == 0x20000 )
172                     strcpy( pflag, "ASLP" );
173             else if ( ( pp->p_flag & 0x01000000 ) == 0x01000000 )
174                     strcpy( pflag, "EXIT" );
175             else if ( ( pp->p_flag & 0x00000010 ) == 0x10 )
176                     strcpy( pflag, "CORE" );
177             else
178                     strcpy( pflag, "SWAP" );
179             /*print the process table entry described by pp;*/
180             /* STATE FLAGS PID PPID UID PRI NI C FILES CMD*/
181             printf( "%-7s %-5s %-6d %-6d %-8s %-3d %-2d %-2d %2d %-s\n",
182                      p_state[pp->p_stat], pflag, pp->p_pid, pp->p_ppid,
183                      pw->pw_name, pp->p_pri, pp->p_nice, pp->p_cpu, x, cmd );
184             return(0);
185     }