title: behavior of filedescriptor in fork()
body: <p>When a child is forked from a parent process, all the variables from the parent are copied to the new child process. Hence when we open a common file before the fork happens, and try to read the same file both from parent and child, both should print results independently. But when i try to implement this, the reading from file for parent and child seems to be interleaved. Need to know as to why is this happening. Even the file descriptor should be duplicated when a new process is forked from the parent. So whats going wrong ?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;fcntl.h&gt;
  
  main()
  {
  int fp;
  char buff[11];
  int pid;
  fp = open("file1.txt", O_RDONLY);
  pid = fork();
  if(pid==0)
  {
      printf("Child begins %d\n", getpid());
      read(fp, buff, 5);
      buff[10] = '\0';
      printf("Child read:");
      puts(buff);
      printf("Child exiting\n");
  }
  else
  {
      read(fp, buff, 5);
      buff[10] = '\0';
      printf("Parent read:");
      puts(buff);
      printf("Parent exiting\n");
  }
   }
  </code></pre>
  
  <p>Now suppose the file1.txt has the content "Hello world", then both the parent and child processes should have printed "Hello". But what i see is one among child/parent prints "Hello" and the other prints " world".</p>