Chapter Contents

Previous

Next
chpriority

chpriority



Change Process Priority

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include int chpriority(int kind, id_t id, 
               int form, int prio);


DESCRIPTION

The chpriority function changes the UNIX System Services priority of a process, or of all processes in a process group or belonging to a user. See the getpriority function description in section getpriority for further information on UNIX System Services priorities.

The kind argument to chpriority should be specified as a symbolic constant indicating the scope of the priority change. The permissible values are:

PRIO_PROCESS
Specifies that the id argument is the pid of the process whose priority is to be changed.

PRIO_PGRP
Specifies that the id argument is the pid of the process group whose processes should be changed in priority.

PRIO_USER
Specifies that the id argument is the uid of the user whose processes are to be changed in priority.

The id argument specifies the process id, process group id, or user id whose priority should be changed. If id is 0, the calling process id, process group id, or user id is specified.

The form argument specifies whether the prio argument is an absolute or relative priority. It should be specified as one of the following symbolic constants:

CPRIO_ABSOLUTE
Specifies that prio is to be the new priority of the processes specified.

CPRIO_RELATIVE
Specifies that prio is the amount which should be added to the existing priority of each process specified.

The prio argument specifies the requested new priority, or the amount by which the priority should be changed, depending of the value of the form argument. Priorities are restricted to the range of -20 to 19, where lower numbers indicate higher priority.

Note:   UNIX System Services sites must enable the use of the chpriority function. If the use of chpriority has not been enabled, any use of chpriority will fail with errno set to ENOSYS.   [cautionend]


RETURN VALUE

chpriority returns 0 if successful, or -1 if unsuccessful.


EXAMPLE

This example is compiled using sascc370 -Krent -o. This program demonstrates the use of the UNIX System Service process priority functions chpriority(), getpriority(), and setpriority().

