PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




sem_acquire

Name

sem_acquire — Acquires the semaphore identified by id .

Synopsis

bool sem_acquire(id);
int id: ID of semaphore to acquire

Returns

TRUE on success; FALSE on failure

Description

sem_acquire() attempts to acquire the semaphore identified by id , and blocks until it can do so. Remember that a semaphore can be simultaneously acquired only as many times as specified in the max_acquire parameter to sem_get() ; if this limit has been reached, sem_acquire() blocks until the semaphore is released. id must be a valid semaphore identifier as returned by sem_get() .

Version

PHP 3 since 3.0.6, PHP 4

See also

See also sem_get() , sem_release()

Example

Example 1170. Simple multiple-script lock blocking

/* To test this script, create two browser windows and load the
 * page containing this script in each of them simultaneously.
 * The script that first gets the semaphore will acquire it and go
 * to sleep for 10 seconds before releasing it, during which time
 * no other process will be able to acquire that semaphore. The other 
 * script will block after printing 'acquiring...', until the first
 * script releases the semaphore. At this point, the second script
 * will wake up and run to completion.
 */

define('MY_SEMAPHORE_KEY', 30);

if (!$sem_id = sem_get(MY_SEMAPHORE_KEY)) {
    echo "Could not get ID for the semaphore.\n";
} else {
    echo "Got semaphore ID; acquiring...\n";
    flush();
    if (!sem_acquire($sem_id)) {
        echo "failed.\n";
    } else {
        echo "OK. Sleeping for 10 seconds, then releasing.\n";
        flush();
        sleep(10);
        sem_release($sem_id);
        echo "Semaphore released. Other scripts should be able to get it now.\n";
    }
}



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.