
  .    .  " OS/2 "                          6



                         

*  ⠪ ᥬ?
* 뢠 () 樨.
* ⨢ .
* ᮢ   ᠬ.
* । ᨣ  砬.
*  ⨢ .
* ⥬ ᥬ.
* 㯨   " 䨫䮢".

      -     ⥬   ।⢠,  ᯮ㥬   OS/2  
ᮣᮢ  ⨢  ࠧ  .   (  㤥  ᯮ짮
ନ ""  祭   楯窠.   ᯮ
    :     ᯥ祭  ᮢ⭮  㯠   饬
   ᯥ祭       楯窨
। ᨣ 㣮.

       ᥬ஢:  ᥬ , ᯮ㥬 楯窠,  
⥬ ᥬ, ᯮ㥬  ᭮ ᠬ.

     ⮩    ᬮਬ   ᥬ஢.   
墠⨬  ᥬ        ᯮ짮  ᮢ⭮ 㯠 
। ᨣ.   祭 ᬮਬ ⥬ ᥬ.

           .

     ⠪ ᥬ  祬  㦭?  ६  
᪮쪮  ,    ६    न樮  ஡.
 ᯥ稢  襭.

      騩  砩  ᯮ짮  ᥬ஢  -   ᯥ祭
ᮣᮢ 㯠  ᮢ⭮ ᯮ㥬    ᪮쪨
.  । ᫮     ᮬ, ᫨
      ६.    ᮬ 
 ࠭ (     ࠣ ⥪ ६
 ),   ਭ,   ᪮.        
     ।⠢  祬  
  ६.   ⨯ ᮢ  뢠  "᫥⥫쭮
ᯮ㥬 ",   (SRR).

      ⢥  ਬ  室  ᮣᮢ  ᬮਬ
᫥ ணࠬ,  ன  楯窨 ६   
 ப  ࠭:

/* xdisp.c */
/*  楯窨  ࠭ */
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#define STACK_SIZE 1024   /* ⥪  楯窨 */
#define WAIT -1L          /* 1 = , 0 = ,
                             >0 = ᥪ㭤 */
#define HANDLE 0          /* १  ᥣ 0 */

char a1[] = " ᯥ稢\n\r";
char a2[] = "ᮢ   ᨣ\n\r";
char a3[] = " ᠬ  楯窠.\n\r\n\r";
char b1[] = " -  ண㫪,  \n\r";
char b2[] = " ⥫   -    業\n\r";
char b3[] = "    祣.\n\r\n\r";

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far thread(); /* ⨯   楯窨 */
 unsigned threadID; /*  楯窨 ID */
                    /*  ⥪,  祯 */
 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread, &threadID, stkptr);
                         /*    ࠭ */
 while( !kbhit() ) {     /*        */
   VioWrtTTy( a1, siseof(a1), HANDLE );
   VioWrtTTy( a2, siseof(a2), HANDLE );
   VioWrtTTy( a3, siseof(a3), HANDLE );
  }
  exit(1);               /*   楯窨  */
}

/* thread() */
/*    ࠭ */
void far thread()
{
   while(TRUE) {
    VioWrtTTy( b1, siseof(b1), HANDLE );
    VioWrtTTy( b2, siseof(b2), HANDLE );
    VioWrtTTy( b3, siseof(b3), HANDLE );
   }
}

     楯窠  뢮 ।   ᥬ,   㣠  
  "" ᯨ.  ᮦ,  ணࠬ 믮,
뢮  楯祪 ᬥ蠫  稫 ਬ୮ ᫥饥:

     ᯥ稢
    ᮢ   ᨣ
     ⥫   -    業
     ᠬ  楯窠.
        祣.

    ..    ⤥  ࠬ    ணࠬ஢,  饬   
ᮣᮢ ࠡ   楯祪.   室   ⥬,  
 楯窠    ᪠ 㣮:   " ,   ᥩ ᯮ
࠭,   ,   "   稫   ࠭, ⢮ ।".
.6-1 뢠,  ⠪ ᮣᮢ 룫廊 ᪨.

 1
  Ŀᯮ짮-Ŀᯮ짮-Ŀ
   ᠳ஢  ᠳ஢ 
  Ĵ           Ĵ           >
                      


 2
 Ŀᯮ짮-Ŀᯮ짮-Ŀ
 ஢  ᠳ஢  ᠳ 
Ĵ           Ĵ           Ĵ  
                       

         .6-1. ᪫饥 ᯮ짮 .

         ᯮ짮 ६  ᮣᮢ.

      砥  楯祪    । ᯮ짮 ⢥
쭮 ६   ᯥ祭  ⠪ .   ⠭
६    0   㤥  ,       ᢮     
ᯮ짮,      ६    ⠭    1 -  .
 楯窠  㤥 ⮢  ᯮ짮 ,   㤥 ஢
६.  ᫨  0, 楯窠 ⠭    1   筥 ᯮ짮
.   ᫨  1,  楯窠   ᢮    ᯮ㥬     
⠭      ᫥饩   ஢ન.       楯窠   砥
ᯮ짮 ,  ⠭ ६  0.
     奬  १,   쥧 ⪨.

          ⨢ .

      쥧   ஡     ᯮ짮  ६   -
⢨⥫쭮  砩 ᡮ.

    ।, 楯窨  1   2 訫  뢮    ࠭ (
ᯮ짮  -  㣮  ) ਡ⥫쭮 ६.
⨬,  楯窠  1  믮    ⠥  ६,   
।  ࠢ  㯮  ࠭.   ६ ࠢ 0,  
楯窠 ਭ 襭 뢮 ᢮   ᮡࠥ  ⠭
६  1,  ⮡ c஢,    .     
ᠬ  ஢騪  ࠥ ࠢ   楯窨 1   ।
 楯窥 2,  ⠥  ६  ⮦ 室   ࠢ 0,
⠭      1    稭  뢮  ࠭.   
楯窠 1 ᭮ 砥 ࠢ,  ⮦ ⠭ ६
 1  稭 뢮  ࠭.   १ - ⠫ 䫨.

    ஡  祭    - ஢ન ६ 
⮢       ६.        ࠧ   ⢨,
믮塞 ࠧ묨 ﬨ  .     ,
..  ࠧ.  ஢騪  ࢠ      
㣮   묨   ﬨ.   ᮣ᭮ ᫮  
, 楯窠 ࢥ  ࠧ  訬  ﬨ.  
  ਬ " ஭⮢", ᬮ७  ᫥ .  
ﭨ,        "  ஭⮢",   ஡, ..
砥 ,   㤭 ᫥.

    室  堭,  ஢     ६
⠭ ६.   ⥪ ࠦ "   ६"
砥,   㣮  ,     믮
 ⨬  ⢨ﬨ.

         ﭨ ⨢ .

        ஡  ᯮ짮  ⮩ ६
  ᮣᮢ   ᮢ.  窠,   㦨,     
ᯮ 㣮 楯窮,   ﭭ ஢  ६,
⮡  㧭,      ᢮.       
: ᫨  ﭭ  ஢ ६  横,  ᯮ
  ६.    㣮 ஭, ᫨ ࠡ 楯窨  㤥
ਮ⠭    DosSleep   ⪮  ६ ᫥ ஢ન
 ࠧ,     ६, ..    ⠭
㯭,    楯窨  ᫥  ⮣      .
室  ᯮᮡ  ਮ⠭        ࠡ
ࠧ ,  ⮫쪮  ⠭ 㯭.

          ᯮ짮 ६ 
            ᮣᮢ ᮢ.

     ᫨   ६     ᮣᮢ
楯祪, ⠭ ஡ ᨭ஭樨  ᮢ.     
襭   ६  ᥣ ᮢ⭮ ᯮ㥬  
(ᬮਬ  10- ),   㤭⢨,     砥
஡ ⠥.

          -   ᯠᥭ.

      -    ன⢮,  ᯮ㥬    OS/2     襭
ਢ  ஡.     -  ᮡ   ६ 
.     樮  ⥬,      ਪ  ணࠬ
ࠢ   ᥬ஬   १   ⥬   맮.     ..   ⥬
⠭  ஢ ᥬ ६,  .. ⥬  
  㢥७,      ᠬ    㤥  ࢠ 㣮 ணࠬ,
 ஫   ⠭     ࢠ.   , 
 ⠭ 㯭,   ନ  ⮬ 䠪
(  祩,    뢠    )     
 , 騩 .

      ࠧ,  ᯮ  ⥬  맮,  OS/2  ᯥ稢
⮥    䥪⨢  ᮣ  ᮢ.  蠥 ஡,
騥  ᯮ짮  権  ஢
- ஫    "⮣" 横 ⨢ .

          .

     ⮩   ᬮਬ ᥬ ,  ᯮ  
ᮣᮢ 楯祪.     ,     
६  , - ६, 㯭 ᥬ 騬.

      ⢥  樨   ᬮਬ  ணࠬ  xdisp.c - ਬ
ᮢ⭮ ᯮ짮 .  ᬮॢ  ,
  ᫥㥬      ᯮ짮  ᥬ஢:   
室 ᨣ஢   ᪮쪨 㣨 砬.  
ᬮਬ ⥬ ᥬ.

         ⢥   饬 .

      ஡, ।⠢  ணࠬ xdisp.c?  
    ணࠬ,  뢠騥  ᯮ짮 ᥬ  
ᮣᮢ 㯠  ࠭  楯祪.

/* xdisp2.c */
/* ᯮ짮 ᥬ  ᮣᮢ
             㯠  ࠭               */
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#define STACK_SIZE 1024   /* ⥪  楯窨 */
#define WAIT -1L          /* 1 = , 0 = ,
                             >0 = ᥪ㭤 */
#define HANDLE 0          /* १  ᥣ 0 */

unsigned long semaphore = 0; /* ᥬ  */

char a1[] = " ᯥ稢\n\r";
char a2[] = "ᮢ   ᨣ\n\r";
char a3[] = " ᠬ  楯窠.\n\r\n\r";
char b1[] = " -  ண㫪,  \n\r";
char b2[] = " ⥫   -    業\n\r";
char b3[] = "    祣.\n\r\n\r";

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far thread(); /* ⨯   楯窨 */
 unsigned threadID; /*  楯窨 ID */
                    /*  ⥪,  祯 */
 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread, &threadID, stkptr);
                         /*    ࠭ */
 while( !kbhit() ) {     /*        */
   DosSemRequest( &semaphore, WAIT );
   VioWrtTTy( a1, siseof(a1), HANDLE );
   VioWrtTTy( a2, siseof(a2), HANDLE );
   VioWrtTTy( a3, siseof(a3), HANDLE );
   DosSemClear( &semaphore );
  }
  exit(1);               /*   楯窨  */
}

/* thread() */
/*    ࠭ */
void far thread()
{
   while(TRUE) {
    DosSemRequest( &semaphore, WAIT );
    VioWrtTTy( b1, siseof(b1), HANDLE );
    VioWrtTTy( b2, siseof(b2), HANDLE );
    VioWrtTTy( b3, siseof(b3), HANDLE );
    DosSemClear( &semaphore );
   }
}

      ⠭  ᥬ    ணࠬ  ᤥ  ⮫쪮  ᪮쪮
