Buchempfehlung
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
"Der" Petzold, das über 1000 Seiten starke Standardwerk zum Win32-API - besonders nützlich u. a. bei der GUI-Programmierung in FreeBASIC! [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Tutorial

[Linux] Kommunikation zwischen Programmen mit Shared Memory

von MitgliedstephanbrunkerSeite 3 von 3

Das PHP-Programm

Da dies hier ein Freebasic-Portal ist, gibt es die PHP-Hälfte des Programms nur zur Info. Im Grunde ist es egal, wer mit wem kommuniziert, der Zugriff ist dann nur sprachabhängig. Wie am Anfang erwähnt, war in dem verklinkten Tutorial ein Fehler, nämlich nur einen statt drei reservierte Semaphore.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Test Page</title>
</head>

<body>

<?php

//--------------------------------
//--------------------------------
//----- ACCESS SHARED MEMORY -----
//--------------------------------
//--------------------------------

//----- SHARED MEMORY CONFIGURATION -----
$SEMAPHORE_KEY = 291623558;             //Semaphore unique key
$SHARED_MEMORY_KEY = 672213396;     //Shared memory unique key

//Create the semaphore
$semaphore_id = sem_get($SEMAPHORE_KEY);        //Creates, or gets if already present, a semaphore
if ($semaphore_id == false)
{
    echo "Failed to create semaphore.  Errormessage: ",$php_errormsg,"<br />";
    exit;
}
else
{
    echo "Got Semaphore ID :",$semaphore_id,"<br /";
}

//Acquire the semaphore
if (!sem_acquire($semaphore_id))                        //If not available this will stall until the semaphore is released by the other process
{
    echo "Failed to acquire semaphore $semaphore_id<br />";
    sem_remove($semaphore_id);                      //Use even if we didn't create the semaphore as something has gone wrong and its usually debugging so lets no lock up this semaphore key
    exit;
}

//We have exclusive access to the shared memory (the other process is unable to aquire the semaphore until we release it)

//Setup access to the shared memory
$shared_memory_id = shmop_open($SHARED_MEMORY_KEY, "w", 0, 0);  //Shared memory key, flags, permissions, size (permissions & size are 0 to open an existing memory segment)
                                                                //flags: "a" open an existing shared memory segment for read only, "w" read and write to a shared memory segment
if (empty($shared_memory_id))
{
    echo "Failed to open shared memory.<br />";           //<<<< THIS WILL HAPPEN IF THE C APPLICATION HASN'T CREATED THE SHARED MEMORY OR IF IT HAS BEEN SHUTDOWN AND DELETED THE SHARED MEMORY
}
else
{
    //--------------------------------------------
    //----- READ AND WRITE THE SHARED MEMORY -----
    //--------------------------------------------
    echo "Shared memory size: ".shmop_size($shared_memory_id)." bytes<br />";


    //----- READ FROM THE SHARED MEMORY -----
    $shared_memory_string = shmop_read($shared_memory_id, 0, 10);               //Shared memory ID, Start Index, Number of bytes to read
    if($shared_memory_string == FALSE)
    {
            echo "Failed to read shared memory";
            sem_release($semaphore_id);
            exit;
    }

    //Display as a string
    echo "Shared memory string: $shared_memory_string <br />";

    //CONVERT TO AN ARRAY OF BYTE VALUES
    $shared_memory_array = array_slice(unpack('C*', "\0".$shared_memory_string), 1);

    echo "Shared memory bytes: ";
    for($i = 0; $i < 10; $i++)
    {
        echo $shared_memory_array[$i] . ", ";
    }
    echo "<br />";



    //----- WRITE TO THE SHARED MEMORY -----
    if(isset($_REQUEST['shutdown']))          //Include "?shutdown" at the end of the url to write these bytes which causes the C application to exit
    {
        //The array to write
        $shared_memory_array = array(30, 255);

        //Convert the array of byte values to a byte string
        $shared_memory_string = call_user_func_array(pack, array_merge(array("C*"), $shared_memory_array));
        echo "Writing bytes: $shared_memory_string<br />";
        shmop_write($shared_memory_id, $shared_memory_string, 8);           //Shared memory id, string to write, Index to start writing from
                                                                            //Note that a trailing null 0x00 byte is not written, just the byte values / characters, so in this example just 2 bytes are written.
    }


    //Detach from the shared memory
    shmop_close($shared_memory_id);
}

//Release the semaphore
if (!sem_release($semaphore_id))                //Must be called after sem_acquire() so that another process can acquire the semaphore
    echo "Failed to release $semaphore_id semaphore<br />";

//Delete the shared memory (only do this if we created it and its no longer being used by another process)
//shmop_delete($shared_memory_id);

//Delete the semaphore (use only if none of your processes require the semaphore anymore)
//sem_remove($semaphore_id);                //Destroy the semaphore for all processes


echo "Complete<br />";

?>

<p><a href="memshare.php?shutdown">Shutdown C App</a></p>

</body>
</html>

 

Gehe zu Seite Gehe zu Seite  1  2  3  
Zusätzliche Informationen und Funktionen
  Bearbeiten Bearbeiten  

  Versionen Versionen