title: where is file descriptor stored in process memory?
body: <p>When a function A is called from a point of execution, internally it is a JMP statement to the address pointing to function A. So the current point of execution is saved onto the stack, the PC loads the address of the called function A and continues.</p>
  
  <p>To get back to the point of execution after the function call, the function block should have equal push and pops onto the stack. Normally in C on exiting the function, the stack variables defined are destroyed(which I presume means popped off the stack), but I decided to define a file descriptor variable inside my function. The code is below:</p>
  
  <pre><code>void main() {
      printf("In the beginning there was main()\n");
      func_call();
      printf("func_call complete\n");
      while(1);
  }
  
  void func_call() {
      int fp;
      //Opening a file to get handle to it.
      fp = open("stack_flush.c", O_RDONLY);
      if (fp &lt; 0 ) {
          perror("fp could not open stack_flush.c");
          return;
      }
  }
  </code></pre>
  
  <p>On running this program and checking lsof, I can see that the fd is still open upon exiting the function func_call().</p>
  
  <pre><code>stack_flu 3791 vvdnlt260    0u   CHR  136,1      0t0        4 /dev/pts/1
  stack_flu 3791 vvdnlt260    1u   CHR  136,1      0t0        4 /dev/pts/1
  stack_flu 3791 vvdnlt260    2u   CHR  136,1      0t0        4 /dev/pts/1
  stack_flu 3791 vvdnlt260    3r   REG    8,3      526 24660187 /home/vvdnlt260/Nishanth/test_space/stack_flush.c
  </code></pre>
  
  <p>I checked the wikipedia entry for file descriptors and I found this:</p>
  
  <blockquote>
    <p>To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables.</p>
  </blockquote>
  
  <p>From the above statement it's obvious that the file descriptor integer value is stored in process memory, but although it was defined in a function, the file descriptor was not local to the function as it did not get removed on function exit. </p>
  
  <p>So my question is 2 fold:</p>
  
  <p>1) If the file descriptor is part of the func_call() stack, then how does the code return to its pre function call execution point although it has not been popped off? Also in this case why does it persist after the function call exists?</p>
  
  <p>2) If not part of the func_call() stack where does the file descriptor reside in the process memory? </p>