#include <stdio.h>
|
#include <errno.h>
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <sys/wait.h>
|
#include <sys/mman.h>
|
#include <sys/syscall.h>
|
#include <linux/futex.h>
|
#include <sys/time.h>
|
#include "usg_common.h"
|
#include <sys/mman.h>
|
#include <sys/stat.h> /* For mode constants */
|
#include <fcntl.h> /* For O_* constants */
|
#include "sem_util.h"
|
|
|
|
|
|
#include "sem_util.h"
|
|
int _get(key_t key, unsigned int value) {
|
// printf("==================_get===============================\n");
|
int semid, perms;
|
|
perms = S_IRUSR | S_IWUSR;
|
|
semid = semget(key, 1, IPC_CREAT | IPC_EXCL | perms);
|
|
if (semid != -1) { /* Successfully created the semaphore */
|
union semun arg;
|
struct sembuf sop;
|
|
//logger.info("%ld: created semaphore\n", (long)getpid());
|
|
arg.val = 0; /* So initialize it to 0 */
|
if (semctl(semid, 0, SETVAL, arg) == -1)
|
err_exit(errno, "semctl 1");
|
//logger.info("%ld: initialized semaphore\n", (long)getpid());
|
|
/* Perform a "no-op" semaphore operation - changes sem_otime
|
so other processes can see we've initialized the set. */
|
|
sop.sem_num = 0; /* Operate on semaphore 0 */
|
sop.sem_op = value;
|
sop.sem_flg = 0;
|
if (semop(semid, &sop, 1) == -1)
|
err_exit(errno, "semop");
|
//logger.info("%ld: completed dummy semop()\n", (long)getpid());
|
|
} else { /* We didn't create the semaphore set */
|
|
if (errno != EEXIST) { /* Unexpected error from semget() */
|
err_exit(errno, "semget 1");
|
|
} else { /* Someone else already created it */
|
const int MAX_TRIES = 10;
|
int j;
|
union semun arg;
|
struct semid_ds ds;
|
|
semid = semget(key, 1, perms); /* So just get ID */
|
if (semid == -1)
|
err_exit(errno, "semget 2");
|
|
// logger.info("%ld: got semaphore key\n", (long)getpid());
|
/* Wait until another process has called semop() */
|
|
arg.buf = &ds;
|
for (j = 0; j < MAX_TRIES; j++) {
|
//logger.info("Try %d\n", j);
|
if (semctl(semid, 0, IPC_STAT, arg) == -1)
|
err_exit(errno, "semctl 2");
|
|
if (ds.sem_otime != 0) /* Semop() performed? */
|
break; /* Yes, quit loop */
|
sleep(1); /* If not, wait and retry */
|
}
|
|
if (ds.sem_otime == 0) /* Loop ran to completion! */
|
err_exit(errno, "Existing semaphore not initialized");
|
}
|
}
|
return semid;
|
}
|
|
/* Reserve semaphore (blocking), return 0 on success, or -1 with 'errno'
|
set to EINTR if operation was interrupted by a signal handler */
|
|
/* Reserve semaphore - decrement it by 1 */
|
int _dec(int semId) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = -1;
|
sops.sem_flg = SEM_UNDO;
|
|
while (semop(semId, &sops, 1) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_dec");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
int _dec_nowait(int semId) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = -1;
|
sops.sem_flg = IPC_NOWAIT ;
|
|
while (semop(semId, &sops, 1) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_dec_nowait");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
int _dec_timeout(const int semId, const struct timespec *timeout) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = -1;
|
sops.sem_flg = 0;
|
|
while (semtimedop(semId, &sops, 1, timeout) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_dec_timeout");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
|
/**
|
* If sem_op equals 0, the value of the semaphore is checked to see whether it
|
* currently equals 0. If it does, the operation completes immediately; otherwise,
|
* semop() blocks until the semaphore value becomes 0.
|
*/
|
int _zero(int semId) {
|
// logger.debug("%d: _dec\n", semId);
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = 0;
|
sops.sem_flg = 0;
|
|
while (semop(semId, &sops, 1) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_zero");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
|
int _zero_nowait(int semId) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = 0;
|
sops.sem_flg = IPC_NOWAIT;
|
|
while (semop(semId, &sops, 1) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_zero_nowait");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
int _zero_timeout(const int semId, const struct timespec *timeout) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = 0;
|
sops.sem_flg = 0;
|
|
while (semtimedop(semId, &sops, 1, timeout) == -1)
|
if (errno != EINTR) {
|
// err_msg(errno, "_zero_timeout");
|
return errno;
|
}
|
|
return 0;
|
}
|
|
|
/* Release semaphore - increment it by 1 */
|
int _inc(int semId) {
|
struct sembuf sops;
|
|
sops.sem_num = 0;
|
sops.sem_op = 1;
|
sops.sem_flg = 0;
|
|
int rv = semop(semId, &sops, 1);
|
if (rv == -1) {
|
// err_msg(errno, "_inc");
|
return errno;
|
}
|
return 0;
|
}
|
|
void _remove(int semid) {
|
union semun dummy;
|
if (semctl(semid, 0, IPC_RMID, dummy) == -1)
|
err_msg(errno, "_remove");
|
}
|
|
int _set(int semId, int val) {
|
union semun arg;
|
arg.val = val;
|
if (semctl(semId, 0, SETVAL, arg) == -1) {
|
err_msg(errno, "_set");
|
return errno;
|
}
|
return 0;
|
}
|
|
|
#define KEY 0x383
|
|
int main() {
|
int semid = _get(KEY, 1) ;
|
|
if(_dec(semid) != 0)
|
err_exit(errno, "_dec");
|
|
printf("(%ld) 进入互斥区\n", (long) getpid());
|
sleep(10);
|
|
if(_inc(semid) != 0)
|
err_exit(errno, "_inc");
|
}
|