Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Using System V Shared Memory on the Google App Engine Flexible PHP Runtime description: Learn how to use System V shared memory in a PHP App Engine flexible environment app. author: tmatsuo tags: App Engine, PHP date_published: 2017-01-23


System V IPC family

PHP has a wrapper for System V IPC family of functions including semaphores, shared memory and inter-process messaging. For more details, visit the official documentation.

Our PHP runtime provides these features out of the box, so you can immediately start using them on App Engine flexible environment. We'll walk through a small demo app to learn how to use them.

Prerequisites

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.

Simple counter with the shared memory

  1. Create a directory

    mkdir myapp
    cd myapp
    
  2. Create an app.yaml file:

    runtime: php
    env: flex
    
    manual_scaling:
        instances: 1
    
  3. Create an index.php file:

    <?php
    
    # Obtain an exclusive lock
    $key = ftok(__FILE__, 'p');
    $semid = sem_get($key);
    sem_acquire($semid);
    
    # Attach shared memory
    $id = shm_attach($key);
    
    # Count up
    if (shm_has_var($id, 0)) {
        $count = shm_get_var($id, 0) + 1;
    } else {
        $count = 1;
    }
    
    # Save the value
    shm_put_var($id, 0, $count);
    
    shm_detach($id);
    
    # Release the lock
    sem_release($semid);
    echo sprintf("%d visits so far", $count);
    
  4. Deploy the app

    gcloud app deploy app.yaml
    
  5. Access the app and see the counts. For example, you can use ab:

    ab -n100 -c10 http://your-project-id.appspot-preview.com/
    

and access the same URL with a browser to see the counter.

Caveats

This counter application is not for production use at all. The shared memory is not shared among multiple servers, and also the memory will go away when the server restarts. It only demonstrates how they work.

Next step

  1. Learn more about System V IPC family
See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.