文件读取锁定失败,错误号为 EBADF

Read lock on file failed with error number EBADF

我正在尝试编写一个测试程序来测试文件锁定是否正常工作。但是,其中一项测试失败了。我正在尝试获取文件的读锁,但测试失败。当我尝试打印错误编号时,它打印了 9,表示 EBADF 即错误的文件编号(或文件描述符)。我还尝试打印文件描述符值,它打印的 3 似乎是一个有效的文件描述符。我正在使用 Netbeans IDE 及其简单测试功能。

要读取的文件与源代码文件在同一文件夹中。

这是我的代码:

#include <stdlib.h>
#include <iostream>
#include <sys/file.h>
#include <sys/unistd.h>
using namespace std;
/*
 * Simple C++ Test Suite
 */
int acquireRLock(int fd, struct flock &lock) {
    lock.l_len = 0;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_type = F_RDLCK;
    struct flock temp_lock = lock;
    fcntl(fd, F_GETLK, &temp_lock);
    if(temp_lock.l_type == F_WRLCK) {
        return -1;
    }
    else {
        int ret_val = fcntl(fd, F_SETLK, &lock);
        if(ret_val == -1) cout << "Failed here errno = " << errno << " fd = " << fd << endl;
        return ret_val;
    }
}
int acquireWLock(int fd, struct flock &lock) {
    lock.l_len = 0;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_type = F_WRLCK;
    struct flock temp_lock = lock;
    fcntl(fd, F_GETLK, &temp_lock);
    if(temp_lock.l_type == F_RDLCK || temp_lock.l_type == F_WRLCK)
        return -1;
    else
        return fcntl(fd, F_SETLK, &lock);
}
int FileOpenTest() {
    int fd = open("test_file.txt", O_WRONLY);
    if(fd == -1) {
        cout << "%TEST_FAILED% time=0 testname=FileOPenTest (newsimpletest) message=file open failed" << endl;
    }
    return fd;
}
void FileReadLockTest(int fd) {
    struct flock lock;
    if(acquireRLock(fd, lock) < 0) {
        cout << "%TEST_FAILED% time=0 testname=FileReadLockTest (newsimpletest) message=file read lock acquire failed" << endl;
    }
}
void FileWriteLockTest(int fd) {
    struct flock lock;
    if(acquireWLock(fd, lock) < 0) {
        cout << "%TEST_FAILED% time=0 testname=FileWriteLockTest (newsimpletest) message=file write lock acquire failed" << endl;
    }
}
void FileWriteTest(int fd) {
    if(write(fd, (void*) "I am a ball", 13) <= 0) {
        cout << "%TEST_FAILED% time=0 testname=FileWriteTest (newsimpletest) message=file write failed" << endl;
    }
}
void FileCloseTest(int fd) {
    if(close(fd) < 0) {
        cout << "%TEST_FAILED% time=0 testname=FileCloseTest (newsimpletest) message=file close failed" << endl;
    }
}
int main(int argc, char** argv) {
    std::cout << "%SUITE_STARTING% newsimpletest" << std::endl;
    std::cout << "%SUITE_STARTED%" << std::endl;

    std::cout << "%TEST_STARTED% FileOpenTest (newsimpletest)" << std::endl;
    int fd = FileOpenTest();
    std::cout << "%TEST_FINISHED% time=0 FileOpenTest (newsimpletest)" << std::endl;

    std::cout << "%TEST_STARTED% FileReadLockTest (newsimpletest)\n" << std::endl;
    FileReadLockTest(fd);
    std::cout << "%TEST_FINISHED% time=0 FileReadLockTest (newsimpletest)" << std::endl;

//    std::cout << "%TEST_STARTED% FileWriteLockTest (newsimpletest)\n" << std::endl;
//    FileWriteLockTest(fd);
//    std::cout << "%TEST_FINISHED% time=0 FileWriteLockTest (newsimpletest)" << std::endl;
//    
//    std::cout << "%TEST_STARTED% FileWriteTest (newsimpletest)\n" << std::endl;
//    FileWriteTest(fd);
//    std::cout << "%TEST_FINISHED% time=0 FileWriteTest (newsimpletest)" << std::endl;

    std::cout << "%TEST_STARTED% FileCloseTest (newsimpletest)\n" << std::endl;
    FileCloseTest(fd);
    std::cout << "%TEST_FINISHED% time=0 FileCloseTest (newsimpletest)" << std::endl;

    std::cout << "%SUITE_FINISHED% time=0" << std::endl;

    return (EXIT_SUCCESS);
}

它显示以下输出:

在取消注释获取写锁的代码后,获取了写锁并写入了文件,但读锁仍然失败,如下所示。但是,实际行为应该是读取锁定成功和写入锁定失败:

请帮我找出测试失败的原因。

man page for fcntl我们可以阅读

In order to place a read lock, fd must be open for reading. In order to place a write lock, fd must be open for writing. To place both types of lock, open a file read-write.

您正在使用

打开文件
int fd = open("test_file.txt", O_WRONLY);

来自man page for open

O_RDONLY Open for reading only.

O_WRONLY Open for writing only.

O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO.

要放置读写锁,用

打开文件
int fd = open("test_file.txt", O_RDWR);