.  ᥬ -  쭠 ६,    砫
⨭.        ⨯  unsigned  long.   ࠢ 0,
 㯥.    ࠢ 0,   ᯮ.   
६ ⠭   0  । ᯮ짮,    
ਬ.   ᫥ ⮣   祭      ⮫쪮
ᥬ묨 㭪ﬨ API.

       䫨  ᥬ஢    ।
⨯:

    semaphore=1; /*  */

    ⨭,  ᥪ樨 ணࠬ, ᮤঠ饩 㭪樨
WioWrtTTy(),  ।  㭪  DosSemRequest(),    ᫥  ᥪ樨
᫥ 㭪 DosSemClear().  楯窠, । ⥬   뢮
 ࠭, 뢠 DosSemRequest().   ᫨ ᥬ ⠭  0  (
""),    뢠,    㯥.   DosSemReuest()
⠭ ᥬ   1  ()   ࠢ ।  楯窥.
窠  ⮣ 믮  㭪樨 WioWrtTTy(). ᫥ ⮣ 
뢠  DosSemClear(),   ⠭  ᥬ ⭮  0 -
ᯮ㥬.

    ।,    楯窠 믮 㭪  DosSemReuest(),
ᥬ  (1).  ⮬ 砥 㭪  , ࠢ 
頥   楯窥.   頥 ,   ⮫쪮  ᥬ
⠭ ࠢ 0, ..  প  㯮 楯窨  ࠭.

 ⮬  砥    ஧     ᬥ訢. 뢮  xdisp.c
㤥 룫拉 ⠪:

 ᯥ稢
ᮢ   ᨣ
 ᠬ  楯窠.

 -  ண㫪   ,
 ⥫   -    業
    祣.

    㭪 DosSemRequest() ᯮ   㬥.   - 
ᥬ .  ⨯ - void far. 祬?  ᥬ - unsigned long,
   ।,      unsigned long  far.  뫮 
⢨⥫쭮   砥  - ᥬ஢.   DosSemRequest()
  㣨   ᥬ  㭪樨   ᯮ  ⠪     ⥬
ᥬ஢.    ⮬  砥  ᥬ    㣮 ⨯. ᯮ짮
㪠⥫  ⨯  void    DosSemRequest()   㣨 ᥬ
㭪   ᯮ㥬묨    -  ᥬࠬ, ⠪   ⥬묨
ᥬࠬ,    室  ਢ  ⨯ ᥬ .
(ᬮਬ    ࠧ "⥬ ᥬ").

         DosSemRequest() -  ⪨ ᥬ஢,  ⠭.
Ŀ
DosSemRequest(SemHandle, Timeout);                    
void far *SemHandle;  /*    ᥬ */  
long Timeout;         /* 1 = , 0 = ,      
                        >0 = ᥪ㭤        */   

    ன   ࠬ      DosSemRequest()      ⨯  long  integer
뢠,      ந室,   ᫨   ᥬ     (㫥).
 3 :

    -1 - 窠 "ᯨ"  ⪨ ᥬ;
     0 - 㭪   ࠢ 楯窥;
    >0 - 祭 - ᫮ ᥪ㭤, ᪮쪮 㤥 
         㭪  饭 ࠢ 楯窥.

        ୠ⨢    楯窨  -      ⪨
ᥬ.     ᬮਬ ਬ,  ᯮ  
 ࠢ.

                           ᥬ 饭
                          V
Ŀ  >
  ᥬ ⠭        <-- ᥬ 
                                   ⠭
                          
                            
  Ŀ    楯窠 믮
> 楯窠 ஢     >
   
   楯窠 믮
     DosSemRequest()

         .6-2. ⢨ DosSemRequest()


    .6-2 뢠 ,   ᥬ 砫쭮  ⠭ 
楯窠   DosSemRequest().  ᥬ 饭 㣮  楯窮,
㭪  DosSemRequest()  ⠭        "" 
। ࠢ 뢠 楯窥.

         DosSemClear() -  ᥬ.
Ŀ
DosSemClear(SemHandle);                               
void far *SemHandle;   /*    ᥬ */ 

    㭪 DosSemClear() ᯮ  ⢥ ६ -  
ᥬ.  DosSemClear()    ᥬ     ᨬ     
।饣 ﭨ.  ﭨ   楯,    맮,
  ⪠  ᥬ    ⠢    楯,   
ᥬ,  ᢮  DosSemRequest()   ᭠砫.

         ᮢ 㯠  饩 

  㣠  ,    ன  ᥬ   ᯮ짮 
㯠  饬 .  ⮬ 砥  -    .

    ।,    ᮤন     . ᪮쪨 砬
室       ,     ᪮쪨   -       .
(।⠢,     㯭 ᪮쪨 ନ   筮
⤥. ᫨   ࠧ襭 ⥭  ,      ६
㣠        ,         ""   
᪠묨  묨,  ..    ६  ⥭      
ࢠ  쭮    ६  祩,  饩  ,   
⠥     㣮 祫.   ࠧ,  
  㢥७,    ࠧ ⮫쪮    
 . ( ⢨⥫쭮  ।, ᫨  祬   
ணࠬ          ६,   㥬  
  ᫨誮 ).

    騩  ਬ            
楯窨,    ⠥  ,      㣨,   ᮢ⭮
  㤠   .     ⮨     ,  ᮤঠ饩
ଠ    ⥫  -  ஬:  ,  ஦  ⪠
ࠪ⨪.

/* dbsen.c */
/* 饤㯭  */
#define INCL_DOS
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#include <string.h>       /*  strcpy() */
#define STACK_SIZE 1024   /* ⥪  楯窨 */
#define WAIT -1L          /* 1 = , 0 = ,
                             >0 = ᥪ㭤 */
struct personnel {   /*   ଠ樨
                           ஬           */
  char name[30];     /*                   */
  int birth;         /*  ஦        */
  char descr[40];    /* ᠭ             */
} novelist;          /* ᮢ⭠  */

unsigned long semaphore = 0; /* ᥬ  */

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far thread1(); /* ⨯   楯窨 */
 void far thread2();
 unsigned threadID; /*  楯窨 ID */

 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread1, &threadID, stkptr);

 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread2, &threadID, stkptr);

 while( !kbhit() ) { /* 横     */
   DosSleep( 200L ); /*  㯨 樨 */
   DosSemRequest( &semaphore, WAIT );
   printf("\nName   %s\n",novelist.name);
   printf("DOB:   %4d\n",novelist.birth);
   printf("Descr:  %s\n",novelist.descr);
   DosSemClear( &semaphore );
  }
  exit(1);               /*   楯窨  */
}

/* thread1() */
/*  묨  */
void far thread1()
{
   while(TRUE) {
    DosSemRequest( &semaphore, WAIT );
    strcpy( novelist.name, "ଠ 좨" );
    novelist.birth = 1819;
    strcpy( novelist.descr, "९⥫  ⨪" );
    DosSemClear( &semaphore );
   }
}
/* thread2() */
/*  묨  */
void far thread2()
{
   while(TRUE) {
    DosSemRequest( &semaphore, WAIT );
    strcpy( novelist.name, " 榥ࠫ" );
    novelist.birth = 1896;
    strcpy( novelist.descr, " 樠" );
    DosSemClear( &semaphore );
   }
}

     ।頥 ⢨  楯窨 (    
    )     ,   㣠 ᯮ  . 
ᥬ  뢮  ணࠬ  ९⠥,    祬   㡥,
㤠  ᥬ  㭪樨    믮  ணࠬ.   ᥬ஢ 
  -   ⠪ த:

    :  榥ࠫ
     ஦: 1896
    ࠪ⨪: ९⥫  ⨪

   榥ࠫ         ९⥫    ⨪,   ⠪
ଠ   㤮⢮.

     㢨  ⠪:

    :  榥
     ஦: 1819
    ࠪ⨪: ९⥫  ⨪

        ஬  ⠫   ਤ.     ⠭   
ࠡ ᥬ 㭪 楫⭮ 뢮 ࠭:

    : ଠ 좨
     ஦: 1819
    ࠪ⨪: ९⥫  ⨪

    :  榥ࠫ
     ஦: 1896
    ࠪ⨪:  樠

     쭥襬  㢨 㣨 ਬ ᯮ짮 ᥬ஢  
㯠  饬 .

         ⪠ 䨣

    㭪 DosSleep() ⠢   㭪 main()  ணࠬ dbsemc.c
 ࠭  ⪮ 䨣樨   ࠧ稢.    ⪮
䨣樨 믮騥  楯窨 ᥣ     ६ 
᫥饬 浪: 楯窠 1, 楯窠 2, 楯窠 3; 楯窠 1, 楯窠
2, 楯窠 3,   ⠪ .   ।⠢ ਬ   ⨬, ⮡
 楯窨  1   2         । ⥬, 
楯窠  main()  砥  ᢮     ६    ⮡ࠦ   .
ᯮ짮 DosSleep() ⠢ 㭪 main()   ࠡ
    ६,    ᮢ騩    ᨣ  砫  뢮
ଠ樨 㣮 楯窨.

          NO WAIT -  

    㬥  Timeout   㭪樨  DosSemRequest()      ணࠬ
    饭    㭪樨,  ᫨ ᥬ
.   , ᫨   ,  ⮡ 楯窠  -  
६  .

    ᫨    ।    NO  WAIT    ᥬ  ,  㭪
 頥  121  ERR_SEM_TIMEOUT.

      ९ᠭ  ணࠬ  dbsem.c.     ⠥,  
 짮⥫. ᫨  짮⥫  ᬮ  ᮤন
 ,   "". ᫨ 㣠 楯窠 ᯮ ,
ᮤন ⠥.  ⨢ 砥 뤠 ᮮ饭 "
 ⮢. ஡  ࠧ".

/* nowait.c */
/* 饤㯭  */
/* 楯窠   ᢮ ᥬ */
#define INCL_DOS
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#include <string.h>       /*  strcpy() */
#define STACK_SIZE 256    /* ⥪  楯窨 */
#define WAIT -1L          /* 1 = , 0 = ,
                             >0 = ᥪ㭤 */
#define WAIT -0L          /* , ᫨ ᥬ
                                               */
#define ERR_SEM_TIMEOUT 121  /*   ᫨ ᥬ
                                                   */
struct personnel {   /*   ଠ樨
                           ஬           */
  char name[30];     /*                   */
  int birth;         /*  ஦        */
  char descr[40];    /* ᠭ             */
} novelist;          /* ᮢ⭠  */

unsigned long semaphore = 0; /* ᥬ  */

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far thread1(); /* ⨯   楯窨 */
 void far thread2();
 unsigned threadID; /*  楯窨 ID */
 unsigned rc;       /*   */

stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread1, &threadID, stkptr);

 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread2, &threadID, stkptr);

 while( TRUE ) {
   printf("\n ""  ⥭ : ");
   if (getche() != '\r')  /* 室 ᫨  "" */
       exit(0);
   if ( rc=DosSemRequest( &semaphore, NOWAIT ) )
      if (rc==ERR_SEM_TIMEOUT)
        printf("\n  ⮢. ஡ ᭮.\n");
      else {
        printf("\nDosSemRequest error=%u\n");
        exit(1);

      }
   else {

   printf("\nName   %s\n",novelist.name);
   printf("DOB:   %4d\n",novelist.birth);
   printf("Descr:  %s\n",novelist.descr);
   }
   DosSemClear( &semaphore );
  }
  exit(1);               /*   楯窨  */
}

