Chapter Contents

Previous

Next
w_getpsent

w_getpsent



Get Process Information

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
EXAMPLE


SYNOPSIS

#include <sys/ps.h>

int w_getpsent(int processPos, struct w_psproc *bufferArea,
              size_t bufferLength);


DESCRIPTION

w_getpsent gets information about all processes for which the caller is authorized. The arguments to the w_getpsent function are:

processPos
is an integer that identifies the relative position of a process within the system, with a 0 representing the first process. You should call w_getpsent from within a loop. On the first call, pass a 0 as the processPos argument, and w_getpsent will return the position value of the next process that the calling process has access to. This value is then used as the processPos argument for the next call. Continue to loop until a 0 is returned.

bufferArea
is a pointer to a buffer area that is used to store information about the processes.

bufferLength
is the length in bytes of the buffer area.

The information about a process is stored in a w_psproc structure, which is declared in <sys/ps.h> . You may access the following elements of this structure to obtain process information:

unsigned int ps_state
process state

pid_t ps_pid
process identification

pid_t ps_ppid
process parent identification

pid_t ps_sid
session identification

pid_t ps_pgpid
process group identification

pid_t ps_fgpid
foreground process group identification

uid_t ps_euid
effective user identification

uid_t ps_ruid
real user identification

uid_t ps_suid
saved user identification

gid_t ps_egid
effective group identification

gid_t ps_rgid
real group identification

gid_t ps_sgid
saved set group identification

long ps_size
total size

time_t ps_starttime
start time

clock_t ps_usertime
user CPU time

clock_t ps_systime
system CPU time

int ps_conttylen
controlling terminal name length

char *ps_conttyptr
controlling terminal name

int ps_pathlen
length of pathname

char *ps_pathptr
pathname

int ps_cmdlen
length of command

char *ps_cmdptr
command and arguments


RETURN VALUE

If successful, w_getpsent returns an integer that identifies the next process that the calling process has access to. A 0 is returned to indicate that there are no more accessible processes. A -1 is returned if the call to w_getpsent is unsuccessful.


PORTABILITY

The w_getpsent function may be useful in USS applications; however, it is not defined by the POSIX.1 standard and should not be used in portable applications.


EXAMPLE

The following example illustrates the use of w_getpsent to obtain process information:

#include <sys/types.h>
#include <sys/ps.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_CHAR 256

main()
{
   int processNum = 0;
   W_PSPROC workArea;

      /* Initialize work area.                   */
   memset(&workArea, 0, sizeof(workArea));

      /* Allocate memory for character strings.  */
   if ((workArea.ps_conttyptr = (char *) malloc(MAX_CHAR)) == NULL) {
      perror("ps_conttyptr memory allocation error");
      abort();
   }
   else
      workArea.ps_conttylen = MAX_CHAR;

   if ((workArea.ps_pathptr = (char *) malloc(MAX_CHAR)) == NULL) {
      perror("ps_pathptr memory allocation error");
      abort();
   }
   else
      workArea.ps_pathlen = MAX_CHAR;

   if ((workArea.ps_cmdptr = (char *) malloc(MAX_CHAR)) == NULL) {
      perror("ps_cmdptr memory allocation error");
      abort();
   }
   else
      workArea.ps_cmdlen = MAX_CHAR;

      /* Get process information.                */
   do {
      processNum = w_getpsent(processNum, &workArea, sizeof(workArea));
      if (processNum == -1)
         perror("error in w_getpsent");
      else  {
         printf("process number = %dn", processNum);
         printf("process ID = %10dn", workArea.ps_pid);
         printf("User ID = %8unn", workArea.ps_ruid);
      }
   } while (processNum != 0 && processNum != 1);
}


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.