home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / m2posx14 / test / tthread.mpp < prev    next >
Encoding:
Text File  |  1994-05-29  |  3.1 KB  |  135 lines

  1. MODULE tthread;
  2. __IMP_SWITCHES__
  3. __DEBUG__
  4. #if (defined HM2) || (defined HM2_OLD)
  5. (*$E+ lokale Prozedur als Parameter *)
  6. #endif
  7. #ifdef HM2
  8. #ifdef __LONG_WHOLE__
  9. (*$!i+: Modul muss mit $i- uebersetzt werden! *)
  10. (*$!w+: Modul muss mit $w- uebersetzt werden! *)
  11. #else
  12. (*$!i-: Modul muss mit $i+ uebersetzt werden! *)
  13. (*$!w-: Modul muss mit $w+ uebersetzt werden! *)
  14. #endif
  15. #endif
  16.  
  17. (* 29-Mai-94, Holger Kleinschmidt
  18.  *)
  19.  
  20. #if (defined MM2) && (defined __DEBUG_CODE__)
  21. IMPORT Debug;
  22. #endif
  23.  
  24.  
  25. VAL_INTRINSIC
  26. CAST_IMPORT
  27.  
  28. FROM PORTAB IMPORT
  29. (* TYPE *) UNSIGNEDLONG, UNSIGNEDWORD, ANYLONG, SIGNEDLONG;
  30.  
  31. FROM proc IMPORT
  32. (* TYPE *) WaitVal,
  33. (* PROC *) tfork, wait, WEXITSTATUS;
  34.  
  35. FROM sig IMPORT
  36. (* CONST*) SIGCHLD,
  37. (* TYPE *) SignalHandler,
  38. (* PROC *) signal;
  39.  
  40. FROM InOut IMPORT
  41. (* PROC *) Read, WriteString, WriteInt, WriteLn;
  42.  
  43. (*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*)
  44.  
  45. VAR
  46.   pid, pid1, pid2 : INTEGER;
  47.   globalvar       : INTEGER;
  48.   ch              : CHAR;
  49.   state           : WaitVal;
  50.   new, old        : SignalHandler;
  51.  
  52. (*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*)
  53.  
  54. PROCEDURE idle;
  55. VAR i : UNSIGNEDWORD;
  56. BEGIN
  57.  i := 0;
  58.  WHILE i < MAX(UNSIGNEDWORD) DO
  59.    INC(i);
  60.  END;
  61. END idle;
  62.  
  63. (*---------------------------------------------------------------------------*)
  64.  
  65. PROCEDURE thread1 (arg : ANYLONG): INTEGER;
  66. VAR i : INTEGER;
  67. BEGIN
  68.   globalvar := 42;
  69.   FOR i:=1 TO 3 DO
  70.     idle;
  71.     WriteString("``thread1'': "); WriteInt(INT(CAST(SIGNEDLONG,arg)), 0);
  72.     WriteLn;
  73.   END;
  74.   RETURN(11);
  75. END thread1;
  76.  
  77. (*---------------------------------------------------------------------------*)
  78.  
  79. PROCEDURE thread2 (arg : ANYLONG): INTEGER;
  80. VAR i : INTEGER;
  81. BEGIN
  82.   FOR i:=1 TO 3 DO
  83.     idle;
  84.     WriteString("``thread2'': "); WriteInt(INT(CAST(SIGNEDLONG,arg)), 0);
  85.     WriteLn;
  86.   END;
  87.   RETURN(22);
  88. END thread2;
  89.  
  90. (*---------------------------------------------------------------------------*)
  91.  
  92. #ifdef HM2
  93. (*$E+,$K+**)
  94. #endif
  95. PROCEDURE handler ((* EIN/ -- *) sig : UNSIGNEDLONG );
  96. BEGIN
  97.  WriteLn;
  98.  WriteString("<<<<<<<<<<<<<<<<<<<<<<<<<"); WriteLn;
  99.  WriteString("``main'': SIGCHLD handled"); WriteLn;
  100.  WriteString(">>>>>>>>>>>>>>>>>>>>>>>>>"); WriteLn;
  101. END handler;
  102. #ifdef HM2
  103. (*$E=,$K-*)
  104. #endif
  105.  
  106. (*===========================================================================*)
  107.  
  108. BEGIN (* tthread *)
  109.  new.proc := handler;
  110.  pid      := signal(SIGCHLD, new, old);
  111.  
  112.  globalvar := 0;
  113.  WriteString("``main'': globalvar: "); WriteInt(globalvar, 0); WriteLn;
  114.  
  115.  pid1 := tfork(thread1, 111);
  116.  pid2 := tfork(thread2, 222);
  117.  
  118.  idle;
  119.  
  120.  WriteString("``main'': thread1-pid: "); WriteInt(pid1, 0); WriteLn;
  121.  WriteString("``main'': thread2-pid: "); WriteInt(pid2, 0); WriteLn;
  122.  
  123.  LOOP
  124.    pid := wait(state);
  125.    IF pid < 0 THEN EXIT; END;
  126.    WriteString("``main'': pid "); WriteInt(pid, 0);
  127.    WriteString(" returned: "); WriteInt(WEXITSTATUS(state), 0);
  128.    WriteLn;
  129.  END;
  130.  
  131.  WriteString("``main'': globalvar: "); WriteInt(globalvar, 0); WriteLn;
  132.  
  133.  Read(ch);
  134. END tthread.
  135.