在xv6中创建新的系统调用,该调用返回有关所有正在运行的进程的打开文件的数据 [英] Create new system-call in xv6 that returns data about an open files for all running processes
问题描述
我开始学习xv6. 我正在尝试添加一个新的系统调用,该调用将为所有正在运行的进程打印打开文件的列表. 如果文件是常规文件或管道文件,并且文件可读性可写,则必须打印每个进程的pid,其文件描述符号(每个pid为0、1,2-).
Im started to learn about xv6. And I'm trying to add a new system call that will print the list of open files for all running processes. It has to print pid of each process, its file descriptor number (0,1,2- for each pid), if the file is regular or piped and if the file is readable of writable.
所以我知道如何获取pid.这是一个代码示例:
So I know is how to get the pid. Here is an example of a code:
struct proc *p;
sti();
acquire(&ptable.lock);
cprintf("name \t pid \t type \t \n");
for (p=ptable.proc; p<&ptable.proc[NPROC]; p++){
cprintf("%s \t %d \n", p->name, p->pid);
}
}
release (&ptable.lock);
我不知道并且在互联网上找不到的是如何检查由文件描述符命名的文件是否可写,可擦写,都可写 而且我不知道如何检查由文件描述符命名的文件的类型是否为pipe \ regular.
What I don't know and couldn't find on the internet is how to check if file named by file descriptor is writable\riadable\both And I don't know how to check if the type of the file named by file descriptor is pipe\regular.
我查看了file.h
,其中有类似type
(FD_NONE,FD_PIPE,FD_INODE),char readable,char writable
的字段.
但是我不知道如何得到他们...
I looked at file.h
and there are fields like type
(FD_NONE, FD_PIPE, FD_INODE), char readable,char writable
.
But I dont understand how to get them...
如果您有任何带有字幕信息的资源可以与我分享,或者可以提供帮助,我很高兴听到您的声音. 非常感谢!
If you have any resources with subtitle info to share with me or if you can help, I would be happy to hear. Thanks a lot!
推荐答案
签出
-
struct proc
file.c
中的功能
proc.h
中的struct proc
inproc.h
- functions in
file.c
您应该能够获得一个指向打开文件数组开头的指针,如下所示:
You should be able to get a pointer to the beginning of the open files array like this:
struct file* fp = (struct file*) &p->ofile;
从那里,您可以从ptable
循环中复制相同的语法来循环遍历文件.要检查文件是否可读,file.c
中的功能只需检查可读标志:
From there, you can copy the same syntax from the ptable
loop to loop through the files. And to check if a file is readable, functions in file.c
just check the readable flag:
if(fp->readable && fp->type == FD_PIPE)
// do some logic
这篇关于在xv6中创建新的系统调用,该调用返回有关所有正在运行的进程的打开文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!