为什么FUSE似乎锁定了所有线程?
Why does FUSE seem to be locking up all threads?
我选择了fuse hello.c并修改了底部以显示我正在谈论的内容。在我的应用程序,我需要做的事情后,我的保险丝FS是可用的。我还需要IPC的另一个线程,并保持某些东西是最新的。因为fuse_main
似乎没有返回,我把它扔到它自己的线程中。
当我注释掉fuse_main
时,控制台显示打印的A和B。然而,如果我不注释出fuse_main
(这是在一个不同的线程),只有a被打印。保险丝是如何停止我的主线程,我如何运行代码后保险丝做它的事情?
#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("An");
sleep(5);
printf("Bn");
t.join();
}
fuse_main
守护进程,即在父进程中调用fork()
并调用_exit(0)
,因此进程退出,因此您只看到A
打印输出。
如果你在./hello -f /tmp/fuse
中给-f
选项,fuse_main
不会调用_exit
,但在前台保持活跃,A
和B
都可以看到。
当你的程序想要退出时,你肯定需要一种方式来优雅地结束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("An");
sleep(5);
printf("Bn");
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.
这样行
err = fuse_session_loop(se);
在main函数中,你可以控制添加和做其他你想做的事情。
也有一个c++实现的FUSE, https://code.google.com/p/fusekit/
希望这对你有帮助。
- 如何检查线程是否锁定
- 在两个线程上读/写 64 位,无互斥/锁定/原子
- 如何在实时应用程序中锁定线程
- 锁定来自其他线程的类成员
- 当只有一个线程主要使用该对象而其他线程很少使用它时,如何最小化该对象的互斥锁锁定?
- 将线程锁定很长时间
- C++将互斥锁锁定为来自另一个线程
- 在以读取为主的多线程程序中,可以使用原子来减少锁定吗
- std::weak_ptr<T>::锁定线程安全吗?
- 线程锁定互斥锁的速度比 std::conditional_variable::wait() 快
- C 优化导致线程锁定
- 是否可以在C++中使用std::atomic_flag获得线程锁定机制
- mutex::lock() 检查一次解锁状态是否已经被另一个线程锁定?
- C++11中的基本线程锁定
- boost线程锁定错误
- 当一个线程锁定一个大映射时,如何避免冻结其他线程
- 导致c++ 11 std::互斥锁将被阻塞的线程锁定到被动等待状态
- shared_ptr.get()可以被多个线程调用,而另一个线程锁定并调用shared_ptr.swap()吗?
- 发现可能由于线程锁定(可能)导致的性能问题
- C++:如何简化线程锁定