簡體   English   中英

為什么FUSE似乎鎖定了所有線程?

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

我拿了保險絲hello.c並修改了底部以顯示我在說什么。 在我的應用程序中,我需要在保險絲FS可用后做一些事情。 我還需要IPC的另一個線程,並保持某些事情是最新的。 因為fuse_main似乎沒有返回,所以我把它扔在自己的線程中。

當我注釋掉fuse_main ,控制台顯示A和B打印。 但是,如果我沒有注釋掉fuse_main (在不同的線程中),則僅打印A. 如何保險絲停止我的主線程以及如何在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,即調用fork()並在父進程中調用_exit(0) ,因此進程退出,因此您只能看到A打印輸出。

如果你給的選項-f./hello -f /tmp/fusefuse_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
[...]

從我在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. 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/

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM