Need help with writeing a new system call! What is a file descriptor?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need help with writeing a new system call! What is a file descriptor?
Hi,
I need a to write a system call. My question is: "WHAT EXACTLY IS A FILE DESCRIPTOR??!?". My system call is supposed to returns a number of open files for a process identified by a pid and file desctiptor for each file used by a process. The system call should look like this:
int get_open_files(pid_t pid, int *count, int** fds); returnt value is error code or an integer greater then 0 if evrything worked fine.
Geting the count is not a problem. The problem is getting (int **fds)!!! I guess it can be found by looking into task_struct of a proces and then into struct files_struct *files whitin task_struct. There is a field struct file ** fd in files_struct. This is my best. I am stuck here. Please help me. Once again: What exactly is a file descriptor? Ideal answer shoul point out one or more fields form a certain structure. For example "A file desctiptor, you idiot, is: some_stuct->some_field1, some_struct->some_field2...)
Thanx a lot
Dragan
and initalise fp = fopen(file,mode);
fopent will return a pointer (memory location of that file starting location).
that is know as file descriptor and it is of type unsigned int.
even you declare
int fp;
fp = open(file,mode,flag); or
fp = creat(file,mode,flag); or read write and so on...
in your declaration,
int get_open_files(pid_t pid, int *count, int** fds);
change it to
int get_open_files(pid_t pid, int *count, unsigned int** fds);
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by smsundar
When you declare
FILE *fp;
and initalise fp = fopen(file,mode);
fopent will return a pointer (memory location of that file starting location).
that is know as file descriptor and it is of type unsigned int.
Hmmm, no. That is known as a file pointer (as the fp name suggests), and is certainly not of type unsigned int but of type pointer to a structure.
Quote:
even you declare
int fp;
fp = open(file,mode,flag); or
fp = creat(file,mode,flag); or read write and so on...
You got it better here, but you should use "fd", not "fp" to avoid confusion.
Quote:
in your declaration,
int get_open_files(pid_t pid, int *count, int** fds);
change it to
int get_open_files(pid_t pid, int *count, unsigned int** fds);
That's optimistic, I do not expect a process with more than 2147483648 open files
Quote:
hope its wright now...
hope i have cleared your doubt?
Hope I've cleared yours.
I guess the OP is asking about file descriptors in the kernel, not in userspace.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.