【发布时间】:2016-05-03 22:12:16
【问题描述】:
我正在尝试使用 sem_open 将我的信号量初始化为 1,但我似乎无法让它工作。
sem_t *Montague, *Capulet; // the two semaphores
char sem_Montague[]= "jud_Montague"; // their names
char sem_Capulet[]= "jud_Capulet"; // their names
int semvalue; // stores value returned by sem_getvalue()
Montague = sem_open(sem_Montague, O_CREAT, 0600, 1);
// We specify that the semaphore should be created it it did
// not exist already, prevent other users from accessing it
// (0600) and set its initial value to one
if (Montague == SEM_FAILED) {
perror("unable to create Montague semaphore");
sem_unlink(sem_Montague);
exit(1);
} // if
// Just to show that we can test the value of a semaphore
sem_getvalue(Montague, &semvalue);
printf("The initial value of sem_Montague is %d\n", semvalue);
当我运行代码时,输出是:
The initial value of sem_Montague is 0
它应该是 1,我不确定为什么它没有正确初始化。
【问题讨论】:
-
我在我的系统上得到 1...你使用什么编译器/工具链?什么平台?
-
我正在使用带有 g++ 编译器的 OS Yosemite。使用 g++ 编译 -ipthread verona.cpp -o verona
-
Mac 的有趣之处在于,g++ 经常默默地别名为 clang。