If you going to work with semaphore, which was created by some external program, you can try the following code for this program (C example):
#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; /* initial value for sem */
if( semctl( semid, 0, SETVAL, semopts) < 0 ) {/*error*/}
if( semctl( semid, 1, SETVAL, semopts) < 0 ) {/*error*/}
/* PHP wanna zero for its own semget at third sem.
* look at ./PHP_SOURCE_PATH/ext/sysvsem/sysvsem.c
*/
semopts.val = 0;
if( semctl( semid, 2, SETVAL, semopts) < 0 ) {/*error*/}
}
else if(errno == EEXIST) { /* connect only */
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, SVSEM_MODE | IPC_CREAT)) < 0 ) {/*error*/}
}
else {/*error*/}
/*.......*/
/* If you want acquire the sem */
semptr.sem_num = 0;
semptr.sem_op = -1; /* lock it */
semptr.sem_flg = SEM_UNDO;
while( semop(semid, &semptr, 1) < 0 ) {/*error*/}
/*.......*/
Thanks,
Roma