$pid_file = "/tmp/process.pid";
$pid = posix_getpid();
$fp = fopen($pid_file, 'w+');
if(flock($fp, LOCK_EX | LOCK_NB)){
echo "got the lock \n";
ftruncate($fp, 0); // truncate file
fwrite($fp, $pid);
fflush($fp); // flush output before releasing the lock
sleep(300); // long running process
flock($fp, LOCK_UN); // 释放锁定
} else {
echo "Cannot get pid lock. The process is already up \n";
}
fclose($fp);
$mutex = new SyncMutex("UniqueName");
for($i=0; $i<2; $i++){
$pid = pcntl_fork();
if($pid <0){
die("fork failed");
}elseif ($pid>0){
echo "parent process \n";
}else{
echo "child process {$i} is born. \n";
obtainLock($mutex, $i);
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
function obtainLock ($mutex, $i){
echo "process {$i} is getting the mutex \n";
$res = $mutex->lock(200);
sleep(1);
if (!$res){
echo "process {$i} unable to lock mutex. \n";
}else{
echo "process {$i} successfully got the mutex \n";
$mutex->unlock();
}
exit();
}
parent process parent process child process 1 is born. process 1 is getting the mutex child process 0 is born. process 0 is getting the mutex process 1 successfully got the mutex Child 0 completed process 0 unable to lock mutex. Child 0 completed
for($i=0; $i<3; $i++){
$pid = pcntl_fork();
if($pid <0){
die("fork failed");
}elseif ($pid>0){
//echo "parent process \n";
}else{
echo "child process {$i} is born. \n";
switch ($i) {
case 0:
wait();
break;
case 1:
wait();
break;
case 2:
sleep(1);
fire();
break;
}
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
function wait(){
$event = new SyncEvent("UniqueName");
echo "before waiting. \n";
$event->wait();
echo "after waiting. \n";
exit();
}
function fire(){
$event = new SyncEvent("UniqueName");
$event->fire();
exit();
}
pthread_mutex_lock (mutex) pthread_mutex_trylock (mutex) pthread_mutex_unlock (mutex)
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
typedef struct ct_sum
{
int sum;
pthread_mutex_t lock;
}ct_sum;
void * add1(void *cnt)
{
pthread_mutex_lock(&(((ct_sum*)cnt)->lock));
for(int i=0; i < 50; i++)
{
(*(ct_sum*)cnt).sum += i;
}
pthread_mutex_unlock(&(((ct_sum*)cnt)->lock));
pthread_exit(NULL);
return 0;
}
void * add2(void *cnt)
{
pthread_mutex_lock(&(((ct_sum*)cnt)->lock));
for(int i=50; i<101; i++)
{
(*(ct_sum*)cnt).sum += i;
}
pthread_mutex_unlock(&(((ct_sum*)cnt)->lock));
pthread_exit(NULL);
return 0;
}
int main(void)
{
pthread_t ptid1, ptid2;
ct_sum cnt;
pthread_mutex_init(&(cnt.lock), NULL);
cnt.sum=0;
pthread_create(&ptid1, NULL, add1, &cnt);
pthread_create(&ptid2, NULL, add2, &cnt);
pthread_join(ptid1,NULL);
pthread_join(ptid2,NULL);
printf("sum %d\n", cnt.sum);
pthread_mutex_destroy(&(cnt.lock));
return 0;
}
$lock = new SyncSemaphore("UniqueName", 2);
for($i=0; $i<2; $i++){
$pid = pcntl_fork();
if($pid <0){
die("fork failed");
}elseif ($pid>0){
echo "parent process \n";
}else{
echo "child process {$i} is born. \n";
obtainLock($lock, $i);
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
function obtainLock ($lock, $i){
echo "process {$i} is getting the lock \n";
$res = $lock->lock(200);
sleep(1);
if (!$res){
echo "process {$i} unable to lock lock. \n";
}else{
echo "process {$i} successfully got the lock \n";
$lock->unlock();
}
exit();
}
$key = ftok('/tmp', 'c');
$sem = sem_get($key);
for($i=0; $i<2; $i++){
$pid = pcntl_fork();
if($pid <0){
die("fork failed");
}elseif ($pid>0){
//echo "parent process \n";
}else{
echo "child process {$i} is born. \n";
obtainLock($sem, $i);
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
sem_remove($sem); // finally remove the sem
function obtainLock ($sem, $i){
echo "process {$i} is getting the sem \n";
$res = sem_acquire($sem, true);
sleep(1);
if (!$res){
echo "process {$i} unable to get sem. \n";
}else{
echo "process {$i} successfully got the sem \n";
sem_release($sem);
}
exit();
}
parent process parent process child process 1 is born. process 1 is getting the mutex child process 0 is born. process 0 is getting the mutex process 1 successfully got the mutex Child 0 completed process 0 unable to lock mutex. Child 0 completed
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有