简体   繁体   English

sem_wait POSIX API和OSX

[英]sem_wait POSIX API and OSX

Im trying to write a simple semaphore program, and found out several things that are different in OSX. 我试图编写一个简单的信号量程序,并发现了OSX中的一些不同之处。 I use Mountain Lion and the latest Xcode version. 我使用Mountain Lion和最新的Xcode版本。 Forgetting the syntactical errors, of missing braces.. because i have more code below, did not copy the full snippet, 忘记了大括号缺失的语法错误..因为我在下面有更多代码,所以没有复制完整的代码段,

Basically i except the code to stop with the sem_wait and not go beyond. 基本上我除了用sem_wait停止且不会超越的代码。

the code compiles and output is as follows 代码编译并输出如下

Output:
-------
    Semaphore wait failed with ret code: -1, and err: 9. 
    Semaphore init failed with ret code: -1, and err: 9.

Tracing back to error code 9, it is EBADF 追溯到错误代码9,即EBADF

My program is 我的程序是

int main(int argc, char * argv[])
{
    pthread_t tid1, tid2;
    int rc;

    rc = sem_unlink(&mutex);
    rc = sem_open(&mutex, O_CREAT,O_RDWR,0);
    rc = sem_wait(&mutex);

    if(rc == 0) {
        printf("Semaphore try wait ok!. \n");
    } else {
        printf("Semaphore wait failed with ret code: %d, and err: %d. \n",
               rc, errno);
    }

    if(rc != SEM_FAILED) {
        printf("Semaphore init ok!. \n");
    } else {
        printf("Semaphore init failed with ret code: %d, and err: %d. \n",
               rc, errno);
        return 0;
    }

Any help here is highly invaluable. 这里的任何帮助都是非常宝贵的。

sem_unlink takes a char * that is the name of the semaphore. sem_unlink一个char * ,它是信号量的名称。 sem_open takes the same, and returns a semaphore descriptor of type sem_t * . sem_open采用相同的sem_open ,并返回sem_t *类型的信号量描述符。 It's this semaphore descriptor that you should be passing to sem_wait . 您应该将这个信号量描述符传递给sem_wait If you fix things so it actually compiles without warnings, like the code below, then it behaves as you'd expect: 如果您修复了问题,使其实际上在没有警告的情况下进行编译(例如下面的代码),那么它的行为将达到您的期望:

#include <semaphore.h>
#include <stdio.h>
#include <sys/errno.h>


int main(int argc, char **argv)
{
    const char *semaphore_name = "my-test-semaphore";

    int rc = sem_unlink(semaphore_name);
    if (rc)
        perror("sem_unlink");

    sem_t *semaphore = sem_open(semaphore_name, O_CREAT, O_RDWR, 0);
    if (semaphore == SEM_FAILED) {
        perror("sem_open");
        return 1;
    }

    rc = sem_wait(semaphore);
    if (rc) {
        perror("sem_wait");
        return 1;
    }

    return 0;
}

You should also be aware of the problems with POSIX semaphores , namely that it's very easy to leak the semaphore count if your application exits unexpectedly. 您还应该注意POSIX信号量存在的问题 ,即,如果您的应用程序意外退出,很容易泄漏信号量计数。 The fact that your sample code mentioned pthread_t suggests that you're trying to use semaphores within a single process. 示例代码中提到了pthread_t的事实表明您正在尝试在单个进程中使用信号量。 Named POSIX semaphores are not what you want for that task. 命名的POSIX信号量不是该任务所需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM