fread() on a bad disk?

This is a discussion on fread() on a bad disk? within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, I have the following code which hangs in fread() when one of my disks goes "bad" or is ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    fread() on a bad disk?

    Hi all,

    I have the following code which hangs in fread() when one of my disks goes "bad" or is pulled from system unplanned (I need to support this case)

    Code:
    mbr = fopen(fullpath, "r");
            if (!mbr) {
                    fprintf(stderr, "ERROR: Can't open device %s
    \n",
                            fullpath);
                    return 0;
            }
    
            count = fread(buffer, 1, 512, mbr);
            if (count < 512) {
                      return and print error
            }

    any ideas why/when fread() would just never return??

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    fyi, this is on Redhat linux.....

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    I tried using read() instead but still get the hang.....

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    does buffer contain the 512 bytes?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by vart View Post
    does buffer contain the 512 bytes?

    I have it defined as:

    Code:
    unsigned char buffer[512]

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    why not use sizeof buffer instead?, so you don't make mistakes, and if you change the size of buffer there's no need to change it anywhere else!

    also your check is dodge... your assuming your going to read all 512 bytes? What if there aren't 512 bytes in the file then if(count < 512) will trigger a false error.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by zacs7 View Post
    why not use sizeof buffer instead?, so you don't make mistakes, and if you change the size of buffer there's no need to change it anywhere else!
    I will try that, but I'm guessing you mean for future.
    Or do you think me assuming the size is 512 might be wrong?

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by zacs7 View Post
    why not use sizeof buffer instead?, so you don't make mistakes, and if you change the size of buffer there's no need to change it anywhere else!

    also your check is dodge... your assuming your going to read all 512 bytes? What if there aren't 512 bytes in the file then if(count < 512) will trigger a false error.

    that was just a hack for now. I never get to that line anyway since I hang in the fread()....

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    How big is what you are reading?

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by zacs7 View Post
    why not use sizeof buffer instead?, so you don't make mistakes, and if you change the size of buffer there's no need to change it anywhere else!

    also your check is dodge... your assuming your going to read all 512 bytes? What if there aren't 512 bytes in the file then if(count < 512) will trigger a false error.

    ran with sizeof(buffer) instead. Got same result. (hung in fread())

    I printed the sizeof(buffer) in gdb before the hang, it was 512 as expected.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by markcole View Post
    How big is what you are reading?

    I'm opening up a disk device. Trying to figure out if its a dos disk or not (on linux)

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by annied View Post
    any ideas why/when fread() would just never return??
    Why WOULD it ever return? Huh?

    If you ask for 512 bytes, fread() is going to give you 512 bytes. Since the disk is physically unavailable, this is impossible. So fread() is going to wait forever. fread() is based on the underlying read() system call, and this call isn't going to return.

    Why would you expect it to not hang, I don't get it.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by brewbuck View Post
    Why WOULD it ever return? Huh?

    If you ask for 512 bytes, fread() is going to give you 512 bytes. Since the disk is physically unavailable, this is impossible. So fread() is going to wait forever. fread() is based on the underlying read() system call, and this call isn't going to return.

    Why would you expect it to not hang, I don't get it.

    thought maybe it would timeout at some point so any application doing a read or scan of devices wouldn't end up hanging.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by brewbuck View Post
    Why WOULD it ever return? Huh?

    If you ask for 512 bytes, fread() is going to give you 512 bytes. Since the disk is physically unavailable, this is impossible. So fread() is going to wait forever. fread() is based on the underlying read() system call, and this call isn't going to return.

    Why would you expect it to not hang, I don't get it.

    in addition to my last response, what about in scenarios where we have failover and such.
    If a disk goes bad, we should be able to recover.

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by annied View Post
    in addition to my last response, what about in scenarios where we have failover and such.
    If a disk goes bad, we should be able to recover.
    Then you need to write the code correctly, using non-blocking reads and such. The OS is capable of telling you when a disk is pulled, learn how to do that.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows system disk on an USB storage: possible?
    By BrownB in forum Tech Board
    Replies: 1
    Last Post: 11-10-2006, 04:34 AM
  2. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  3. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM

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