home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NICE10.ZIP / NICE.C < prev    next >
C/C++ Source or Header  |  1992-03-20  |  7KB  |  166 lines

  1. /******************************************************************
  2.  
  3.  NICE.C : This program allows you to run an OS/2 program in the idle 
  4.           time priority. Invoking:
  5.                                                                                              
  6.      NICE /switches MYPROG.EXE program_parameters
  7.  
  8. Like:
  9.  
  10.      NICE SEARCH "keyword"  readme.doc > keyword.out
  11.  
  12. NICE switches:
  13.  
  14.     /C : runs program in time-critical priority instead of idle priority.
  15.     /S : runs program synchronously with NICE.EXE.
  16.     /D : runs program detached from NICE.EXE (no console I/O possible)
  17.  
  18. Can be compiled under IBM C/SET 2 compiler.
  19. Programmer can be reached via BITNET:  <TURGUT@FRORS12.BITNET>
  20.                                     or <TURGUT@TREARN.BITNET>
  21. *******************************************************************/
  22. char *copyright = "NICE V1.0 (C)1992 T.Kalfaoglu, 1378 Sok. 8/10, Alsancak,Izmir,Turkey\n";
  23. #define INCL_DOSPROCESS
  24. #include <os2.h>
  25.  
  26. #include <stdio.h>
  27. #include <conio.h>
  28.  
  29. #define BUFLEN 512L
  30.  
  31. /* defining flags for the DosExecPgm call: */
  32. #define SYNCH 0L
  33. #define ASYNCH 2L
  34. #define DETACH 4L
  35.  
  36. /* flags for DosSetPriority call: */
  37. #define IDLE 1L
  38. #define CRITICAL 3L
  39.  
  40. void helper(void);
  41.  
  42. main(int argc, char *argv[])
  43. {
  44.    RESULTCODES result;
  45.    unsigned int i, startfrom = 1;
  46.    ULONG prt=IDLE, rc, dflag = ASYNCH, termPID;  
  47.    LONG delta=-31L, childID; 
  48.    char buffer[BUFLEN], flag, args[BUFLEN], cmdline[BUFLEN], out[128];
  49.    
  50.    fputs(copyright,stderr);
  51.    if (argc<2) helper();
  52.    
  53.    if (*argv[1] == '/' || *argv[1] == '-') { /* parsing run-time options */
  54.       startfrom = 2;
  55.       flag='?';
  56.       for (i=1;flag;i++) {
  57.          flag = toupper(*(argv[1]+i));
  58.          switch (flag) {
  59.             case 'C'  : prt   = CRITICAL;  
  60.                         delta = 31L;
  61.                         break;
  62.             case 'S'  : dflag = SYNCH;
  63.                         break;
  64.             case 'D'  : dflag = DETACH;
  65.                         break;
  66.             case  0   : break; 
  67.             default   : 
  68.                 sprintf(out,"Parameter '%c' is not recognised.\n",flag);
  69.                 fputs(out,stderr);
  70.                 exit(1);
  71.          }
  72.       }
  73.    }
  74.    strcpy(args,"/C \"");  /* parameter to CMD.EXE */
  75.    for (i=startfrom;i  < argc;i++) {
  76.       strcat(args,argv[i]);                                                                         
  77.       strcat(args," ");                  
  78.    }
  79.    strcat(args,"\"");
  80.    strcpy(cmdline,"CMD.EXE");
  81.    strcat(cmdline+8,args);  /* we need to have a \0 after CMD.EXE */
  82.  
  83.    rc = DosExecPgm(buffer,BUFLEN,dflag,cmdline,0L,&result,"CMD.EXE");
  84.    if (rc) {
  85.       sprintf(out,"Error %lu starting CMD.EXE with these parameters\n",rc);
  86.       fputs(out,stderr);
  87.       fputs(cmdline,stderr);
  88.       fputs("\nReport buffer contents: ",stderr);
  89.       fputs(buffer,stderr);
  90.       fputs("\n",stderr);
  91.    }
  92.    if (dflag == SYNCH) {
  93.       sprintf(out,"DosExecPgm invocation rc=%lu\n",rc);
  94.       fputs(out,stderr);
  95.       sprintf(out,"Termination code: %lu\n",result.codeTerminate);
  96.       fputs(out,stderr);
  97.       sprintf(out,"Result (exit) code: %lu\n",result.codeResult);
  98.       fputs(out,stderr);
  99.    }
  100.    else { /* if asynch.. */
  101.       childID = result.codeTerminate;
  102.       rc = DosSetPriority(1L,prt,delta,childID);
  103.       if (rc) { /* set priority failed */
  104.          sprintf(out,"DosSetPriority call failed with rc=%lu\n",rc);
  105.          fputs(out,stderr);
  106.          fputs("Process execution continues at standard priority level\n",stderr);
  107.       }
  108.       else /* success */
  109.          sprintf(out,"Process %lu started with %s priority\n",result.codeTerminate,
  110.            (prt == 1 ? "idle" : "time-critical"));
  111.           fputs(out,stderr);
  112.    }
  113.    /* If asynch, collect results, and reset priority for new decendants */
  114.    if (dflag == ASYNCH) 
  115.       while (DosCwait(0L,1L,&result,&termPID, childID) == 129) {
  116.          rc = DosSetPriority(1L,prt,delta,childID);
  117.          if (rc) { /* set priority failed */
  118.             sprintf(out,"DosSetPriority call failed with rc=%lu\n",rc);
  119.             fputs(out,stderr);
  120.             fputs("Process execution continues at previous priority level\n",stderr);
  121.          }
  122.          DosSleep(3000);
  123.    }    
  124.    DosExit(0,rc);
  125. }
  126.  
  127. void helper() {
  128.  int i;
  129.  printf("\nThis program allows you to run an OS/2 program either in idle\n");
  130.  printf("time priority, or time-critical priority levels by invoking a\n");
  131.  printf("command processor (CMD.EXE) with the parameters you specify, \n");
  132.  printf("and then modifying the priority of that CMD.EXE. It periodically\n");
  133.  printf("re-sets the priority of all subprocess of CMD.EXE\n\n");
  134.  printf("Usage:\n");
  135.  printf("    NICE /switches MYPROG.EXE program_parameters \n\n");
  136.  printf("Examples:\n");
  137.  printf("    NICE /cs SEARCH \"keyword\"  *.doc *.txt > keyword.out\n");
  138.  printf("    DETACH NICE ICC /c /Ti+ myfile.c\n");
  139.  printf("    DETACH NICE CL /c /AL myfile.c > compiler_says.out\n\n");
  140.  printf("Optional Switches:\n");
  141.  printf("   /C : runs program in time-critical priority instead of idle priority.\n");
  142.  printf("   /S : runs program synchronously with NICE.\n");
  143.  printf("   /D : runs program detached from NICE (no console I/O possible)\n\n");
  144.  printf("--Hit ENTER for the second page---\n");
  145.  i = getchar();
  146.  printf("Note 1: Due to a limitation, if NICE is invoked without the /D switch,\n");
  147.  printf(" the command prompt will not appear until the execution is completed.\n");
  148.  printf(" To get the command prompt right away, start your command with the DETACH\n");
  149.  printf(" keyword.\n");
  150.  printf("Note 2: If /D is specified, the program is run in the normal-priority mode\n\n");
  151.  printf(" NICE is written in C language with IBM C/SET 2 32-bit compiler and\n");
  152.  printf(" OS/2 2.0 beta 6.177 toolkit.\n\n");
  153.  printf("Things to try: \n");
  154.  printf(" - Open two OS/2 Windows in the workshell.\n");
  155.  printf(" - At one window, type COUNTER to run counter.cmd\n");
  156.  printf(" - At the other window, type: NICE COUNTER so that it runs in idle priority\n");
  157.  printf(" - Let them run for a while. Notice the difference in speed!\n");
  158.  printf(" - Try again by giving one of them time-critical priority:\n");
  159.  printf("   you may lose control of the workshell, until the counter hits 4000\n\n");
  160.  printf("User-Supported Software:\n");
  161.  printf(" A donation of $10 is required if you decide to keep this program.\n");
  162.  printf(" Please send payment to the above address. Thank you!\n");
  163.  exit(1);
  164.  }
  165.  
  166.