/* thread1() */
/*  묨  */
void far thread1()
{
   while(TRUE) {
    DosSemRequest( &semaphore, WAIT );
    strcpy( novelist.name, " ࠤ" );
    novelist.birth = 1857;
    strcpy( novelist.descr, "⠭ ᪮ 樨" );
    DosSemClear( &semaphore );
   }
}
/* thread2() */
/*  묨  */
void far thread2()
{
   while(TRUE) {
    DosSemRequest( &semaphore, WAIT );
    strcpy( novelist.name, "୥ " );
    novelist.birth = 1896;
    strcpy( novelist.descr, ", 론, ⭨" );
    DosSemClear( &semaphore );
   }
}

     ਬ ணࠬ 뢮:

 ""  ⥭ :
  ⮢. ஡ ᭮.

 ""  ⥭ :
  ⮢. ஡ ᭮.

 ""  ⥭ :
:  ࠤ
 ஦: 1857
ᠭ: ⠭ ᪮ 樨

      ⮬ ண       ࠧ,   ⨩  ࠧ
 ᢮   ᯮ짮 楯窮 1.

     室  ணࠬ  Ctrl-C.

         ஡ ⪨  

     ᫥   ᬮ५ ணࠬ readwrite.c,   ன
 楯窠 頥      ,  㣠    
  ⮡ࠦ  ࠭.  ,     ஡
- 몮  ணࠬ 뢮  ணࠬ ,  ⠭ 
㪠⥫  砫 ,  - 쭮 ⮥ .  ᫨ 
楯窠 㤥  ࢠ 饩   ⨬   蠣,  
᫮.

     ᫥饬  ਬ   ஡ 蠥    ᥬ஢,
  ᯮ    ।饭  ६  㯠  
  楯窠.

/* rwsem.c */
/* 1 -  楯窠 ⠥ , 2 -    ࠭ */
/* ᯮ짮 ᥬ஢    ஭⮢ */
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>     /*  malloc() */
#define STACKSIZE 1024  /* ࠧ ⥪   楯窨 */
#define ARRAY_SIZE 50   /* ࠧ ᨬ쭮  */
#define HANDLE 0        /*  kbd ᥣ 0 */
#define KBDWAIT 0       /* 0=, 1=  */
#define FREQ 1000       /*   DosBeep */
#define DURATION 100    /* ⥫쭮 DosBeep */
#define SEMWAIT -1L     /* -1 = , 0 =  ,
                           >0 = ᥪ㭤 */

char charbuff[ARRAY_SIZE]; /* ᨬ  */
char *writeptr;            /* 㪠⥫  charbuff */
char *readptr;             /* 㪠⥫  charbuff */
unsigned long sem;         /* ᥬ */
main()
{
  struct _KBDKEYINFO keydata; /*   bsesub.h */
  char far *stkptr;    /* 㪠⥫   ⥪ */
  unsigned writerID;   /* 䨪 楯窨 */
  void far writer();   /*  writer() */

  readptr = charbuff;  /* ⠭ 㪠⥫ */
  writeptr = charbuff; /* 砫  */
                       /* 砫 饩 楯窨 */
  stkptr = (char far *)malloc(STACKSIZE) + STACKSIZE;
  DosCreateThread(writer, &writerID, stkptr);
  while (TRUE) {
    KbdCharIn(&keydata, KBDWAIT, HANDLE); /*  ᨬ */

    DosSemRequest( &sem, SEMWAIT );
    if (readptr < charbuff + ARRAYSIZE ) /*᫨  */
       *readptr++ = keydata.chChar; /*    */
    else                                 /*  */
    DosBeep( FREQ, DURATION ); /*  㪠 */
    DosSemClear( &sem );
  }
}

/* writer() */
/*  ᨬ    ࠭ */
void far writer()
{
   while ( TRUE ) {
     if ( writeptr < readptr ) {            /* ᫨ ᨬ */
       VioWrtTTy ( writeptr++, 1, HANDLE ); /* 뢥  */
       DosSemRequest ( &sem, SEMWAIT );
       if ( writeptr==readptr ) { /* ᫨ 墠⨫ */
           DosSleep(200L);        /*  嫮 */
          writeptr = readptr = charbuff; /* ⠭ */
                                         /* ptrs          */
       }
    DosSleep(200L); /* প   ⨯ */
    }
}

      ணࠬ ꥤ প  楯窥 뢮, 
  ᯮ㥬    ᫥      ⢥ 樨 㤠筮
ࠡ ணࠬ.    砥 ணࠬ ⫨筮 ࠡ⠥,  ..
樨  ஢ન    ⠭  뢮饩  楯窨      
ࢠ 饩. ஢ .   ஡ -    
믠訥 ࠭ ᨬ.

            

    ஥  ᯮ짮 ᥬ஢ ᫥ ᮣᮢ 㯠-
ᨣ.     -    ⮩  ᯮᮡ   
    㣮.  (  ᬥ訢    ⨥   ନ
"ᨣ" 楯樨 ᨣ, ⠪,  Ctrl-C, ᬮ७   
12).    稭,  祬    뢠  室 
ᨣ  㣮.    ᬮਬ      ᪮쪮  ⥩     ⮬
ࠣ.

      ᥬ  ᯮ    ᮢ⭮  㯠    騬
ᠬ,  室  API  -  㭪  DosSemRequest(), ..  ⮫쪮
ᥬ  頥   㣨  ᮬ,   DosSemRequest()   
⠭  ᭮, ⠪  ⮫쪮      ६
砥    饬  .   㣮 ஭,   ᥬ
ᯮ     ᨣ樨,     ,     ᪮쪮
ࠧ  楯祪      饭   饭  믮,
 ⨫   ᥬ.  ⮬  ᯮ ࠧ API  -
㭪樨  ᨣ樨 - DosSemWait()  DosSemSetWait() , 祬
DosSemRequest().

    ᬮਬ  ਬ.     ⮩   ணࠬ     楯窠   ⠥
, 㣠  ⠥ ᮮ饭   ࠭.   ⠪  ࠧ
ࠡ      楯窠      ⠪   쪮   ணࠬ   
室,    쥧 饩  룮.   楯窠
 ।   ⥭ ⮩    吝   - 
,    㣠  楯窠     ६ ᫨ 㦭 
䨪 ࠭.

     襬 ਬ "ᯫ 楯窠" ⠥  ᪠  
짮⥫,       "ୠ"   楯窠    墠뢠    ⢥
짮⥫.   ᪠ ᫥  ⢥, ⥬  ᭮ ᪠  
..  ᮮ⢥饥 ࠢ ᥬ஬:

/* alert.c */
/*  楯窠 ᨫ 㣮 १ ᥬ */
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>     /*  malloc() */
#define STACKSIZE 1024  /* ⥪   楯窨 */
#define SEMWAIT -1L     /* -1 = , 0 =  ,
                           >0 = ᥪ㭤 */
#define KBDWAIT 0       /* 0=, 1=  */
#define BUFFLEN 80      /*  室 ep */
#define HANDLE 0        /* १  ᥣ 0 */

char name[BUFFLEN];     /*       */
char position[BUFFLEN]; /*   樨  */
char age[BUFFLEN];      /*    */

unsigned long semaphore = 0; /* ᥬ  */

main()
{
  char far *stkptr;    /* 㪠⥫   楯 */
  void far keyboard(); /* ⨯   楯窨 */
  unsigned threadID;   /*  楯窨 ID */
                       /* 砫 楯窨  */
  stkptr = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
  DosCreateThread(keyboard, &threadID, stkptr);
                       /* 砫  */
  printf("  : ");
  DosSemSetWait( &semaphore, SEMWAIT );
  printf("  : ");
  DosSemSetWait( &semaphore, SEMWAIT );
  printf("  : ");
  DosSemSetWait( &semaphore, SEMWAIT );
  printf("At %s,  ᫨誮   \n", age );
  printf("⮡  %s, %s?", position, name );
  exit(0);
}

/* keybord() */
/* 祭    */
void far keybord()
{
  gets(name);
  DosSemClear( &semaphore );
  gets(position);
  DosSemClear( &semaphore );
  gets(age);
  DosSemClear( &semaphore );
  DosExit(0,0);
}

    1 -  楯窠 ⠥ ᪠   ⪨ ᥬ.   
砥  ⢥  짮⥫    頥  ᥬ,  ࠧ   ࢮ
뤠 ᫥ ᪠.   ਦ   ,  
1 -  楯窠  뤠   ᮮ饭, 饥  짮⥫
 ଠ.   ਬ ࠡ  ணࠬ:


  :  
  : ணࠬ
  : 23
 23  ᫨誮   
⮡  ணࠬ⮬,  ?


     ⮩ ணࠬ   ᯮ㥬 㭪 DosSemSetWait()   
楯窥.   㭪 ⠭  ᥬ   ६ ᪠
楯,    ⪨  ᥬ.      ⠪ ᯮ짮
㭪 DosSemWait(),  ࠡ⠥ 筮,   ⠭
ᥬ,  믮  ।⥫쭮  㭪  DosSemSet().    
ਠ  ਥ   砥.

    DosSemSetWait() - ⠭ ᥬ   ⪨.
Ŀ
DosSemSetWait(SemHandle, Timeout);                           
void far *SemHandle; /* ᥬ    */        
long Timeout;        /* -1=, 0=, >0=.ᥪ */ 


ଠ 맮  祭 DosSemRequest(),  ⢨ ⠪  筮.
    ,  ⢥  ⫨稥  -  ,   DosSemSetWait() 
⠭  ᥬ,       ⨫.   .6-3   뢠
⢨ ⮩ 㭪樨.

                                       ᥬ 饭
                 ᥬ ⠭    V
               Ŀ
                                      
ᥬ 饭                        
  㭪 ⠭ >
               ! ᥬ               !
 楯窠       Ŀ      楯窠
 > 楯窠 ஢   >
 믮       믮
                楯窠 믮
                          DosSemSetWait()


         .6-3. ⢨ DosSemSetWait()

      짮⥫  ⠥  ⢥   ᪠,  楯窠
 ᥬ    DosSemClear().    1 -   楯窥
 ᫥ ᪠.

          ᥬ ᨣ ᪮쪨 楯窠

     ।饬 ਬ ⮫쪮  楯窠  ⪨ ᥬ.
 ⠪ ४祭 ᪮쪨 楯祪.  ᫥饩  ணࠬ
  ࠧ   楯祪     믮,     ⮫쪮  ᥬ
.     ணࠬ  -   ⥫쭮     ணࠬ
horsrace.c  ᫥  ; ⢥ 䨪  - 
ᥬ.  ᥬ ᯮ  ࠭⨨ ࠢ  
.  ࠭ ᨨ  ணࠬ  蠤 稭  , 
⮫쪮 ନ஢ ᮮ⢥ 楯窠.   ࠢ
२⢮  (    讥)  ⥬  蠤,  ନ஢
묨.

     ணࠬ horstart.c  楯窨 - 蠤 ᮧ ࠭,  
     ᥬ  ।   砫  .      ணࠬ
稢 ᮧ   楯祪   ⮣  頥 ᥬ, ࠧ
ᥬ   蠤   ⮢   ६.   蠤       
ᥪ㭤 ࢠ    ࠭,  ⥬ ᪠  
"⮢ ".  ⨭:

/* horstart.c */
/* ᥬ ᯮ    */
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#define STACK_SIZE 400    /* ࠧ ⥪  楯窨 */
#define NUM_HORSES 5      /* ᫮ 蠤 */
#define FINISH_LINE 75    /*  ஦ */

unsigned long StartSem=0; /* ᥬ    */
int dist[NUM_HORSES];     /* ⠡ ⥪饩 ⠭樨 */
char share[] = "\xDD\xDF\xDD\xDF"; /* */

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 unsigned threadID; /*  楯窨 ID */
 unsigned rc;       /*   */
 int j;             /* ६ 横  䨪  */
                    /*      楯窨           */
 int blank = 0x0720; /* ଠ ਡ    */
 struct _DATETIME TimeBuff; /*    ६ */
                            /* ⪠ ࠭        */
 VioScrollUp(0,0,-1,-1,-1, (char far *)&blank,0);
 VioSetCurPos(0,0,0); /* ⠭    */
                      /*    㣮                   */
     /* ᯮ짮 ६  砩 ⠭ */
 DosGetDateTime( &TimeBuff);
 srand( (unsigned)TimeBuff.hundredths );
    /* ⠭ ᥬ,  믮 楯窠 */
 DosSemSet( &StartSem );
             /* 横 ᮧ  蠤 ⤥쭮 */
 for (j=0; j<NUM_HORSES; j++);
                             /*   ⥪ */
    stkprt = (int far *)malloc(STACK_SIZE) + STACK_SIZE;
    *--stkptr = j; /* j - 㬥 楯窨  ⥪ */
                   /* 砫  楯窨 */
    if (rc=DosCreateThread(horse,&threadID,(char far
      *)stkptr))
       {printf("DosCreateThread error=%u",rc); exit(1); }
    DosSleep( 500L ); /* ᮧ 蠤  */
 }    /* ⪠ ᥬ    */
 DosSemClear( &StartSem );
 DosExit(0,0);  /*   楯 */

/* horse() */
/*   ⮩ 楯窨 ।⠢  蠤 */
void far horse(horseID)
int horseID;  /* 䨪 ⮩ 蠤 */
{
  int blank = 0x0720; /* ଠ ਡ */

  while(TRUE) {
           /*    ᮢ 蠤 */
    VioWrtNCell((char far *)&blank,80,horseID*2,0,0);
    VioWrtCharStr((char far *)shape,4,horseID*2,
                                 dist[horseID],0);
           /*  砫  */
    DosSemWait(&StartSem, -1L);
           /*  蠤 */
    dist[horseID] += rand() % 4; /*   0,1,2,3 */
                 /* 롮 먣ࠢ襩 蠤 */
    if(dist[horseID] >= FINISH_LINE) {
      VioSetCurPos(20,0,0); /*   -  */
      printf("蠤 %d !!!\n", horseID);
      exit(0);  /*   蠤 */
      }
    DosSleep( 200L ); /* ঠ 蠤 */
  }
}

      ணࠬ  ࠡ⠥  ⠪  ,    horsrace.c,  襭
ਠ  ࠭  ࠢ  .    ᪮쪮   楯祪
          ᥬ,  㤭  ।᪠,   
  ᢮  ᥬ, ..     ⮣,  
楯窠 ஢  ।.

     ⮩ ணࠬ 楯窠,  ᥬ,  ⠭ .
 楯窠 ⠭  ᥬ    ᢮ ⠫樮
.  ᮧ  ⠫ 楯窨,   ⥬  ᥬ  
 .  .. 㣨  楯窠   㦭 ⠭  ᥬ,
     稭  ,     ᯮ   DosSemWait()  
DosSemSetWait().

         DosSemWait() -  ⪨ ᥬ
Ŀ
DosSemWait(SemHandle,Timeout);                         
void far *SemHandle; /* ᥬ    */  
long Timeout; /* -1=, 0=, >0=ᥪ. */  

     㭪 室  DosSemRequest()  DosSemSetWait().
⢥ ࠧ稥  DosSemSetWait() - ,    ⠭
ᥬ, 뢠 ࢮ. . 6-4 뢠 ⢨.

                                       ᥬ 饭
                 ᥬ ⠭    V
 Ŀ
                                       
                                       
                                       >
                                       !
               Ŀ      楯窠
 > 楯窠 ஢   >
 믮       믮
                楯窠 믮
                          DosSemWait()


         .6-4. ⢨ DosSemWait()

          楯窠  ᪮쪮 ᥬ஢

     ᬮ५,  ⪮  ᥬ   
 楯窨.  ⭠ :  楯窠   
⪨  ᪮쪨  ᥬ஢.    砥, ᫨ ਬ 楯窥
㦭    ,             
ࠩ஢ 稫   䠩,   ࠩ 易  ࠧ
ᥬ஬.

      ⮩  楫  ᯮ  API -  㭪 DosMuxSemWait(). 
믮  ஫,  室    DosSemWait(),     
ᥬ,    ᪮쪮  頥  ࠢ,    
 .

      㭪樨    ᫥騩  ਬ.  楯窠
ᮧ    ᥬ    ᪠    ୨  楯窨.    ⥬
ᯮ  DosMuxSemWait()  -  ,      㣠 
楯窠 .   楯窠  䨪஢ ६  
⥬  ᥬ.    -   ᥬ ,  
楯窠 "믠"  ᯫ 뤠 ᮮ饭.  ⨭:

/* mux.c */
/*  㭪樨 DosMuxSemWait() */
#define INCL_DOS
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#define STACK_SIZE 1024   /* ⥪  楯窨 */
#define WAIT -1L          /* 1 = , 0 = ,
                             >0 = ᥪ㭤 */

unsigned long s1 = 0; /* 1 -  ᥬ  */
unsigned long s2 = 0; /* 2 -  ᥬ  */

struct  {              /* ᯨ᮪ DosMuxSemWait() */
   unsigned SemCount;  /* ᫮ ᥬ஢ */
   unsigned res1;      /* sem1 -   0 */
   unsigned far *ps1;  /* sem1 - 㪠⥫  ᥬ */
   unsigned res2;      /* sem2 -   0 */
   unsigned far *ps2;  /* sem2 - 㪠⥫  ᥬ */
} list;

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far thread1(); /* ⨯   楯窨 */
 void far thread2(); /* ⨯   楯窨 */
 unsigned threadID; /*  楯窨 ID */
 unsigned rc;       /*   */
 unsigned index;    /* ᥬ   */

 list.SemCount = 2; /* ᯨ᮪ 樠樨 */
 list.res1 = 0;
 list.ps1 = (unsigned far *)&s1;
 list.res2 = 0;
 list.ps1 = (unsigned far *)&s2;

 DosSemSet( &s1 ); /* ⠭ ᥬ஢ */
 DosSemSet( &s2 );

 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread1, &threadID, stkptr);

 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(thread2, &threadID, stkptr);

 if(rc=DosMuxSemWait(&index, (unsigned far *)&list,WAIT))
    {printf("DosMuxSemWait error=%u\n", rc); exit(1); }

 printf(", 墠⨢訩 ᮡ⨥ - %d\n",index);

 DosSleep(4000L);  /*  室  楯窨 */
 exit(0);          /*   楯窨 */
}

/* thread1() */
/* প । ⪮ ᥬ s1 */
void far thread1()
{
  DosSleep(3000L);    /* 㧠 */
  DosSemClear( &s1 );
  DosExit(0,0);       /*  ⮩ 楯窨 */
}

/* thread2() */
/* প । ⪮ ᥬ s2 */
void far thread2()
{
  DosSleep(2000L);    /* 㧠 */
  DosSemClear( &s2 );
  DosExit(0,0);       /*  ⮩ 楯窨 */
}

     ⮩  ணࠬ 楯窠  2,    প, ࢠ
 ᥬ.   ,  楯窠 ⠥ ᮮ饭:

    " ⬥⨫ ᮡ⨥ 1"

祭 , ࠢ 1, ᮮ⢥ 楯窥 2. ᫨   
  প    㣮   楯窨,  ᮮ饭     
ᮮ⢥騬 ࠧ.  㧭 ணࠬ,  ᥬ  砥
㭪 DosMuxSemWait()?  ᬮਬ  㬥 㭪樨.

         DosMuxSemWait() -  ⪨   ᪮쪨
                           ᥬ஢.
Ŀ
DosMuxSemWait(Aindex, Alist, Timeout);                        
unsigned far *Aindex; /*  ࠧ饭   */   
void far *Alist;      /*  ᯨ᪠ ᥬ஢ */            
long Timeout;         /* -1=, 0= , >0=ᥪ.*/ 



    1 -  㬥 -    - , 頥  㭪樥.
 ,   ᥬ஢  ᯨ᪠ ᥬ஢ (ᯨ᮪ 㢨  
쭥襬) 맢  㭪樨. 㬥  稭  0  1  -
 ᥬ  ᯨ᪥.  襬 ਬ ⮫쪮  ਠ: 0  1.

    ன 㬥  -   ᯨ᪠ ᥬ஢.   ᥬ, 
 㭪.  ᮪ 稭   室  ᫮, ஥ ᮤন
᫮  ᥬ஢    ᯨ᪥.    ᥬ  ॡ  ᫮, ஥
१ࢨ஢    ᥣ  ࠢ  0,      ᫥  ᫮,
ᮤঠ饥 쭨  㪠⥫   ᥬ.   ᫮ + 
᫮    室.

     ਬ  mux.c   ⨫ ,   ᮤন ᥬ
।⢥    ணࠬ.  ६    -   
ᯨ᮪.   ᫨    室    ,  ⮡  ঠ
襥  ᫮   ᥬ஢,               
⥫ ப  ᥬ.

        㣠        ⠭    ᯨ᪠    ᥬ஢.
⮢᪨    䠩  bsedos.h  ।    :
MUXSEM,  ᮤঠ  ଠ      ᥬ,   MUXSEMLIST,
ᮤঠ 稪    16  MUXSEM.  ⥬ 
ᯮ짮  ࠧ饭   אַ  ணࠬ,   
.    ⠪    -  DEFINEMUXSEMLIST  bsedos.h, ஥
 ᯮ짮  ᮧ   MUXSEMLIST.

    ⨩ 㬥  DosMuxSemWait() - ᥬ⭠ প.    
 㣨  ᥬ 㭪   ।,  㤥   㭪 
⪨ ᥬ,  ,  㤥  ।
६,   ᥪ㭤.

    ᮡ  㭪樨  DosMuxSemWait()    ⮬,   ᪠
஭⮬ ᨣ,   ६  㣨 ᥬ 㭪樨  ᪠
஢.    ?   -    ᥬ 㭪権,
⠪   DosSemWait(), DosSemSetWait()   DosSemRequest()  
ᥬ,       ⠢  ,   㭪 
  ,    㭪樨      ࠢ. ᫨ ᥬ
