home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_02 / 2n02062a < prev    next >
Text File  |  1991-01-02  |  3KB  |  105 lines

  1.  
  2. /* ---------------------------------------------------
  3.    TESTTIME.C  -  Microsoft C ver 5.1
  4.  
  5.    A program to determine execution times.
  6.    Displays the number of milliseconds needed to
  7.    determine if enough file handles are available for
  8.    an application to proceed using the functions in
  9.    Listings 1 and 2 as well as by opening the NUL
  10.    device.
  11.  
  12.    The number of handles which must be available for
  13.    "success" is determined by the #define below
  14.  
  15.    Commands to produce an executable from the code as
  16.    shown in the listings:
  17.        masm sfht;             (listing 1)
  18.        masm pfht;             (listing 3)
  19.        masm timer;            (CODE DISK TS 2.2)
  20.        cl -c ctimer.c         (CODE DISK TS 2.2)
  21.        cl -c testtime.c       (listing 7)
  22.        link testtime sfht pfht timer ctimer;
  23.    -------------------------------------------------- */
  24.    
  25. #include <stdio.h>
  26. #include <dos.h>
  27. #include "tctimer.h"        /* not included here */
  28. #include <io.h>
  29. #include <fcntl.h>
  30. #include <types.h>
  31. #include <stat.h>
  32.  
  33. /*
  34.  * how many file handles must be available for the
  35.  * "application" to continue
  36. */
  37. #define HANDLES_I_NEED 10
  38.  
  39. /* ----------------------------------------------------
  40.  *  MAIN()
  41.    ----------------------------------------------------  */
  42. main()
  43. {
  44. long t1,t2;
  45. double time;
  46. int sys_handles, sys_handles_used,
  47.     sys_available, process_free,
  48.     ok_to_proceed, h_table[HANDLES_I_NEED], i;
  49.  
  50.   initializetimer();    /* init the timer routines */
  51.  
  52. /* Listings 1 & 2 method */
  53.   printf("\nusing routines from listings 1 & 2\n");
  54.  
  55.   t1 = readtimer();     /* get start time */
  56.  
  57.   ok_to_proceed = 1;    /* default = there are enough */
  58.  
  59.   sys_handles = max_sys_file_handles();
  60.   sys_handles_used = sys_handles_in_use();
  61.   sys_available = sys_handles - sys_handles_used;
  62.   process_free = process_handles_free();
  63. /*
  64.  * if there are not enough system-level handles or not
  65.  * enough process-level handles available, indicate that the
  66.  * application can't proceed
  67. */
  68.   if( process_free < HANDLES_I_NEED ||
  69.       sys_available < HANDLES_I_NEED )
  70.      ok_to_proceed = 0;
  71.  
  72.   t2 = readtimer();     /* get end time */
  73.                         /* calculate elapsed time */
  74.   elapsedtime(t1, t2, &time);
  75.   printf("elapsed time = %f\n%s\n\n",
  76.          time, (ok_to_proceed==1) ? "OK" : "NOT OK");
  77.  
  78.  
  79. /* "open the nul device" method */
  80.   printf("using open()\n");
  81.  
  82.   t1 = readtimer();     /* get start time */
  83.  
  84.   ok_to_proceed = 1;    /* default = there are enough */
  85.  
  86.   for(i = 0; i < HANDLES_I_NEED; i++)
  87.      {                 /* if the open failed */
  88.      if( (h_table[i] = open("nul", O_TEXT | O_RDWR)) == -1 )
  89.         {              /* indicate failure & leave loop */
  90.         ok_to_proceed = 0;
  91.         break;
  92.         }
  93.      }
  94.   while( i >= 0 )      /* close what was opened */
  95.      close(h_table[i--]);
  96.  
  97.   t2 = readtimer();     /* get end time */
  98.                         /* calculate elapsed time */
  99.   elapsedtime(t1, t2, &time);
  100.   printf("elapsed time = %f\n%s\n\n",
  101.          time, (ok_to_proceed==1) ? "OK" : "NOT OK");
  102.                         /* clean up the timer routines */
  103.   restoretimer();
  104. }
  105.