tags:

views:

130

answers:

2
+2  Q: 

Open system call

Hi guys, I'm studying for my operating systems midterm and was wondering if I can get some help.

Can someone explain the checks and what the kernel does during the open() system call?

Thanks!

+1  A: 

I would go back to what the prof told you - there a lot of things that happen during open(), depending on what you're opening (i.e. a device, a file, a directory), and unless you write what the professor's looking for, you'll lose points.

That being said, it mostly involves the checks to see if this open is valid (i.e. does this file exist, does the user have permissions to read/write it, etc), then an entry in the kernel handle table is allocated to keep track of the fd and its current file position (and of course, some other things)

Paul Betts
+5  A: 

Very roughly, you can think of the following steps:

  1. Translate the file name into an inode, which is the actual file system object describing the contents of the file, by traversing the filesystem data structures.
  2. During this traversal, the kernel will check that you have sufficient access through the directory path to the file, and check access on the file itself. The precise checks depend on what modes were passed to open.
  3. Create what's sometimes called an open file descriptor within the kernel. There is one of these objects for each file the kernel has opened on behalf of any process.
  4. Allocate an unused index in the per-process file descriptor table, and point it at the open file descriptor.
  5. Return this index from the system call as the file descriptor.

This description should be essentially correct for opening plain files and/or directories, but things are different for various sorts of special files, in particular for devices.

Dale Hagglund