c - 在 Solaris 5.10 上使用集群()

标签 c gcc compiler-errors solaris flock

我正在为 Solaris 5.10 编写一个 C 实用程序,它使用 flock()用于文件同步。我的目标是与还调用 flock() 的现有遗留系统进行互操作在引擎盖下。根据我机器上的手册页,它在 BSD 兼容模式下受支持 - 毕竟遗留系统是以某种方式编译的! (不幸的是,我现在只能访问二进制文件。)

问题是,当我尝试编译代码时,它失败并出现以下错误:

main.c: In function `main':
main.c:11: warning: implicit declaration of function `flock'
main.c:11: error: `LOCK_EX' undeclared (first use in this function)
main.c:11: error: (Each undeclared identifier is reported only once
main.c:11: error: for each function it appears in.)
main.c:11: error: `LOCK_NB' undeclared (first use in this function)

这是一个简短的可重现示例(如果您手头有 5.10,则可以重现......):

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char** argv) {
  int fd;
  if ((fd = open("file.txt", O_CREAT | O_RDWR, 0644) == -1))
    return 1;
  if (flock(fd, LOCK_EX | LOCK_NB) == -1)
    return 1;

  puts("File locked!");
  return 0;
}

我使用 gcc 3.4.3 并带有以下参数:-o test -std=gnu99 -Wall main.c 。错误消息似乎表明我包含了错误的头文件,但 flock() 的手册页除了标准 #include <sys/file.h> 之外没有提及任何内容.

我需要指定哪些编译器标志才能使用 flock() 获取程序在 Solaris 5.10 上工作?

最佳答案

flock() 已经很老了,Solaris 系统不支持,您必须选择其他替代系统调用,例如 fcntl(),如 here 中所述。

手册页说:这些接口(interface)的使用应仅限于在 BSD 平台上编写的应用程序。不支持将这些接口(interface)与任何系统库或多线程应用程序一起使用。

关于c - 在 Solaris 5.10 上使用集群(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59033269/

相关文章:

将字列转换为整数的 C 程序

iPhone应用程序在模拟器中运行,但不在设备上运行

c# - 为什么 C# 编译器不会在静态方法调用实例方法的地方出现错误代码?

c++ - 如何在结构中嵌入 CUDA 纹理对象?

如果 Linux 内核模块不使用任何物理硬件,它可以使用 UIO 吗?

c - 增量运算符的原子性

c - MAC 10.7 : Where is the gl. h 文件?

c++ - 英特尔 C++ 编译器 : What is highest GCC version compatibility?

c++ - 无法在 Windows 上将 SDL 与 MinGW 一起使用

java - Sipdroid 构建错误