⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sher1.pll

📁 一个嵌入式系统的C代码
💻 PLL
字号:
PROGRAM Sher1 (INPUT, OUTPUT);

{-----------------------------------------------------------}
{ PURPOSE: IPC demonstration using shared memory allocation }
{          with counting semaphore synchronisation.         }
{                                                           }
{ AUTHOR:  Ron Chernich                                     }
{ DATE:    18-APR-94                                        }
{-----------------------------------------------------------}

VAR
  s1 : INTEGER;


{------------------------------------------------------
{ "Client" routine opens semaphore and shared memory,
{ requests a value from the user, writes the value
{ to the shared memory, then signals the "Server"
{ (via the semaphore) that it can continue and finally
{ cleans up and exits.
{
{ To assist synchronisation, "Client" tries three times
{ to open the shared memory (delaying between each try)
{ before reporting an error.
}
PROCEDURE CLIENT;
VAR
  m1, v1, dummy, retry : INTEGER;
BEGIN
  WriteLn('Initialising shared memory');
  retry := 3;
  REPEAT
    m1 := SYSTEM(SHR_OPEN, 'toklas');
    IF m1 <= 0 THEN
      FOR dummy := 1 TO 100 DO
        v1 := 0;
    retry := retry - 1
  UNTIL (m1 > 0) OR (retry = 0);
  IF m1 <= 0 THEN
    WriteLn('Unable to open shared memory')
  ELSE BEGIN
    Write('Enter value: ');
    Read(v1#);
    v1 := SYSTEM(SHR_WRITE, m1, 0, v1);
    WriteLn;
    WriteLn('Old value was: ', v1#);
    dummy := SYSTEM(SHR_CLOSE, m1)
  END;
  dummy := SYSTEM(SEM_SIGNAL, s1);
  dummy := SYSTEM(SEM_CLOSE, s1)
END;

{-----------------------------------------------------
{ "Server" routine creates a shared memory block of
{ length 1 and a binary semaphore with a zero count,
{ then waits on the semaphore (which will be set by
{ the "Client" when the memory has been initialised).
{ When woken up, "Server" displays the value in
{ shared memory, closes memory and semaphore, then
{ terminates.
}
PROCEDURE SERVER;
VAR
  s0, m0, v0, humpty : INTEGER;
BEGIN
  v0 := -5;
  s0 := SYSTEM(SEM_CREATE, 'aliceb', 0);
  WriteLn('Semaphore created');
  m0 := SYSTEM(SHR_CREATE, 'toklas', 1);
  WriteLn('Setting Shared mem to -5');
  v0 := SYSTEM(SHR_WRITE, m1, 0, v0);
  WriteLn('Shared Mem created');
  Write('Waiting.. ');
  humpty := SYSTEM(SEM_WAIT, s0);
  WriteLn;
  WriteLn;
  v0 := SYSTEM(SHR_READ, m0, 0);
  WriteLn('Read value: ', v0#);
  humpty := SYSTEM(SEM_CLOSE, s0);
  humpty := SYSTEM(SHR_CLOSE, m0)
END;


         {----------------------------------------------}
         {                                              }
         { MAIN ENTRY decides who this instance will be }
         { by trying to open the named semaphore.       }
         {                                              }
         {----------------------------------------------}

BEGIN
  s1 := SYSTEM(SEM_OPEN, 'aliceb');
  IF (s1 > 0) THEN
    CLIENT
  ELSE
    SERVER;
  WriteLn('Done.')
END.

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

⌨️ 快捷键说明

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