home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_01 / 2n01014a < prev    next >
Text File  |  1990-10-26  |  2KB  |  98 lines

  1. /* threads3.c -- An example of a print server thread */
  2.  
  3. #define INCL_DOSQUEUES
  4. #define INCL_DOSFILEMGR
  5. #define INCL_DOSPROCESS
  6. #define INCL_DOSSEMAPHORES
  7. #include <os2.h>
  8.  
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <stdlib.h>
  12. #include <process.h>
  13.  
  14. #define THREAD_SSIZE 3000
  15. void ServerThread( void *arg);
  16. void ClientThread( void *arg);
  17.  
  18. USHORT ThreadID2;
  19. USHORT ThreadID3;
  20. USHORT ThreadID4;
  21. USHORT ThreadID5;
  22.  
  23. int t2 = 2;
  24. int t3 = 3;
  25. int t4 = 4;
  26. int t5 = 5;
  27. long p_ready_sem = 1L;     /* hey, please note, pre-set to 1 */
  28.  
  29. void main( void)
  30. {
  31.  
  32.   ThreadID2 = _beginthread( ServerThread, NULL,
  33.       THREAD_SSIZE, &t2);  /* note, don't pass &ThreadID2,
  34.                         since it may not be valid yet */
  35.   DosSemRequest( &p_ready_sem, SEM_INDEFINITE_WAIT);
  36.   ThreadID3 = _beginthread( ClientThread, NULL,
  37.       THREAD_SSIZE, &t3);
  38.   ThreadID4 = _beginthread( ClientThread, NULL,
  39.       THREAD_SSIZE, &t4);
  40.   ThreadID5 = _beginthread( ClientThread, NULL,
  41.       THREAD_SSIZE, &t5);
  42.   puts("Press any key to exit");
  43.   getch();
  44. }
  45.  
  46.  
  47. HQUEUE pq;
  48.  
  49. void ServerThread( void *arg)
  50. {
  51.   USHORT ec;
  52.   HFILE hp;
  53.   char buf[100];
  54.   USHORT n, nw;
  55.   USHORT openAction;
  56.   QUEUERESULT qr;
  57.   UCHAR pri;
  58.   char *msg;
  59.  
  60.   ec = DosOpen( "LPT1", &hp, &openAction, 0L,
  61.     FILE_NORMAL,  FILE_OPEN,
  62.     OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE |
  63.       OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_WRITE_THROUGH,
  64.     0L);
  65.   n = sprintf( buf, "Print Server Thread (%d) Started\r\n", *(int *)arg);
  66.   ec = DosWrite( hp, buf, n, &nw);
  67.   ec = DosCreateQueue( &pq, QUE_FIFO, "\\QUEUES\\PRINT.QUE");
  68.   ec = DosSemClear( &p_ready_sem);
  69.  
  70.   while (1)
  71.     {
  72.     ec = DosReadQueue( pq, &qr, &n, &msg, 0, DCWW_WAIT, &pri, 0);
  73.     ec = DosWrite( hp, msg, n, &nw);
  74.     free( msg);
  75.     }
  76. }
  77.  
  78.  
  79. void ClientThread( void *arg)
  80. {
  81.   USHORT ec;
  82.   int i, t, n;
  83.   USHORT j;
  84.   char *txt;
  85.  
  86.   t = * (int *) arg;
  87.   DosSetPrty( PRTYS_THREAD, PRTYC_NOCHANGE, t, 0);  /* change the priority */
  88.   for ( i=0; i < 16; i++)
  89.     {
  90.     for (j=0; j < 0xFFFF; j++)  /* waste some time, like a real program would */
  91.       n = j * i * j * i;
  92.     txt = malloc( 50);
  93.     n = sprintf( txt, "Thread %d, request %d.\r\n", t, i);
  94.     ec = DosWriteQueue( pq, 0, n, txt, (UCHAR) t );  /* could test priority queue */
  95.     }
  96. }
  97.  
  98.