⨫      ᭮  ⠭    ⮣,   㭪, 
DosSemRequest()  ᨫ  ,  㭪  㢨  ⮫쪮  ,    
⠭,   ᤥ  .         )    )
.6-5.

     㣮  ஭   㭪樥 DosMuxSemWait()  ᥬ 㦤 
⪥ ⮫쪮  ,   㭪 ।    饭.
 ᫨ ᥬ ⠭ । ⥬,  DosMuxSemWait()  
  , 㭪 ᤥ ,  ᫨  ᥬ
 .     ) .6-5.

 Ŀ      >
    ᥬ        楯窠 頥 ࠢ
              ࠧ
            V               V
 Ŀ
        楯窠 ஢ >
   楯窠 믮

    ) ४祭  ஭, ⪨ .


 Ŀ      >
    ᥬ        楯窠 頥 ࠢ
               ࠧ
            V               V
 Ŀ
        楯窠 ஢                    >
 

    ) ४祭  ஢, ⪨ .


 Ŀ              >
    ᥬ                楯窠 頥 ࠢ
              ࠧ Ŀ
            V            
 Ŀ
     楯窠 ஢ >
   楯窠 믮

    ) ४祭  ஢,  .

         .6-5. ४祭  ஭   ஢.


          ᫮ ᮢ ᨣ

    騩 ਬ  뢠,   ࠧ    楯窨
  ஢   㣮,   ⮬ न஢ ᢮
⢨, ᯮ ᥬ.

     ணࠬ -  " ". ࠡ, ࠦ  5-
᪨ ᨬ,    ।     ࠭.
 , ࠢ塞 蠬 ,   - ।
 㣮 . ௥  ᪠     ⨥
 "५ ". ᫨    筮   ࠡ 
᪥ ௥, ࠦ ࠡ 뢠 , ࠧ 
    ࠭    ᮮ饭.  .6-6  뢠  ࠡ  
 .

  Ŀ
                                      
                                     
                 ࠡ       
                                      
                                      
                                      
                                     
                                     
                        
                                      
                                      
  

         .6-6.  " ".

     ணࠬ         ࠡ  ।⠢   ࠧ묨
楯窠.   ணࠬ, ।祭   㪠
 뢮 ᯥ譮  ᮮ饭  ࠭,  ⠪ ⤥쭠 楯窠.  
楯窨 易 ᥬࠬ.      ५ ௥,
  ᨣ    ⮬  ᮡ⨨  ᯮ  ᥬ.  ࠡ ⮣
ࠢ ᢮ ⮯   ௥, ⮡  ।,
       .   ᫨  ,   ᯮ  ᥬ   
ᨣ樨    ⮬    楯窥,    ᮮ頥  䠪
짮⥫.

/* sub.c */
/*    - 㭪樨 DosSemWait()*/
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <malloc.h>       /*  malloc() */
#define STACK_SIZE 1024   /* ⥪  楯窨 */
#define SEM_WAIT -1L      /*  ᥬ */
#define SEM_NO_WAIT -0L   /*  ᥬ */
#define SHIPLINE 10       /* ࠡ 㤥  ⮩  */
#define SUBLINE 13        /*  㤥  ⮩  */
#define HANDLE 0          /* १  ᥣ 0 */
#define MIDDLE 40         /* 業ࠫ ⮫ ࠭ */
#define FREQ 256          /*   DosBeep() */
#define DURATION 1000     /* ⥫쭮  DosBeep() */

unsigned long FireSem =0; /* 饭,  饭 ௥ */
unsigned long SunkSem =0; /* 饭,  ࠡ  */
int torpedopos;           /*  ᪠ ௥ */

main()
{
 char far *stkptr; /* ⥪ 㪠⥫   楯 */
 void far ship();    /* ⨯  楯窨 - ࠡ */
 void far sub();     /* ⨯  楯窨 -  */
 unsigned threadID; /*  楯窨 ID */
 int blank = 0x7020;

                 /* 砫 楯窨 - ࠡ */
 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(ship, &threadID, stkptr);
                 /* 砫 楯窨 - ࠡ */
 stkprt = (char far *)malloc(STACK_SIZE) + STACK_SIZE;
 DosCreateThread(sub, &threadID, stkptr);
 while( TRUE ) {
             /*  ࠭, ⠭  */
      VioScrollUp(0,0,-1,-1,-1, (char far *)&blank,0);
      VioSetCurPos(0,0,0); /* ⠭    */
                      /*    㣮                   */
                /* 樠 ᥬ ௥ */
      DosSemSet(&FireSem);
                /*  ⮯ */
      DosSemSetWait( &SunkSem, SEM_WAIT );
      VioWrtTTy(" !", 11, HANDLE );
      DosBeep( FREQ, DURATION ); /*   */
      DosSleep( (long)DURATION*3); /*  */
 }
}

/* ship() */
/* ᫥  ࠡ  ⮡ࠦ  */
/*  । ஬ 砩 ᥫ */

char far Xship[]="\xDC\xDC\xDB\xDC\xDC"; /* ࠡ*/
char far Xwave[]="\xF7\xF7\xF7\xF7\xF7"; /*   */

void far ship()
{
  int shippos = MIDDLE;  /*   ࠡ */
  int blank = 0x0720;    /* ଠ ਡ */
  unsigned rc;           /*   */

  while(TRUE) {    /* 砩  ࠡ */
    shippos = (rand() & 0x01) ? shippos + rand() % 4;
                                shippos - rand() % 4;
                 /*  ப, ᮢ ࠡ */
    VioWrtNCell( (char far *)&blank, 80, SHIPLINE, 0, 0);
    VioWrtCharStr(Xship, 5, SHIPLINE, shippos, 0);
    rc = DosSemWait( &FireSem, SEM_NO_WAIT );
    if( rc == 0) {    /* ᫨ ௥ 饭 */
      if(torpedopos==shippos+2) { /*    */
                       /* 맮  */
        DosSemClear( &SunkSem );
                       /* ᮢ  */
        VioWrtCharStr(Xwave, 5, SHIPLINE, shippos, 0);
        DosSleep( (long)DURATION*3 );
        shippos = MIDDLE; /* ࠡ 稭  業 */
      }     /* ᫨ ᥬ , ⠭  */
      DosSemSet( &FireSem );
  }
  DosSleep(200L);  /* "஧"  */
 }
}

/* sub() */
/* ᫥    ⮡ࠦ  */
/* , । ५  */
#define LEFT_ARROW 75    /*   */
#define RIGHT_ARROW 77
#define UP_ARROW 72
#define ESCAPE 27        /* [Esc] -  */
#define KBD_WAIT 0       /*    */
char Xsub[] = "\xDB";    /*   */

void far sub()
{
  int blank = 0x0720;         /* ଠ ਡ */
  int subpos = MIDDLE;      /*     */
  struct _KBDKEYINFO keydata; /* ।  bsesub.h */

  while(TRUE) { /*  ப, ᮢ  */
   VioWrtNSell((char far *)&blank, 80, SUBLINE, 0, 0);
   VioWrtCharStr((char far *)Xsub, 1, SUBLINE, subpos, 0);
                 /*  ⨥  */
   KbdCharIn( &keydata, KBD_WAIT, HANDLE );
   if( keydata.chScan == RIGHT_ARROW)  /*  ࠢ */
      subpos++;
   else if( keydata.chScan==LEFT_ARROW)  /*   */
      subpos--;
   else if( keydata.chScan==UP_ARROW)  { /* ५ */
      torpedopos = subpos;    /* ᪠           */
      DosSemClear( &FireSem); /*         ࠡ   */
   }
   else if( keydata.chChar == ESCAPE )  /* exit */
      exit(0);
  }
}

     ணࠬ ⠭  SunkSem - ᥬ,  뢠騩,
  ࠡ    ⮯,      ⪨  ࠡ.   ᫨   
室   - , ஬   ⮯ ࠡ,
⮣ ᯮ  㭪 DosSemSetWait()   樥  WAIT.   㣮
஭,   楯窠 ࠡ   FireSem  - ᥬ, 뢠騩,
 ௥  饭 -  室 ﭭ  ६ ࠡ. 
ᯮ DosSemWait()   樥  NO_WAIT. 窠   
  ᥬ,    짮⥫      ௥.   .6-7
뢠 ⢨ 楯祪  ᥬ஢.

    ⨬,   ⮩ ணࠬ    ࠧ묨 楯窠
 १  ᥬ (  ࠧ饭 ௥).     ᯥ稢
   ࠧ   ⥩   ணࠬ      ६   ॠ쭮
⢨.      ᯮᮡ   ୮,   쭮
ணࠬ஢.

 Ŀ
                                                   
      Ŀ        Fires         Ŀ          
       Sub> Ship          
              torpedo                 
                                                   
   DosSemClear(FireSem)      DosSemWait(FireSem)   
                                                   
                                                   
      Ŀ       Report        Ŀ          
       Ship> Main          
             sinking                 
                                                   
   DosSemClear(SunkSem)      DosSemWait(SunkSem)   
                                                   
 

         .6-7. 窨  ᥬ  sub.c


          

      ᬮ५  ᯮ짮  ᥬ஢    न樨 ⮫쪮
楯祪.        ᯮ짮      ᮣᮢ ᮢ.
 ࠧ    ᯮ짮   ६
 ⢥ ᥬ஢, ..       ࠢ
⭠   㣮   .       ⮬   砥   ᥬ   
ᮢ  न樨        ᭮     ᥬ
६, ⠭  ন ⥬.   ᥬ -
  ,        樮  ⥬,     ࠢ
⥬.       ᥬ  뢠  ᮮ⢥⢥   ⥬
ᥬ஬.

    ணࠬ 訢 ⥬  ᮧ ⥬ ᥬ  
 㭪樨 DosCreateSem().  ᮧ ᥬ  頥 ,
 ணࠬ  ᯮ짮     뫪  .
 -  ⮩   (⨯ unsigned  long), ᢠ  ⥬
 ᥬ.    室  䠩묨 ,  ᯮ㥬묨 
䨪樨 䠩.

     ᥬ ᮧ,  뤠  , 祭 宦    䠩.
 ।  \SEM,    䠩,  ⮨   祬
 8 ᨬ  3 - 㪢 ७.

    ⠫         ⥬ ᥬ,
ᯮ  㭪  DosOpenSem(),  뢠   ⠪    ,     
DosCreateSem(). DosOpenSem() 㤥  , ⮡ ᥬ 
 㯥  .

      ਬ    ᮢ,  ᯮ  ⥬ ᥬ 
᪫饣  㯠    ࠭.  䥪  室    ணࠬ
xdlsp.c  ࠧ " ".     ⮬ ਬ,  ᫨ ᥬ
 ᯮ,  뢮   ᮢ 㤥  ᬥ訢   ࠭.
ᮢ  室,  ⮡    㢥७,  ᮮ饭
         ⮣,  筥
ᮮ饭  㣮.  饭,  뢮    ⮬   㣮 ਬ -
 檠 ઠ.

     த⥫᪨ :

