Help required in understanding fread call!!

This is a discussion on Help required in understanding fread call!! within the C Programming forums, part of the General Programming Boards category; Code: In the following program,I have doubt in the second printf which is in the child process.Actually in file opreations,file ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Help required in understanding fread call!!

    Code:
    In the following program,I have doubt in the second printf which is in the child 
    process.Actually in file opreations,file descriptor will be shared among parent
     and child process.
    So,We should get output as 4107 in second printf.
    But instead of that i got 11.
    Please clarify me why it is happening!!!
    
    code:
    ====
    #include <stdio.h>
    #include <memory.h>
    #include <stdlib.h>
    
    main() {
            FILE *fp;
            char buff[11];
            int pid , i ;
            fp = fopen ( "book.txt" , "w+" );
    
            for ( i = 0 ; i < 4096 ; i++ )
                    fprintf(fp , "A" );
            for ( i = 0 ; i < 4096 ; i++ )
                    fprintf(fp , "B" );
            for ( i = 0 ; i < 4096 ; i++ )
                    fprintf(fp , "C" );
            for ( i = 0 ; i < 4096 ; i++ )
                    fprintf(fp , "D" );
    
            fclose(fp);
            fp = fopen("book.txt","r");
    
            pid = fork();
    
            if ( pid == 0 ) {
                    printf("Child starts...ftell -> %ld\n",ftell(fp));
                    fread ( buff , sizeof(buff) , 1 , fp );
                    buff[10] = '\0';
                    printf("Child -> After child fread -> ftell -> %ld %s\n",ftell(fp),buff);
                    sleep(5);
                    printf("Child -> After parent read ->ftell -> %ld %s\n",ftell(fp),buff);
    
                    fread(buff,sizeof(buff),1,fp);
                    buff[10] = '\0';
                    printf("Child -> After 2nd child fread -> ftell -> %ld %s\n",ftell(fp),buff);
            }
            else {
                    sleep(1);
                    printf("Initially in parent -> ftell -> %ld \n",ftell(fp));
                    fread(buff,sizeof(buff),1,fp);
                    buff[10] = '\0';
                    printf("After parent read -> ftell -> %ld %s\n",ftell(fp),buff);
    
            }
            fclose(fp);
    }
    
    
    
    Thanks in advance.
    
    Regards,
    Rajisankar

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is it you think you should be getting 4107? You read 11 bytes, so why do you think ftell should give you something else?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    sizeof(buff) is 11. That should give you a hint.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by quzah View Post
    Why is it you think you should be getting 4107? You read 11 bytes, so why do you think ftell should give you something else?


    Quzah.
    Code:
    Quzah!! It should be 4107.
    Because fread will read 4096 bytes in 16-bit processor.
    But it returned whatever the size we defined in buff.( here sizeof(buff) is 11 )
    So,In child process,first fread actually reads 4096 bytes,but it returns 11 bytes 
    while using ftell function.(This is the beauty of fread function)
    By this concept it returns 4096 in parent process.
    After fread in parent process,actually the fread pointer will be in 8192 .
    But it returns 4107(4096+11).
    Right now,no problem in understanding...Here my doubt started......
    "In file operations,file descriptor will be shared among parent and child processes."
    So, the second printf which is in Child process will return 4107.
    But it is not happen.
    I know some concepts behind this issue.But i cannot found yet.
    
    Thanks.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,688
    I would suggest you stop trying to understand undefined behaviour, because it won't be anything you can re-use in another programming scenario.

    You're basically asking "I drove my car into a wall and I survived - why is that?"

    This is "voodoo" programming. You can come up with all the bizarre "I can explain....", cases you want, but most people who know what they're talking about will just see that you're blowing smoke out of your ass.

    If you really want to know how to read the same file in two separate processes, then ask "How do I read a file in two separate processes".
    Not what you posted, seeking some mystical explanation for it, in the hope of understanding how it might work for you (because it never will, reliably).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Note that file descriptors created by dup(2) or fork(2) share the current file position pointer, so seeking on such files may be subject to race conditions.
    lseek(2): reposition read/write file offset - Linux man page

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help required
    By rits in forum C Programming
    Replies: 6
    Last Post: 12-11-2009, 02:52 PM
  2. Help Required
    By Fatima Rizwan in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2009, 06:49 AM
  3. Help required
    By ItsMeHere in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 08:26 AM
  4. Little help required...
    By Ninestar in forum C Programming
    Replies: 5
    Last Post: 03-17-2006, 04:32 PM
  5. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21