问题
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.
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);
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.
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!
回答1:
Check out
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;
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
来源:https://stackoverflow.com/questions/61914398/create-new-system-call-in-xv6-that-returns-data-about-an-open-files-for-all-runn