我拿了保险丝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();
}
Run Code Online (Sandbox Code Playgroud)
fuse_main守护进程,即父进程中的调用fork()和调用_exit(0),因此进程退出,因此您只能看到A打印输出.
如果你给的选项-f中./hello -f /tmp/fuse的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("A\n");
sleep(5);
printf("B\n");
kill(tid, SIGTERM);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
选项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
[...]
Run Code Online (Sandbox Code Playgroud)