Re: CVS update: php3/doc
| From: | Tom May | Date: | Thu, 19 Nov 1998 17:25:45 +0000 |
| Subject: | Re: CVS update: php3/doc | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-2486@lists.php.net to get a copy of this message | ||
eschmid <php-dev@lists.php.net> writes:
> Date: Wednesday November 18, 1998 @ 14:25
> Author: eschmid
>
> Update of /repository/php3/doc
> In directory asf:/u2/tmp/cvs-serv8659/doc
>
> Modified Files:
> manual.sgml
> Log Message:
> New semaphores between regex and solid.
> Tom can you provide some very useful examples?
Here are some examples. First a quick demo to make sure everything is
working ok:
<?php
/* Simple semaphore demo. If you run this script concurrently in two
processes, you'll be able to see one process block while the other
holds the semaphore for 5 seconds. After running this, you might
want to use the ipcs and ipcrm commands to remove the semaphore
with key 0x9900, otherwise it will exist until your system is
rebooted. */
/* Get the semaphore with key 0x9900 and allow only 1 process to
acquire it at a time. Use permissions 0600 so only the owner has
access to it. Note that if this fails and we don't check for
error it's not the immediate end of the world; subsequent calls to
sem_acquire() and sem_release() will fail without performing a
semaphore operation and life will go on but without semaphore
protection. */
$sem = sem_get(0x9900, 1, 0600);
echo "semaphore id is $sem<br>\n";
if (!sem_acquire($sem)) {
echo "sem_acquire failed<br>\n";
}
else {
echo "acquired sem $sem<br>\n";
}
/* Other processes calling sem_acquire() on the same semaphore will
block while we kill some time here. */
sleep(5);
/* Release the semaphore so some other process can acquire it. This
will be done automatically when the script finishes, but a warning
will be generated since it's not a good idea to hold a semaphore
longer than necessary. It will also be cleaned up by the OS
if the process terminates abnormally. */
if (!sem_release($sem)) {
echo "sem_release failed<br>\n";
}
else {
echo "released sem $sem<br>\n";
}
?>
And here's an example of how you might use semaphores in practice:
<?php
/* A class which uses a semaphore to limit the number of processes
actively using a database. Any number of processes can connect to
the database, but only $max_active per machine are allowed to have
queries outstanding. */
class dbhandle {
var $server, $username, $passwd, $dbname;
var $sem;
var $dbid;
function dbhandle($server, $username, $passwd, $dbname, $semkey, $max_active) {
$this->server = $server;
$this->username = $username;
$this->passwd = $passwd;
$this->dbname = $dbname;
$this->sem = sem_get($semkey, $max_active);
}
function connect() {
sem_acquire($this->sem);
$this->dbid = sybase_pconnect($this->server, $this->username, $this->passwd);
if ($this->dbid) {
$select_worked = sybase_select_db($this->dbname, $this->dbid);
}
sem_release($this->sem);
return $select_worked;
}
function query($sql) {
sem_acquire($this->sem);
$resid = sybase_query($sql, $this->dbid);
sem_release($this->sem);
return $resid;
}
}
/* Create a database handle protected by a semaphore to limit
the number of processes actively using the database to 10. */
$db = new dbhandle("myserver", "me", "mypasswd", "mydb",
0xDBDB, 10);
if (!$db->connect()) {
echo "connect failed<br>\n";
return;
}
$resid = $db->query("select * from mytable");
/* Etc. */
?>
Tom.
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net