/* psysem.c */
/*  ࠭  chsysem.c, ᯮ ⥬ ᥬ */
#define INCL_DOS
#include <os2.h>
#define LENGTH 40
#define FLAGS  1
#define COMS   0L
#define ENVS   0L    /*  ६ 㦥 */
#define WAIT  -1L    /*  ६ 㦥 */
#define NO_EX  1     /*  ६ 㦥 */
#define KILLALL 0    /* 㭨⮦    ⮬ */
main()
{

  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬                   */
  char fbuf[LENGTH];              /*   쥪⭮ , */
                       /* 室   㤠筮 襭 */
  struct _RESULTCODES childID ;   /* 䨪 ᮧ */
                                  /*                    */

  unsigned rc;                    /*                 */

  if(rc = DosCreateSem(NO_EX,&SemHandle,"\\SEM\\DISP.EXE" ))

     { printf("DosCreatSem error = %u\n",rc); exit(1);}
                                  /*  ୥    */
  if(rc = DosExecPgm(fbuf,
            LENGTH,FLAGS,COMS,ENVS,&childID,"CHSYSEM.EXE"))

     { printf("DosExecPgm error = %u\n",rc); exit(1);}

                /* 뢭  ⥪    */

     while(!kbhit() )
    {
       DosSemRequest(SemHandle,WAIT);
       printf("He that would");
       printf("speak truth");
       printf("must have one foot");
       printf("in the stirrup.\n");
       DosSemClear(SemHandle);
    }

     /* ⨥ ᥬ */

  if(rc = DosCloseSem(SemHandle) )
     { printf("DosCloseSem    error = %u\n",rc); exit(1);}
  exit(0);
}


      ᮧ ᥬ   DosCreateSem()  ᪠
୨ . ⥬  ᯮ DosSemRequest()   DosSemClear()
  ᮢ⭮  㯠    㯯  ।  㭪樨   printf().
।      ,   짮⥫  
,     㪠뢠       ᥬ    㭪樥
DosCloseSem()   ࠡ ᠬ.

         DosCreateSem() - ᮧ ⥬ ᥬ.
Ŀ
DosCreateSem(Ownership,Ahandle,Name);                        
unsigned Ownership;     /* 0=ᮢ, 1= ᮢ */  
void far * far *Ahandle; /*    */              
char far *Name;          /*  ᥬ */                  

    DosCreateSem()  ᯮ  3  ࠬ.    -  䫠  
묨  ﬨ.  祭  0  뢠,    ᥬ 㤥
⥭ 㣮 ;  뢠 ணࠬ ᯥ稢  ࠢ
ᮡ⢥.  祭  1  뢠,    㣮     
      ᥬ;      筮 ﭨ.  ன
ࠬ  -  ,  㤠  㭪    䠩 , 
⨩ -   ᥬ.  ⥫  ᥬ  ᯮ짮  
, 稭饥   \SEM  ࠧ襭   䠩 .

    ⨬,      -  ᥬ஢  㡫,
.. ⭠  - 祢 ᨬ  . ⮡ ஢ 
 , ।  室 ⠢  .

         DosCloseSem() -  ⥬ ᥬ.
Ŀ
DosCloseSem(Handle);                           
void far *Handle;    /* ᥬ  */    

   㭪 DosCloseSem() ᮤন 1 㬥:  ᥬ.
  ਬ   ⮩ 㭪樨.

     ୨ , 믮塞 ணࠬ psysem.c:

/* chsysem.c */
/* ⠥ ⥪  ࠭                              */
#define INCL_DOS
#include <os2.h>
#define TIMES  40
#define WAIT  -1L    /*  ६ 㦥 */
main()
{

  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬                   */
  int count;

  unsigned rc;                    /*                 */

                   /* ⨥  ⥬ ᥬ஢ */
  if(rc = DosOpenSem(&SemHandle,"\\SEM\\DISP.EXE" ))

     { printf("DosOpenSem error = %u\n",rc); exit(1);}

   for(count =0;count<TIMES;count++);
 {
      if(rc = DosSemRequest(SemHandle,WAIT))

       { printf("DosSemRequest error = %u\n",rc); exit(1);}

       printf("%d",count);
       printf("HE THAT WOULD");
       printf("SPEAK TRUTH");
       printf("MUST HAVE ONE FOOT");
       printf("IN THE STIRRUP\n");

   if(rc = DosSemClear(SemHandle) )
      { printf("DosSemClear    error = %u\n",rc); exit(1);}

 }
  exit(0);
}


    ୨    ⠭  ⠪    ᥬ஬  
DosOpenSem().  ,  ⥬ ᯮ 横 for  뢮 
।  printf()  ।  ᫮  ࠧ.     த⥫᪮
,  DosSemRequest()    DosSemWait()  ᯥ稢   ᮢ
    ࠭       ᥬ୮  ,  祭   
DosOpenSem().  横 稢,  蠥.

         DosOpenSem() - 뢠 騥 ⥬
                        ᥬ.
Ŀ
DosOpenSem(Ahandle,Name);                         
void far * far *Ahandle; /*    */   
char far *Name;          /*  ᥬ */       

    㭪 DosOpenSem()  ᯮ   ᥬ   ⢥ ண
㬥.         ,      DosCreateSem(). 
 ,   ࠧਢ    ⮬  ᥬ, ..
 ᯮ짮     ᮧ   ⨨ .  
㬥 㭪樨 - , 㤠 㤥 饭 ᥬ , 
頥 㣨 ᮬ  ⮣ ᥬ.

㭪 DosOpenSem()  ᯮ짮   ᥬ஢.
⪠      ᥬ,            ᮧ    
DosCreateSem()  訡.

      ᮧ  ᥬ        ,     
,  ᮧ訥,  뫨 ,  訫.

         ⨥ ⥬ ᥬ.

          㭪樥  DosCloseSem(), ᫨   
饭 (..  ). ⪠   ⠭  ᥬ  
 102 - ERROR_SEM_IS_SET.   :  㪠 
⠭ ᥬ,  ॡ  騩 , ᫨   
. 㣨  , 騥  ᥬ,    筮,
 騩   .

          ⨯ .

     , ᯮ㥬  ⥬ ᥬ஢, ।  
 ⮢᪨ 䠩, 㦨 ᫮ ᭥.

    ᪠  ᨬ  ⨯    몠      ⮬,  
ᥬ -  ६, 騥 4   .   ,   祬
ࠡ⠥ ணࠬ  ᥬ.

      砥  ᥬ஢      塠 ६ 
  ࠡ祬 ࠭⢥ .  API - 㭪樨, ⠪,
 DosSemRequest(), ࠡ  ᠬ  ᥬ ६.

    ⥬   ᥬ      ⫨.      맢  㭪
DosCreateSem()    DosOpenSem(),  ⥬  頥 塠⭮
᫮  -    ᥬ.      ,  ⮫쪮  ᥬ.
DosSemRequest()  㣨 API - 㭪樨 ࠡ   ᮬ,   אַ
 ⨬ ஬.

    ..  API -  㭪樨  ࠡ       - , ⠪   ⥬묨
ᥬࠬ,    ᥬ  㬥  -     塠⭮
६,   ᠬ ६.  ⨯    
뫮  ᯮ짮  ⮩ 樨  API - 㭪?

      襭  -  ᤥ  ⨯  㬥 㭪樨 unsigned long, 
  -  ⥬  ᥬ஢ ⮦ unsigned long.   ⮩ 奬
 室  ਢ  ⨯  ⥬ ᥬ஢,   ⨯
 - ᥬ஢  ८ࠧ뢠  unsigned long far*   unsigned
long.   襭 ᯮ짮   ࢮ  ॠ樨 ⮢᪮
,  ⠪  IBM -  .

    奬 ᯮ  ᥩ    ⮢᪨  䠩 
 ਢ  ⨯ 㬥.   ⨯   API -  㭪権
।  ᥬ  㬥    void  far*.     -  ᥬ
।    unsigned  long,      ⮦⢫  ⨬
⨯. ⥬  ᥬ   饭   ६ ⨯  void
far*,  ⠪ ⮦⢫   ⨬ ⨯,     㪠뢠
  .

         "०६ ᬥ"  ᥬ.

     ந室,  ᫨ ,  ᯮ騩 ⥬  ᥬ, 
।   ६ "㬥",  ᨢ ⠭  ᥬ?  
㣨 , 騥  , 饭 ᥬ஬,  筮 
 ᢮ ᥬ.  㤥   ⥬,   ᫨
  , ⥬   ࠫ.

      ணࠬ  psysem.c    믮塞  室  ⢨   
砭: ᥬ    横 頥, । ⥬,   ணࠬ
஥        ࠡ.      ୥ ணࠬ
chsysem.c த ࠡ ࠢ쭮,  ᫨ த⥫᪠ 
ࢮ.      ᯥਬ஢    ⨬,   
⮣,   ⠥   ப. ⥫᪠ 
,      㤥  த.      ਬ 뢮
짮⥫   ᫥ 26 ப.  ⮬   த⥫᪠
楯窠 稢,  ⠢   த ᢮  뢮
⠢訥 ப.

Ŀ
 - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
 ,  ⨭, ঠ   ६.   
 ,  ⨭, ঠ   ६.   
 25 ,  ,     
 ,  ⨭, ঠ   ६.   
 ,  ⨭, ঠ   ६.   
 26 ,  ,     
 ,  ⨭, ঠ   ६.   
 27 ,  ,     
 28 ,  ,     
 29 ,  ,     
 30 ,  ,     
 - - - - - - - - - - - - - - - - - - - - - - - - - - - -  


      ந, ᫨ ணࠬ ⢥⢥   ᥬ
 ⠭ ﭨ?  ᫨  㭨⮦ 㣨  ᮬ,
 ந ⠫쭠 ⥬ 訡? ᫨  , 
 .    .

    ᫨  ⠭  ᥬ "㬥", ᫥騩  ,
訢騩 ᥬ,     105  ERROR_SEM_OWNER_DIED.
    ࠥ  ᥡ  ᮡ⢥    ᥬ,  ᮫
ࠥ  䫠    㯠襣  ⮢.    ,  㣨
, ᯮ騥 ,  ᯮ짮 ᥬ.

        ணࠬ  psysem.c   chsysem.c, 
ࠡ. ⥫᪨   ஢ ᥬ   室, 
짮⥫  :

/* psysem2.c */
/*  ࠭  chsysem.c, ᯮ ⥬ ᥬ */
#define INCL_DOS
#include <os2.h>
#define LENGTH 40
#define FLAGS  1
#define COMS   0L
#define ENVS   0L    /*  ६ 㦥 */
#define WAIT  -1L    /*  ६ 㦥 */
#define NO_EX  1     /*  ६ 㦥 */
#define KILLALL 0    /* 㭨⮦    ⮬ */
main()
{

  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬                   */
  char fbuf[LENGTH];              /*   쥪⭮ , */
                       /* 室   㤠筮 襭 */
  struct _RESULTCODES childID ;   /* 䨪 ᮧ */
                                  /*                    */

  unsigned rc;                    /*                 */

  if(rc = DosCreateSem(NO_EX,&SemHandle,"\\SEM\\DISP.EXE" ))

     { printf("DosCreatSem error = %u\n",rc); exit(1);}
                                  /*  ୥    */
  if(rc = DosExecPgm(fbuf,
            LENGTH,FLAGS,COMS,ENVS,&childID,"CHSYSEM2.EXE"))

     { printf("DosExecPgm error = %u\n",rc); exit(1);}

                /* 뢭  ⥪ */

     while(TRU )
    {
       DosSemRequest(SemHandle,WAIT);
       printf("He that would");
       printf("speak truth");

     if (kbhit())  /*                     */
         exit(0);  /*                     */
       printf("must have one foot");
       printf("in the stirrup.\n");
       DosSemClear(SemHandle);
    }

}

