how to access files via their descriptors?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to access files via their descriptors?
# 1  
Old 01-31-2009
how to access files via their descriptors?

hello, I'm new here, and new to Linux.
I was wondering how to get hold of a certain file's descriptor, and how to use that number so that I access the contents of the file.

please give me a full example, that is
(1) how to determine the descriptor and
(2) what magic symbols to use so that one can open the file in say mcedit.

thank you.
# 2  
Old 01-31-2009
Could you be more specific about what you're trying to do? A file won't have a descriptor unless your process has already opened it.
# 3  
Old 01-31-2009
hmm.. OK, so for a descriptor, it must be opened by a process.....
well then, what about the inode number, when you run ls -li ?
can that be used for anything?

all in all, I'm just curious about these things, I was fascinated to think that in Linux you can access a file other ways than writing its name..
kind of like kill [PID] is.
# 4  
Old 01-31-2009
To get a (new) file descriptor to do anything with you must first call open()*, which takes a filename. So yes, to open a file, you need a filename; the act of opening a file creates a file descriptor to represent that, the same way that running a program creates a new process ID to represent that.

Files are rather different from processes though, in that the PID is fundamental, and systemwide. The filename a process was ran with really isn't relevant since
a) If you have 4 xterms doing different things, you can't tell them apart by what file you ran
b) Lots of processes clone themselves, instead of loading new programs

So the PID is really the only way to identify processes. Commands like killall just trawl through the system's big list of PID's for you.

It's the files themselves that're unusual, and the ways you can use them, not the ways they're opened. There's several kinds of files:
  • Ordinary files, just blocks of data like you'd expect.
  • Symbolic links, a kind of special file that leads to a different file or directory sort of like a hyperlink leads to a website.
  • Hard links, where the same file is literally in two entirely seperate places. They have to be on the same partition, and share the same inode.
  • Named pipes aka FIFOs, where data written into the file by one process just gets pumped into whatever process is reading it.
  • Domain sockets, a kind of beefed-up version of named pipes that adds things like the ability for one server to talk to many clients over it.
  • Device files, which literally represent some hardware device or system-provided data source. 'dd if=/dev/random of=/dev/dsp' will read random data from the kernel and hurl it into your sound device on Linux systems with OSS compatibility...

You can also use FD's that have already been opened for you. The FD's 0, 1, and 2 are usually provided for you, where 0 represents the input stream(keyboard, for a console), 1 represents the output stream(the console display, for a console), and 2 represents error message output(also the console display). You can rearrange them how you please, having input coming from a file instead of console and having output going into a file or into another process etc etc etc.

* Okay, socket() and pipe() make FDs too, but have nothing to do with files on disk.

Last edited by Corona688; 01-31-2009 at 03:17 PM..
# 5  
Old 01-31-2009
thank you for your answer, Corona.
I appreciate your time.

a lot of learning is due..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read loop from two files using descriptors

What I would like to do is read each line in the atdinfile: A sample atdinfile would look like this: 651 652 653 654 655 656 657 658 659 660 661 664 665 666 667 668 (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

3. UNIX for Dummies Questions & Answers

kernel giving access for multiple users to access files

hi all, i want to know y kernel is giving access for multiple users to access a file when one user may be the owner is executing that file. Because other user can manipulate that file when the other user is executing that file, it will give the unexpected result to owner . plz help me... (1 Reply)
Discussion started by: jimmyuk
1 Replies

4. UNIX for Dummies Questions & Answers

Semaphores and File Descriptors

What is the difference between a file descriptor and a semaphore? My basic understanding is: - a file descriptor is a small positive integer that the system uses instead of the file name to identify an open file or socket. - a semaphore is a variable with a value that indicates the... (1 Reply)
Discussion started by: Mr_Webster
1 Replies

5. UNIX for Dummies Questions & Answers

how to list the files using File Descriptors

hello, I have written a script named listall.sh with the following codes init. #!/bin/bash PATH="/proj/cmon/$1" echo $PATH if ; then echo "Usage: $0 ***" exit 1 else ls -l $PATH/*.sc fi Here there are 3 subdirectories (namely - src, data and jobs)under /proj/cmon, so... (2 Replies)
Discussion started by: shyjuezy
2 Replies

6. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

7. UNIX for Dummies Questions & Answers

file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may... (3 Replies)
Discussion started by: a25khan
3 Replies

8. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

9. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies

10. UNIX for Dummies Questions & Answers

Need help to access/mount so to access folder/files on a Remote System using Linux OS

Hi I need to access files from a specific folder of a Linux system from an another Linux System Remotely. I know how to, Export a folder on One SCO System & can access the same by using Import via., NFS in the Sco Unix SVR4 System using the scoadmin utility. Also, I know to use mount -t ... (2 Replies)
Discussion started by: S.Vishwanath
2 Replies
Login or Register to Ask a Question