home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / threads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-28  |  3.3 KB  |  153 lines

  1. /* threads.c (emx+gcc) */
  2.  
  3. #define INCL_DOSMEMMGR
  4. #define INCL_DOSPROCESS
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <getopt.h>
  11. #include <os2emx.h>
  12.  
  13. static int alloca_flag = FALSE;
  14. static int malloc_flag = FALSE;
  15. static int malloc_errors = 0;
  16.  
  17. static char *check[] =
  18. {
  19.   "ok", "empty", "bad begin", "bad node", "bad end", "bad rover"
  20. };
  21.  
  22.  
  23. static void usage (void)
  24. {
  25.   fputs ("Usage: threads [-ams] <number>\n", stderr);
  26.   exit (1);
  27. }
  28.  
  29.  
  30. static void thread (void *t)
  31. {
  32.   int i;
  33.   ULONG written;
  34.   char c;
  35.   void *p;
  36.  
  37.   c = *(char *)t;
  38.   if (alloca_flag)
  39.     p = alloca (0x10000);
  40.   for (i = 0; i < 1000; ++i)
  41.     {
  42.       if (malloc_flag)
  43.         {
  44.           p = malloc (4);
  45.           if (p == NULL || realloc (p, 16) == NULL)
  46.             {
  47.               ++malloc_errors;
  48.               c = (char)toupper (c);
  49.             }
  50.         }
  51.       DosWrite (1, &c, 1, &written);
  52.       c = (char)tolower (c);
  53.     }
  54. }
  55.  
  56.  
  57. static void show_mem (void)
  58. {
  59.   ULONG rc, size, flags, base, esp;
  60.  
  61.   base = 0x10000;
  62.   esp = (ULONG)&esp;
  63.   for (;;)
  64.     {
  65.       size = 0xffffffff;
  66.       rc = DosQueryMem ((PVOID)base, &size, &flags);
  67.       if (rc != 0)
  68.         return;
  69.       printf ("%.8lx - %.8lx (%.8lx)", base, base + size - 1, size);
  70.       if (flags & PAG_FREE)    printf (" FREE  ");
  71.       if (flags & PAG_COMMIT)  printf (" COMMIT");
  72.       if (flags & PAG_GUARD)   printf (" GUARD ");
  73.       if (flags & PAG_SHARED)  printf (" share ");
  74.       if (flags & PAG_READ)    printf (" read  ");
  75.       if (flags & PAG_READ)    printf (" write ");
  76.       if (flags & PAG_EXECUTE) printf (" exec  ");
  77.       if (base <= esp && esp < base + size) printf ("<-- ESP");
  78.       putchar ('\n');
  79.       base += size;
  80.       if (base == 0) break;
  81.     }
  82. }
  83.  
  84.  
  85. int main (int argc, char *argv[])
  86. {
  87.   char *tmp;
  88.   int c, i, n, rc, running, show_mem_flag, big_stack_flag;
  89.   TID tid;
  90.  
  91.   show_mem_flag = FALSE; big_stack_flag = FALSE;
  92.   opterr = FALSE;
  93.   while ((c = getopt (argc, argv, "abms")) != EOF)
  94.     switch (c)
  95.       {
  96.       case 'a':
  97.         alloca_flag = TRUE;
  98.         break;
  99.       case 'b':
  100.         big_stack_flag = TRUE;
  101.         break;
  102.       case 'm':
  103.         malloc_flag = TRUE;
  104.         break;
  105.       case 's':
  106.         show_mem_flag = TRUE;
  107.         break;
  108.       default:
  109.         usage ();
  110.       }
  111.   if (argc - optind != 1)
  112.     usage ();
  113.   n = atoi (argv[optind]);
  114.   if (n < 1 || n > 26)
  115.     usage ();
  116.   c = 'a'; running = 0;
  117.   for (i = 0; i < n; ++i)
  118.     {
  119.       tmp = alloca (1);
  120.       *tmp = (char)c;
  121.       rc = _beginthread (thread, NULL,
  122.                          (big_stack_flag ? 16*1024*1024 : 128*1024), tmp);
  123.       if (rc == -1)
  124.         {
  125.           perror ("_beginthread");
  126.           exit (2);
  127.         }
  128.       ++c; ++running;
  129.     }
  130.   if (show_mem_flag)
  131.     {
  132.       DosEnterCritSec ();
  133.       putchar ('\n');
  134.       show_mem ();
  135.       fflush (stdout);
  136.       DosExitCritSec ();
  137.     }
  138.   while (running != 0)
  139.     {
  140.       tid = 0;
  141.       if (DosWaitThread (&tid, DCWW_WAIT) != 0)
  142.         break;
  143.     }
  144.   if (malloc_flag)
  145.     {
  146.       printf ("\n");
  147.       if (malloc_errors != 0)
  148.         printf ("%d malloc errors\n", malloc_errors);
  149.       printf ("Heap check: %s\n", check[_heapchk ()]);
  150.     }
  151.   return (0);
  152. }
  153.