简体   繁体   English

为什么FUSE似乎锁定了所有线程?

[英]Why does FUSE seem to be locking up all threads?

I took fuse hello.c and modified the bottom to show what I am talking about. 我拿了保险丝hello.c并修改了底部以显示我在说什么。 In my app I need to do things after my fuse FS is available. 在我的应用程序中,我需要在保险丝FS可用后做一些事情。 I also need another thread for IPC and keeping certain things up to date. 我还需要IPC的另一个线程,并保持某些事情是最新的。 Because fuse_main doesn't appear to return I threw it in its own thread. 因为fuse_main似乎没有返回,所以我把它扔在自己的线程中。

When I comment out fuse_main the console shows A and B printed. 当我注释掉fuse_main ,控制台显示A和B打印。 However if I don't comment out fuse_main (which is in a different thread) only A is printed. 但是,如果我没有注释掉fuse_main (在不同的线程中),则仅打印A. How the heck is fuse stopping my main thread and how do I run code after FUSE does its thing? 如何保险丝停止我的主线程以及如何在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();
}

fuse_main daemonizes, ie calls fork() and calls _exit(0) in the parent process, so the process exits, hence you only see the A printout. fuse_main daemonizes,即调用fork()并在父进程中调用_exit(0) ,因此进程退出,因此您只能看到A打印输出。

If you give the option -f in ./hello -f /tmp/fuse the fuse_main does not call _exit but stays alive in the foreground and both A and B can be seen. 如果你给的选项-f./hello -f /tmp/fusefuse_main不调用_exit ,但在前台和两个保持活着AB就可以看出。

You surely need a way to end the fuse_main thread gracefully when your program wants to exit: 当程序想要退出时,你肯定需要一种方法来优雅地结束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();
}

Options for hello : 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
[...]

From what I read in the documentation here http://fuse.sourceforge.net/doxygen/hello_8c.html on the usage of the hello.c program, it says that the program exits and vanishes into the background, that is the nature of the fuse_main API . 从我在http://fuse.sourceforge.net/doxygen/hello_8c.html文档中读到的hello.c程序的使用情况来看,它说程序退出并消失在后台,这就是本质fuse_main API。 Why don't you give a try, starting with this code http://fuse.sourceforge.net/doxygen/hello__ll_8c.html , from their description, unlike hello.c this example will stay in the foreground. it also replaced the convenience function fuse_main(..) with a more low level approach. 为什么不试一试,从这个代码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. unlike hello.c this example will stay in the foreground. it also replaced the convenience function fuse_main(..) with a more low level approach. This way in the line, 这种方式在线,

err = fuse_session_loop(se);

in the main function you have control to have additions and do other things you wish to do. 在主要功能中,您可以控制添加内容并执行其他您想要执行的操作。

Also there is a c++ implementation for FUSE, https://code.google.com/p/fusekit/ 还有一个针对FUSE的c ++实现, https://code.google.com/p/fusekit/

Hope this helps. 希望这可以帮助。

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

相关问题 为什么g ++似乎混淆了数据类型? - Why does it seem that g++ is mixing up data types? 为什么线程似乎是随机执行的? - Why do threads seem to execute randomly? 具有单独读取器和写入器线程的单个队列是否需要锁定? - Does a single queue with separate reader and writer threads needs locking? std :: conditional_variable :: notify_all不会唤醒所有线程 - std::conditional_variable::notify_all does not wake up all the threads 具有多个线程的事务和锁定 - Transaction and Locking with multiple threads 锁定多线程 - Locking multiple threads 虚假唤醒是否会解除所有等待线程的阻塞,甚至是不相关的线程? - Does a spurious wake up unblock all waiting threads, even the unrelated ones? 为什么线程似乎没有在这段代码中并行运行? - Why don't threads seem to run in parallel in this code? 为什么 jeprof 评估的 jemalloc 内存配置文件似乎显示所有内存分配? - Why does jemalloc memory profile evaluated by jeprof seem to show all memory allocations? 在C ++中所有并行线程中的所有锁定和解锁实例均使用相同的std :: mutex和lock对象 - Using same std::mutex & lock object everywhere for all instances of locking and unlocking in all parallel threads in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM