【问题标题】:Why does FUSE seem to be locking up all threads?为什么 FUSE 似乎锁定了所有线程?
【发布时间】:2015-08-03 12:58:58
【问题描述】:

我拿了保险丝hello.c 并修改了底部以显示我在说什么。在我的应用程序中,我需要在我的 fuse FS 可用后做一些事情。我还需要另一个 IPC 线程并保持某些内容是最新的。因为fuse_main 似乎没有返回,所以我把它扔到了它自己的线程中。

当我注释掉 fuse_main 时,控制台显示打印了 A 和 B。但是,如果我不注释掉 fuse_main(在不同的线程中),则只会打印 A。 fuse 是如何停止我的主线程的,以及在 FUSE 完成它之后我如何运行代码?

#define FUSE_USE_VERSION 26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

static const char *hello_str = "Hello World!\n";
static const char *hello_path = "/hello";

static int hello_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));
    if (strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
    } else if (strcmp(path, hello_path) == 0) {
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(hello_str);
    } else
        res = -ENOENT;

    return res;
}

static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
             off_t offset, struct fuse_file_info *fi)
{
    (void) offset;
    (void) fi;

    if (strcmp(path, "/") != 0)
        return -ENOENT;

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, hello_path + 1, NULL, 0);

    return 0;
}

static int hello_open(const char *path, struct fuse_file_info *fi)
{
    if (strcmp(path, hello_path) != 0)
        return -ENOENT;

    if ((fi->flags & 3) != O_RDONLY)
        return -EACCES;

    return 0;
}

static int hello_read(const char *path, char *buf, size_t size, off_t offset,
              struct fuse_file_info *fi)
{
    size_t len;
    (void) fi;
    if(strcmp(path, hello_path) != 0)
        return -ENOENT;

    len = strlen(hello_str);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, hello_str + offset, size);
    } else
        size = 0;

    return size;
}

static struct fuse_operations hello_oper;
//modification starts below this line
#include<thread>
#include<unistd.h>
int main(int argc, char *argv[])
{
    std::thread t([&]{
        hello_oper.getattr  = hello_getattr;
        hello_oper.readdir  = hello_readdir;
        hello_oper.open     = hello_open;
        hello_oper.read     = hello_read;

        return fuse_main(argc, argv, &hello_oper, NULL);
    });
    printf("A\n");
    sleep(5);
    printf("B\n");
    t.join();
}

【问题讨论】:

    标签: c++ c fuse


    【解决方案1】:

    从我在http://fuse.sourceforge.net/doxygen/hello_8c.html 的文档中读到的关于hello.c 程序的使用,它说程序退出并消失在后台,这就是fuse_main API 的本质。你为什么不试一试,从这段代码开始 http://fuse.sourceforge.net/doxygen/hello__ll_8c.html,根据他们的描述, unlike hello.c this example will stay in the foreground. it also replaced the convenience function fuse_main(..) with a more low level approach.这样就行了,

    err = fuse_session_loop(se);

    在主函数中,您可以控制添加内容并执行您想做的其他事情。

    FUSE 还有一个 c++ 实现,https://code.google.com/p/fusekit/

    希望这会有所帮助。

    【讨论】:

    • 如果 fuse_main() 函数派生然后,是的,你的线程将消失,因为派生只保留派生线程的执行。
    【解决方案2】:

    fuse_main 守护进程,即在父进程中调用fork() 并调用_exit(0),因此进程退出,因此您只能看到A 打印输出。

    如果您在./hello -f /tmp/fuse 中提供-f 选项,则fuse_main 不会调用_exit,而是在前台保持活动状态,并且可以看到AB

    当您的程序想要退出时,您肯定需要一种方法来优雅地结束 fuse_main 线程:

    //modification starts below this line
    #include<thread>
    #include<unistd.h>
    #include <signal.h>
    #include <sys/syscall.h>
    
    int main(int argc, char *argv[])
    {
        pid_t tid;
        std::thread t([&]{
            hello_oper.getattr  = hello_getattr;
            hello_oper.readdir  = hello_readdir;
            hello_oper.open     = hello_open;
            hello_oper.read     = hello_read;
    
            tid = syscall(SYS_gettid);
            return fuse_main(argc, argv, &hello_oper, NULL);
        });
        printf("A\n");
        sleep(5);
        printf("B\n");
        kill(tid, SIGTERM);
        t.join();
    }
    

    hello 的选项:

    general options:
        -o opt,[opt...]        mount options
        -h   --help            print help
        -V   --version         print version
    
    FUSE options:
        -d   -o debug          enable debug output (implies -f)
        -f                     foreground operation
        -s                     disable multi-threaded operation
    [...]
    

    【讨论】:

    • 谢谢。在我一切正常后,我会接受答案。无论如何,您在哪里找到-f 选项?我想查看所有选项
    • 我在调查 fuse 源代码以查看 fuse_main 的作用时发现了它们。但我看到他们也可以简单地用./hello --help打印出来。
    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多