室 ந室  横,   த⥫  ᥬ஬   뢮
 ࠭.

  ୨ , ᯮᮡ  ࠡ 
०६ 㬥襬 த⥫:

/* chsysem2.c */
/* ⠥ ⥪  ࠭, ⠭ ᥬ           */
#define INCL_DOS
#include <os2.h>
#define TIMES  40
#define WAIT  -1L    /*  ६ 㦥 */
main()
{
  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬                   */
  int count;
  unsigned rc;                    /*                 */
                   /* ⨥  ⥬ ᥬ஢ */
  if(rc = DosOpenSem(&SemHandle,"\\SEM\\DISP.EXE" ))
     { printf("DosOpenSem error = %u\n",rc); exit(1);}
   for(count =0;count<TIMES;count++);
 {
      /* ⪠  ᥬ */
      rc = DosSemRequest(SemHandle,WAIT))
      if (rc==105)
         printf("\n*** Owner has died***\n");
      else if (rc !=0)
         printf("DosSemRequest error = %u\n",rc); exit(1);
       printf("%d",count);
       printf("HE THAT WOULD");
       printf("SPEAK TRUTH");
       printf("MUST HAVE ONE FOOT");
       printf("IN THE STIRRUP\n");
   if(rc = DosSemClear(SemHandle) )
      { printf("DosSemClear    error = %u\n",rc); exit(1);}
 }
  exit(0);
}

    ᫨ ୨  㦨 訡 , 頥
DosSemRequest(),  ஢,    105. ᫨ ,   
"ࠢ"  ᥬ,  ࠡ த.

     ਬ 뢮,  짮⥫ 訫 ""  ணࠬ
 । 뢮:

Ŀ
 - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
 ,  ⨭, ঠ   ६.   
  7 ,  ,     
  8 ,  ,     
 ,  ⨭, ঠ   ६.   
 ,  ⨭                                   
 * * *  㬥 * * *                                
  9 ,  ,     
 10 ,  ,     
 11 ,  ,     
 - - - - - - - - - - - - - - - - - - - - - - - - - - - -  


       ⠥ ᮮ饭,  뢠饥,   ।騩
"㬥", ⥬  ࠡ⠥   ᥬ஬   뢠 ப.  ( 
,   짮⥫ ᠬ   ᮡ ᥬ).

         ᨢ ᯮ짮 ᥬ஢.

    ⥬  ᥬ,  ᫨    ᮧ    樥   ᮢ⭮
⠭,   ⠭ ४ᨢ.     ,   
祣 㦭?

    ।⠢ ,  ன 楯窠  ணࠬ ⠥ ⥪,
砥  짮⥫,   ࠭.  㭪 ᯮ  ᥬ
 ࠢ  㯮   ࠭,   㣮    蠥 .
㣠  㭪  ⮩  楯窨  ⠥  । ࠧ  ࠭,
ਬ,   ⢥   ⨥ 짮⥫  । . 
㭪  ⠪  ᯮ  ᥬ.    ।,    1 - 
㭪 室   ।  뢮    室  맢 
㭪  ⠢ ࠧ  ⥪.

    1 -   㭪 ⠭ ᥬ,   ᢮ ᮡ⢥  
࠭.    ᢮,  ..   祭 뢮   ࠭.
   맮   㭪,    ᥬ  ,   
 㯠.   ?

    ᫨ ।⠢,     㭪   楯窥 ⠪  ,  
ࢠ,  㤥 ࠧ襭   ᥬ (  饬 ).  
筮  ணࠬ - ,     ࠢ     ⮩
 楯窥   ன ࠧ, ..  楯窠  㤥 
 ᢮ ᮡ⢥ ⢨.

     ᫨  㭪樨 ᯥ譮 "" ᮡ⢥  ᥬ,  
⥬ 㧭,  ᢮ ? ᫨ ⮣,  2 -  㭪
믮 DosSemClear(), 㣮    "⮫"   1 -  
㭪樥,   訫.    襭 ⮩ ஡  ᥬ
࣠ 稪.   ᯥ譮  稪 㢥稢,
  ⪥ - 㬥蠥.  砥 2 -  㭪権 稪 㤥
㢥祭  0  1 ࢮ 㭪樥,   1  2 - ன 㭪樥. 
2 -  㭪 ᢮ ᥬ, 稪 㬥  2  1,   1
-  -  1  0,  㣮   ᯮ짮 .

         㣨 ᯮ짮 ⥬ ᥬ஢.

      ਬ  ᥬ஢     ஢
 ࠡ  ᠬ  ⥬ ᥬ.     ᮧ
⥬ ᥬ   DosCreateSem(), 㣮    
   DosOpenSem().    㣨 ᥬ 㭪樨  
ᯮ짮,   砥  楯窠.  ⭮  DosSemSet(),
DosSemClear(), DosSemWait(), DosSemSetWait()  DosMuxSemWait().

         ६ 㭪樨  ⥬ ᥬ.

      ணࠬ  室   ,    襫   ।
ࢠ ६. ਬ, ணࠬ   ᮡ⨥, ஥   
 (    ⥫㭨樮 ணࠬ).    
,  ⨬  த 믮 (⥭  ᨬ ,
ਬ). ⥬  ⨬ ᢮ ணࠬ,   ६, 
᪠ ᥡ: "   ᮡ⨥, ஥  ய,   
祬 -  ". ਬ, ᮮ 짮⥫,   .

     ணࠬ 室   -  १ ।
ࢠ. ਬ,  ⥬   拉୮ ॠ
 10 ᥪ㭤.

      OS/2    ᪮쪮  㭪権,    ᨭ஭
প    ࢠ  ६.  ("ᨭ஭"  砥  ⠩
ࠡ ᨬ   ணࠬ).   㭪樨  ᯮ ⥬
ᥬ. 㭪 প  ᥬ ᫥ । ६,
 ६ 㭪   १ । ࢠ.
    騩 ਬ 뢠 㭪 প DosTimerAsync().

砫 ணࠬ ⠥  ᥫ.  প (1000 ᥪ㭤
- 1  ᥪ㭤) ணࠬ  ⠭.      ॠ쭮 樨
 뢮 ᥫ  㤥   ࠡ.

/* delay.c */
/*   ६  ⠭                          */
/* ᯮ ᨭ஭ প                              */
#define INCL_DOS
#include <os2.h>
#define NO_EX       1
#define FREQ     1000
#define DURATION  300
#define MSECS    1000L
#define ERR_SEM_TIMEOUT  121 /*  , ᫨ ᥬ */
#define NO_WAIT  0L                 /*  ६ 㦥 */
main()
{

  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬  DosCreateSem() */

  unsigned short TimerHandle; /*  DosTimerAsync()              */
  char far *SemName = "\\SEM\\DELAY\\.SEM"

  int count;                      /* 稪 横             */

  unsigned rc;                    /*                 */

                     /* ᮧ ᥬ஢                      */
  if(rc = DosCreateSem(NO_EX,&SemHandle,"SemName" ))

     { printf("DosCreatSem error = %u\n",rc); exit(1);}

   DosSemset(SemHandle); /* ⠭ ᥬ */
                         /*  ⠩      */


  if(rc = DosTimerAsync(MSECS,SemHandle,&TimerHandle) )

     { printf("DosTimerAsync error = %u\n",rc); exit(1);}

  do{                                    /* ᥬ*/

     printf("%d",count++);               /* busy work       */
     rc = DosSemWait(SemHandle,NO_WAIT); /* ᥬ ? */
    }
     while( rc ==ERR_SEM_TIMEOUT);       /*           */

   if (rc !=0)                           /* ஫ 訡 */

     { printf("DosSemWait error = %u\n",rc); exit(1);}
                                         /* 祭*/
       printf("\nThe delay has time out.\n");
     DosBeep(FREQ,DURATION);
}

뢮 ணࠬ ᫥騩: ( 祪 -  ஬ ᥫ):

Ŀ
 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 221 222 223 224 225 226 227 228 229 230 231 232 233 234 
 235 236 237                                             
 প                                                


      ⢨⥫쭮  ᫮  ⠥  ᥫ ࠧ筮,  
⮣,  ᪮쪮  ६    ஢騪  ணࠬ । প.
ࢠ  ६ ᥣ .

     㦤  ᥬ  裡  ணࠬ.  ࠧ,
ணࠬ  ᮧ ⥬ ᥬ  ⠭ . , 
ᯮ    ᮢ⭠      ᮧ ᥬ. ணࠬ
⮣ ।  ᥬ ,  祭   DosCreateSem() १
DosTimerAsync()

         DosTimerAsync() - 稭 ᥬ প.
Ŀ
DosTimerAsync(Interval, SemHandle, ATimerHandle);          
unsigned long Interval;  /* ६ প  ᥪ. */  
void far *Semhandle;     /* ᥬ  */            
unsigned chort far *ATimerHandle; /*   ⠩୮  
                                          */         

    DosTimerAsync()  ᯮ  3  㬥.  1  -    -  প 
ᥪ㭤.  ᪠,   砥 㭪,   ᫨
ᥬ ⠭ . ன 㬥 - ᥬ . 3 -   -
 ᫮, ᮤঠ饣 ⠩  (᫮, 頥 ⥬
  䨪樨   ⠩).       ᯮ짮    㭪樥
DosTimerStart() (ᬮਬ  ᫥饬 ਬ).

      饭  ⠩,  ணࠬ      ⪨ ᥬ
㭪樥  DosSemWait(),    ⪨  㣮  ᥬ.   ணࠬ
delay.c  ᯮ짮  NO_WAIT  DosSemWait(), ⮡  ணࠬ
 த  ᫠.

         ࢠ ⠩.

    㭪,  室    DosTimerAsync(),  ᯮ  
⮪ ᥬ १ ।  ஬⪨ ६.    㭪
DosTimerStart().    ᫥饬  ਬ    㭪 ᯮ 
ᯥ祭 㪠    ᥪ㭤,  ணࠬ  ⠥
᫠     ࠭.     짮⥫     ,   ⠩
४砥  㣮  㭪樥,   DosTimerStop().  ᫠   த
,    ४.   ᯮ Ctrl-C   室  
ணࠬ.

/* timer.c */
/*  ᨣ  ᥪ㭤                                 */
/* ᯮ ᨭ஭ ६ ࢠ                    */
#define INCL_DOS
#include <os2.h>
#define NO_EX       1        /*   쭮         */
#define FREQ     1000
#define DURATION  300
#define MSECS    1000L
#define ERR_SEM_TIMEOUT  121 /*  , ᫨ ᥬ */
#define NO_WAIT  0L                 /*  ६ 㦥 */
main()
{

  void far *SemHandle; /* 㪠⥫  ࠬ ࠢ      */
                       /* ⥬ ᥬ஬  DosCreateSem() */

  unsigned short TimerHandle; /*  DosTimerAsync()              */
  char far *SemName = "\\SEM\\DELAY\\.SEM"

  int count;                      /* 稪 横             */

  unsigned rc;                    /*                 */

                     /* ᮧ ᥬ஢                      */
  if(rc = DosCreateSem(NO_EX,&SemHandle,"SemName" ))

     { printf("DosCreatSem error = %u\n",rc); exit(1);}

   DosSemset(SemHandle); /* ⠭ ᥬ */
                         /*  ⠩      */


  if(rc = DosTimerStart(MSECS,SemHandle,&TimerHandle) )

     { printf("DosTimerStart error = %u\n",rc); exit(1);}

    while (TRUE)
  {
     printf("%d",count++);               /* busy work       */
     rc = DosSemWait(SemHandle,NO_WAIT); /* ᥬ ? */
    if (rc==0) {

     DosBeep(FREQ,DURATION);
     DosSemSet(SemHandle);        /* ⠭ ᥬ */
               }
      else
          if (rc != ERR_SEM_TIMEOUT)
             { printf("DosSemWait error = %u\n",rc); exit(1);}

     if (kbhit())
      {
         DosTimerStop(TimerHandle); /* ⠭ ⠩    */
         getch();
      }
  }
}
      ᮧ  ⠭, ⠩ 饭, ணࠬ  室
 横.   横  ⠥  ᫠  믮  DosSemWait(). ᫨ 
१    ⮩  㭪樨 뢠,   ᥬ   饭,
ணࠬ    ⠭ ᥬ. ⥬  
१ ᥪ㭤.

         DosTimerStart() - ᪠ ࢠ ⠩.
Ŀ
DosTimerStart(Interval, SemHandle, ATimerHandle);           
unsigned long Interval;  /* ६ প  ᥪ. */   
void far *Semhandle;     /* ᥬ  */             
unsigned chort far *ATimerHandle; /*   ⠩୮   

    DosTimerStart() ᯮ   㬥,   DosTimerAsync().
DosTimerStop()  ⠭  ⠩.        ᥬ, 
⠥  ० ﭨ.

         DosTimerStop() - ⠭ প 
                          ࢠ ⠩.
Ŀ
DosTimerStop(TimerHandle);                                   
unsigned short TimerHandle; /* ⠩  */            

     1  㬥: ⠩  , 頥  DosTimerAsync()
 DosTimerStart(), ⨢襩 ⠩.

          

                ᨭ஭樨  ᮢ   
㯮 ஡ 㯨 樨.   ,    
     ॡ     ⠪,          
᢮     㣨  ᮬ.     ,     
த,  믮 ⠭.

    ᬮਬ 2 , 1  2,    ॡ 2 ,
    .  ।,    1    ᯮ짮       
⠭ ᥬ  DosSemRequest(). ⥬ 訢  ,
     뢠 ᮬ 2,  ॡ  .
    .6-8. ५ 뢠  㯨 :
         ,      㣨.    믮
⠭                   ஭
⥫⢠.



    1 ন      Ŀ         1 ॡ
         > 1 Ŀ  
                                
                                     
                                     V
              Ŀ                  Ŀ
                                    
                                
                ^                     
                        Ŀ        
                Ĵ 2 <
     1 ॡ             1 ন
                                  


         .6-8. 㯨 

     ਬ ணࠬ, 뢠騩 㯨    ⢨.
 楯窨   뢥     ࠭.    - 
, 饭 ᥬ஬ BuffSem. ࠭ 㣮 ,  饭
ᥬ஬ DispSem.

     楯窠   묨  ᪠ 楯窨 1  2,
    ᮤন  ࠭,   ⮫쪮 ᫥  ⮣, 
㤥  ࣠  (      ᥬ஢) ᮢ  
  ࠭.

/* deadlock.c */
/*  ﭨ deadlock */
/*  楯窨 ॡ 㯠   ᠬ */
#define INCL_DOS
#include <os2.h>
#include <malloc.h>
#include <string.h>
#define STACK_SIZE 1024          /*  ⥪   楯窨    */

#define WAIT -1L                 /* -1 -  0 - ,     */
                                 /* >0 =  ᥪ㭤 */

struct  {

     char name[30];
     int birth;
     char accomp[40];
        } math;

  unsigned long DispSem = 0; /* ᥬ ࠭ */
  unsigned long BuffSem = 0; /* ᥬ  */

main()
{

  char far *stkptr;
  void far thread1();
  void far thread2();
  unsigned threadID;   /*   䨪 楯窨 */



  strcpy(math.name,"Pierre de Fermat");
  math.birth = 1601;
  strcpy(math.accomp," Fermat's last theorem");
                       /*   楯祪 */

  stkptr = (char far *)malloc(STACK_SIZE)+STACK_SIZE;
  DosCreateThread(thread1,&threadID,stkptr);

  stkptr = (char far *)malloc(STACK_SIZE)+STACK_SIZE;
  DosCreateThread(thread2,&threadID,stkptr);

  DosExit(0,0); /*   楯窠 */

}

/* 楯窠 1 */
/* ⠥  ᢥ稢  */
void far thread1();

 {
  int j;
  while(TRUE) {
       DosSemRequest(&DispSem),Wait);
       for(j=0;j<100;j++);
       j = j+ (j-j);
       DosSemRequest(&BuffSem,Wait);
        printf("\nThread 1 displaying data\n");
        printf("Mathematician:   %s\n",math.name);
        printf("Data of birth:   %4d\n",math.birth);
        printf("Accomplishment   %s\n",math.accomp);
       DosSemClear(&DispSem);
       Dos SemClear(&BuffSem);
   }
}

/* 楯窠 2 */
/* ⠥  ᢥ稢  */
void far thread2();

 {
  while(TRUE) {
       DosSemRequest(&DispSem),Wait);
       DosSemRequest(&BuffSem,Wait);
        printf("\nThread 1 displaying data\n");
        printf("Mathematician:   %s\n",math.name);
        printf("Data of birth:   %4d\n",math.birth);
        printf("Accomplishment   %s\n",math.accomp);
       DosSemClear(&DispSem);
       Dos SemClear(&BuffSem);
   }
}


    ⮡  㡥,    㯨    砥 , 
⠢ 横  প   楯 1,   㭪ﬨ DosSemRequest()
 ᯫ   . ᫨    ᯮ짮 DosSleep(),  㯨
   ,       ।. 
  ਬ୮ १  ᫥ 砫 ணࠬ.  䥪
ਢ  ⥫⢮ - ணࠬ 뢮    䠩
 ஡,  ⥬   "ᠥ". (     横
প, ⮡    ᠬ  襬 ).

         ஡ " 䨫䮢".

    㯨  - ꥪ ᫥  ୮ 㪥.
   ஡, 뢠 " 䨫䮢",
 .

     䨫䮢 (।⠢ ) ᨤ  㣫 ⮫ 
 ᯠ.   ६ 㬠  (᢮ 䨫᪮ 㤥)
   ६ .  ⮡   ᯠ,   㦭   
() ६.     ⮫쪮  5 ,    
 ५,    .6-9.

            ५      
                                 ५
     
                                  
      ५

                                  ५
          
                          
               ५


        .6-9. " 䨫䮢".


     䨫 ६  ६ ⠥     .
᫨ 㤠, ⠥      ࠢ. ᫨   ,    
,          ⭮     筥 㬠.  ⥬
ࠡ⠥,      䨫䮢       ந쭮
६.   ।⠢,    䨫        
,    ⠭  㯭  ࠢ  .     㯨
     .

    㤭  ⥣, । 㯨   
.   ᤥ    ணࠬ   ᪮쪨 騬
ᠬ.   ਬ,      ணࠬ   deadlock.c     ⨣
ᯮ짮  2  -    楯祪,  訢   ।
浪,  ᮣ᭮  浪      ᯨ᪥.       ணࠬ
⠭  ᫮,    ⢮  ᮢ   ᮢ, 㯨
 ⠭ 쥧 ஡.


         

     ⮩   㧭,  ᥬ ᯮ  ᮣᮢ
ࠡ  ࠧ  ᮢ     楯祪.      ᭮   
ᥬ஢:  ᥬ      ⥬.    ࠪ⨪ ᢥ 
᫥ ⠡.

          .

ᯮ:  楯窠.
 : 譨 ६ ⨯ unsigned long.
:  ६.
樠: ⠭  0 ("᢮").
: 㣨 楯窠, 騬  譨 ६.
:  楯窮, 饩  ६.
:  砥 ⢮ 䠩, ᮤঠ饣
            ६.

         ⥬ ᥬ.

ᯮ:  ᠬ.
 : ६, ࠢ塞 ⥬.
: DosCreateSem().
樠: ⠭  0 ("᢮")  ᮧ,
: 㣨 ᠬ, ᯮ騬 DosOpenSem().
: ᮬ,   ᮧ, 㣨 ᠬ,
᫨ ⠭ 䫠 NoExclusive.
:   , 訥  ᮧ訥 ,
            뤠 DosCloseSem()  訫.

     ⠡ 頥 ᥬ API - 㭪樨:  ᯨ᮪
㭪権, ⢨  ᥬ  ⢨  楯.


    㭪          -  ᥬ      -  楯
Ŀ
 DosSemClear()                                    
 DosSemSet()        .""                         
 DosSemRequest()                                        
  ᫨ 饭       .""      뢠饩 
  ᫨           ---           ⪨         
  ᫨      .""      뢠饩 
                                                        
 DosSemWait()                                           
  ᫨ 饭         ---            뢠饩 
  ᫨           ---           ⪨         
  ᫨        ---            뢠饩 
                                                        
 DosSemSetWait()                                        
  ᫨ 饭       .""     ⪨         
  ᫨           ---           ⪨         
  ᫨        ---            뢠饩 
                                                        
 DosMuxSetWait()                                        
  ᫨  ᥬ.                                       
           饭     ---            뢠饩 
  ᫨  ᥬ.                                         
         饭.     ---           ⪨         

   ( ⪠ ࠧ 楯).

    ࠧ  "    ⪨"   ।    ⢨⥫쭮   3
:  ⪨  । ६,   䨪஢
६    .  ᫥     
 121_ERROR_SEM_TIMEOUT, ᫨ ᥬ ⠭. ⨬,  
"⠭"  "" ᨭ, ⠪   "饭"  "".

     ⮩     㧭,   ᮢ    饬 
      ⢫         㭪権  DosSemRequest()  
DosSemClear().       ।     楯窠   
ᠬ   DosSemSet()  DosSemClear()  אַ ࠢ
ᥬ஬, 騬 ⪨    DosSemWait(),  DosSemSetWait() 
DosMuxSemWait() ᮮ⢥⢥.

     ⠪ 㧭 ⠩ 㭪樨 DosTimerAsync(), DosTimerStart()
 DosTimerStop(),  ᯥ稢騥 ६  প   ࢠ 
ᥬ஢.

             㧭,         㯨    ,
⠭ ⥬, ᫨ ᪮쪮 ᮢ ਭ﫨 ࠢ쭮
襭  ६ 㯠  ᠬ.