/*---------------------------+
| POSIX/UNIX header files    |
+----------------------------*/
#include 
#include 
#include  
/*---------------------------+
| ISO/ANSI header files      |
+----------------------------*/
#include 
#include 
#include 
/*---------------------------+
| Name:     main             |
| Returns: exit(EXIT_SUCCESS)|
| or exit(EXIT_FAILURE)      |
+----------------------------|
+---------------------------*/
int main()
{
/* generic return code      */
   int rc;

/* which kind of process    */
/* id to use                */
   int kind;                  

 /* process id              */
   id_t id;                  

/* is oeprty relative or    */
/* absolute                 */
   int form;                 
 
/* process priority         */
/* (version 1)              */
   int prty1;                
 
/* process priority         */
/* (version 2)              */
   int prty2;                
 
/* process id from getpid() */
   pid_t pid;                
 
/* process group id from     */
/* getpgid()                 */
   pid_t pgid;               
  
/* process user id from      */
*/ getuid()                  */
   uid_t uid;                  

/* get the user id for this   */
/* process                    */
   uid = getuid();              

/* get the process id for     */
/* this process               */
   pid = getpid();              
/* get the group process id   */
/* for this process  */
   pgid = getpgid(pid); 

   if (pgid == -1)
   {
    perror("Call to getpgid failed");
    exit(EXIT_FAILURE);
   }

   printf("      The process id: %d\n", 
     (int)pid);
   printf("The process group id: %d\n", 
     (int)pgid);
   printf(" The process user id: %d\n", 
     (int)uid);

   /*------------------------------*/
   /* Get the process priority     */
   /* using the process id         */
   /*------------------------------*/
   print 
     ("\nGet the Process Priority 
         using the Process ID\n");

/* the id arg is the pid of a process */

   kind = PRIO_PROCESS;

   /* version 1 */
   id = (id_t)pid;

/* Set errno to zero for    */
/* error handling           */
   errno = 0;            
   prty1 = getpriority(kind, id);   

/* ---------------------------------*/
/* Test for Error                   */
/* Note:                            */
/* getpriority() may return a '-1'  */
/* return code for either a         */
/* failure rc, or when the priority */
/* is in-fact '-1'.  To distinguish */
/* between the two conditions,      */
/* check the errno                  */
/* value for a non-zero value.      */
/*----------------------------------*/
 if (prty1 == -1 && errno != 0)
 {
   perror("Call to getpriority failed");
   exit(EXIT_FAILURE);
   }
   else
   {
      printf("The process priority 
       (pid:version 1): %d\n", prty1);
   }
 
/* version 2 */
/* 0 implies current processs id  */
   id = (id_t)0;         

/* Reset errno to zero for        */
/* error handling                 */
   errno = 0;            
   prty2 = getpriority(kind, id);   

/* Test for Error */
   if (prty2 == -1 && errno != 0)
   {
      perror("Call to getpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
     printf("The process priority 
     (pid:version 2): %d\n", prty2);
   }

/*-----------------------------*/
/* Get the process priority    */
/* using the group process id  */
/*-----------------------------*/
   printf("\nGet the Process Priority 
    using the Group Process ID\n");

/* the id arg is the group process id */
   kind = PRIO_PGRP;     

/* version 1 */
   id = (id_t)pgid;

/* Set errno to zero for error handling      */
   errno = 0;            
   prty1 = getpriority(kind, id);   

/* Test for Error */
   if (prty1 == -1 && errno != 0)
   {
      perror("Call to getpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      printf("The process priority 
       (gpid:version 1): %d\n", prty1);
   }
 
/* version 2 */
/* 0 implies current group processs id       */
   id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            
   prty2 = getpriority(kind, id);   

/* Test for Error */
   if (prty2 == -1 && errno != 0)
   {
      perror("Call to getpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      printf("The process priority 
       (gpid:version 2): %d\n", prty2);
   }

 /*-------------------------------------------*/
 /* Get the process priority using the        */
 /* process User id                           */
 /*-------------------------------------------*/
 print 
  ("\nGet the Process Priority of the User ID\n");

/* the id arg is the user id of the process  */
   kind = PRIO_USER;     

/* version 1 */
   id = (id_t)uid;
/* Set errno to zero for error handling      */
   errno = 0;            
   prty1 = getpriority(kind, id);   

   /* Test for Error */
   if (prty1 == -1 && errno != 0)
   {
      perror("Call to getpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      printf("The process priority (uid:version 2):
        %d\n", prty1);
   }
 
/* version 2 */
/* Reset errno to zero for error handling    */
   errno = 0;   
        
 /* 0 implies current process user id        */
   id = (id_t)0;         
   prty2 = getpriority(kind, id);   

   /* Test for Error */
   if (prty2 == -1 && errno != 0)
   {
      perror("Call to getpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      printf("The process priority (uid:version 2): 
       %d\n", prty2);
   }

/*-----------------------------------------------*/
/* Set the process priority using the            */
/* process id                                    */ 
 /*----------------------------------------------*/
 printf
("\nSet the Process Priority using 
   the Process ID\n");

/* the id arg is the pid of a process        */
   kind = PRIO_PROCESS;  

/* an id of 0 implies current processs id    */
   id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            

/* Set process priority to 5                 */
   prty1 = 5;            

   rc = setpriority(kind, id, prty1);

   /*------------------------------------------------*/
   /* Test for Error                                 */
   /* Note: UNIX System Services  sites must enable  */
   /*       the use of the setpriority() function.   */
   /*       If the use of setpriority() has not      */
   /*       beenenabled, any use of setpriority()    */
   /*       will fail with errno set to ENOSYS.      */
   /*                                                */
   /*------------------------------------------------*/
   if (rc == -1)
   {
      perror("Call to setpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
      printf("The process priority is now (pid):
        %d\n", prty2);
   }

   /*--------------------------------------------*/
   /* Set the process priority using the group   */
   /* process id                                 */
   /*--------------------------------------------*/
 printf
("\nSet the Process Priority using 
   the Group Process ID\n");

/* the id arg is the group id of the process */
   kind = PRIO_PGRP;     

/* 0 implies current group processs id       */
   id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            

/* Set process priority to 10                */
   prty1 = 10;           


   rc = setpriority(kind, id, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to setpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
      printf("The process priority is now (gpid): 
       %d\n", prty2);
   }

 /*-------------------------------------------*/
 /* Set the process priority using the        */
 /*process User id                            */
 /*-------------------------------------------*/
  printf
  ("\nSet the Process Priority of 
     the User ID\n");

/* the id arg is the user id of the process  */
   kind = PRIO_USER;     

/* an id of 0 implies current user id        */
   id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            

/* Set process priority to 15                */
   prty1 = 15;           


   rc = setpriority(kind, id, prty1);

/* Test for Error */
   if (rc == -1)
   {
      perror("Call to setpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
printf
("The process priority is now 
   (uid): %d\n", prty2);
   }

/*-------------------------------------------*/
/* Change the process priority using the     */
/* process id                                */
/*-------------------------------------------*/
printf
 ("\nChange the Process Priority 
    using the Process ID\n");

/* the id arg is the pid of a process        */
   kind = PRIO_PROCESS;  

/* an id of 0 implies current processs id    */
   id = (id_t)0;        
 
/* Reset errno to zero for error handling    */
   errno = 0;            

/* change using "absolute"                   */
/* priority - equivalent to setpriority()    */
   form = CPRIO_ABSOLUTE;
   printf("\tChange using CPRIO_ABSOLUTE\n");

/* Change process priority to 3              */
   prty1 = 3;            

   rc = chpriority(kind, id, form, prty1);

/*------------------------------------------------*/
/* Test for Error                                 */
/* Note: UNIX System Services  sites must enable  */
/*       the use of the chpriority() function.    */
/*       If the use of chpriority() has not been  */
/*       enabled, any use of chpriority() will    */
/*       fail with errno set to ENOSYS.           */
/*                                                */
/*------------------------------------------------*/
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
  printf
  ("The process priority is now 
     (pid): %d\n", prty2);
   }

/* change using "relative" priority */
   form = CPRIO_RELATIVE;
   printf("\tChange using CPRIO_RELATIVE\n");

/* Bump process priority up by 2            */
   prty1 = 2;            

   rc = chpriority(kind, id, form, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
       perror("Call to getpriority failed");
       exit(EXIT_FAILURE);
      }
   printf("The process priority is now (pid): 
     %d\n", prty2);
   }

/*---------------------------------------------*/
/* Change the process priority using the       */
/* group process id                           */ 
/*---------------------------------------------*/
 printf
  ("\nChange the Process Priority using 
     the Group Process ID\n");

/* the id arg is the group id of the process */
   kind = PRIO_PGRP;     

/* 0 implies current group processs id       */
    id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            

   /* change using "absolute"                 */
   /* priority - equivalent to setpriority()  */
   form = CPRIO_ABSOLUTE;
   printf("\tChange using CPRIO_ABSOLUTE\n");

 /* Change process priority to 7              */
   prty1 = 7;           

   rc = chpriority(kind, id, form, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
  printf
("The process priority is now 
    (gpid): %d\n", prty2);
   }

/* change using "relative" priority */
   form = CPRIO_RELATIVE;
   printf("\tChange using CPRIO_RELATIVE\n");

/* Bump process priority up by 3            */
   prty1 = 3;            

   rc = chpriority(kind, id, form, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
         perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }
 printf("The process priority is now (gpid): 
   %d\n", prty2);
   }

/*--------------------------------------*/
/* Change the process priority using    */
/* the process User id                  */
/*--------------------------------------*/
printf
("\nChange the Process Priority 
   of the User ID\n");

/* the id arg is the user id of the process  */
   kind = PRIO_USER;     

/* 0 implies current group processs id       */
   id = (id_t)0;         

/* Reset errno to zero for error handling    */
   errno = 0;            

/* change using "absolute"                  */
/*  priority - equivalent to setpriority()  */
   form = CPRIO_ABSOLUTE;
   printf("\tChange using CPRIO_ABSOLUTE\n");

/* Change process priority to 11           */
   prty1 = 11;           

   rc = chpriority(kind, id, form, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
       perror("Call to getpriority failed");
         exit(EXIT_FAILURE);
      }

printf("The process priority is now (uid):
   %d\n", prty2);
   }

/* change using "relative" priority */
   form = CPRIO_RELATIVE;
   printf("\tChange using CPRIO_RELATIVE\n");

/* Bump process priority up by 4            */
   prty1 = 4;            

   rc = chpriority(kind, id, form, prty1);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to chpriority failed");
      exit(EXIT_FAILURE);
   }
   else
   {
      prty2 = getpriority(kind, id);   

      /* Test for Error */
      if (errno != 0)
      {
      perror("Call to getpriority failed");
       exit(EXIT_FAILURE);
      }
      printf("The process priority is now (uid):
         %d\n", prty2);
   }

   exit(EXIT_SUCCESS);

}  /* end of main() */


RELATED FUNCTIONS

getpriority, setpriority


Chapter Contents

Previous

Next

Top of Page

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