如果您要使用由某些外部程序创建的信号量,则可以尝试以下代码段(C 示例)
#define SVSEM_MODE (SEM_R | SEM_A | SEM_R>>3 | SEM_R>>6) /* 0644 */
#define PHP_SEM_NEED_NUMBER 3
/*.......*/
int semid, semflag = SVSEM_MODE | IPC_CREAT | IPC_EXCL;
struct sembuf semptr;
union semun semopts;
/*.......*/
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, semflag)) >= 0 ) {
semopts.val = 1; /* 信号量的初始值 */
if( semctl( semid, 0, SETVAL, semopts) < 0 ) {/*错误*/}
if( semctl( semid, 1, SETVAL, semopts) < 0 ) {/*错误*/}
/* PHP希望在第三个信号量上获得零值。*/
/* 请查看 ./PHP_SOURCE_PATH/ext/sysvsem/sysvsem.c */
*/
semopts.val = 0;
if( semctl( semid, 2, SETVAL, semopts) < 0 ) {/*错误*/}
}
else if(errno == EEXIST) { /* 只连接 */
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, SVSEM_MODE | IPC_CREAT)) < 0 ) {/*错误*/}
}
else {/*错误*/}
/*.......*/
/* 如果你想获取信号量 */
semptr.sem_num = 0;
semptr.sem_op = -1; /* 锁定它 */
semptr.sem_flg = SEM_UNDO;
while( semop(semid, &semptr, 1) < 0 ) {/*错误*/}
/*.......*/
感谢,
Roma