macos - mac os x - sem_open 返回 errno 0,未定义错误

标签 macos semaphore

我想在 mac os x 中创建一个信号量:

const char *semaphore_open_path = "/tmp/sem_handle_open";
errno = 0;
sem_t *semaphore_handle_open = sem_open(semaphore_open_path, O_CREAT, S_IRUSR | S_IWUSR, 0);
if(semaphore_handle_open == SEM_FAILED || !semaphore_handle_open)
{
    printf("ERROR sem_open init: %s , %d\n", strerror(errno), errno);
    exit(EXIT_FAILURE);
}

我收到错误:错误 sem_open init: 未定义错误:0、0

我做错了什么? 谢谢。

最佳答案

sem_open 需要一个名称,而不是路径。另外,errno 应该声明为 extern int errno,否则它将始终为零!

此代码片段在我的 Mac 上运行良好。

#include <semaphore.h>
#include <stdio.h>
#include <string.h>

extern int errno;

int main(void) {

  sem_t *sem = sem_open("semaphore", O_CREAT, S_IRUSR | S_IWUSR, 0);
  if (!sem) {
    fprintf(stderr, "%s (%d)", strerror(errno), errno);
  }
  return 0;

}

关于macos - mac os x - sem_open 返回 errno 0,未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154252/

相关文章:

c - 理解 C 中的信号量

c# - 如何确保一个线程恰好在特定数量的其他线程运行结束后运行?

c - 当进程被终止时,有没有办法让线程从无限信号量等待中出来?

macos - 10.7.3 之前的 Mac App Store 沙箱和处理安全范围书签

c++ - 阻止 Apple clang 将 Xcode 路径添加到标准包含路径

java - 同步块(synchronized block): lock many objects?

node.js - NodeJS分布式处理(分区-集群)

macos - VSCode 在格式化 Dart 文件时不考虑标签大小

macos - 如何在 Bash 中列出可用的音频 I/O 设备

macos - 透明 MTKView 未与其后面的窗口正确混合