shmarr1.pll

来自「一个嵌入式系统的C代码」· PLL 代码 · 共 56 行

PLL
56
字号
PROGRAM Shm1 (INPUT, OUTPUT);

{-----------------------------------------------------------}
{ PURPOSE: Shared memory example using shm segment array    }
{          Binary semaphore used to syncrhonize access      }
{                                                           }
{ AUTHOR:  David Jones                                      }
{ DATE:    18-APR-94                                        }
{-----------------------------------------------------------}

VAR
  count, semA, semB, shm, value, result, temp : INTEGER;

BEGIN
  { create two semaphores and set both to 0 }
  { semA - is used to block Shm1 waiting for Shm2 }
  { semB - is used to block Shm2 waiting for Shm1 }
  semA := SYSTEM(SEM_CREATE, 'sem1', 0);
  semB := SYSTEM(SEM_CREATE, 'sem2', 0);

  shm := SYSTEM(SHR_CREATE, 'myshm', 10);

  IF (semA > 0) AND (shm > 0) THEN
  BEGIN
    
    Write('Enter a number ==> ' );
    FOR count := 1 TO 10 DO
    BEGIN
      Write( count#, ', ' );
      temp := SYSTEM(SHR_WRITE, shm, count-1, count)
    END;
    Writeln;  Writeln;

    { Signal other process that it now has a value to work with }
    temp := SYSTEM( SEM_SIGNAL, semB );    
    
    { Wait for other process to read value }
    { from shared memory and square it }
    Write('Waiting.. ');
    temp := SYSTEM(SEM_WAIT, semA);
    WriteLn;  WriteLn;

    FOR count := 1 TO 10 DO
    BEGIN
      result := SYSTEM(SHR_READ, shm, count-1);
      WriteLn( count#, ' squared is ', result# )
    END;    
    
    temp := SYSTEM(SEM_CLOSE, semA);
    temp := SYSTEM(SEM_CLOSE, semB);
    temp := SYSTEM(SHR_CLOSE, shm)
  END;
END.

{------------------------------ EOF -----------------------------}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?