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; Originally Posted by brewbuck Then you need to write the code correctly, using non-blocking reads and such. The OS is ...

  1. #16
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by brewbuck View Post
    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.

    very hostile.....thanks for the help?

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by annied View Post
    very hostile.....thanks for the help?
    Sorry, didn't mean that to be hostile at all. I'm just saying that doing blocking reads is not the right way to do this.

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by brewbuck View Post
    Sorry, didn't mean that to be hostile at all. I'm just saying that doing blocking reads is not the right way to do this.

    My apologizes, guess that's why email/internet is tough to hear tone in someone's voice.

    Thanks for your help, I'll try to figure out the better way to do this.

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by annied View Post
    My apologizes, guess that's why email/internet is tough to hear tone in someone's voice.

    Thanks for your help, I'll try to figure out the better way to do this.
    Id' suggest, for starters, to use fcntl on the open fd to put it into a non-blocking mode. And investigate how to get disk status from the kernel. Probably via the /proc filesystem somehow. Been a while since I wrote that sort of code.

  5. #20
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by brewbuck View Post
    Id' suggest, for starters, to use fcntl on the open fd to put it into a non-blocking mode. And investigate how to get disk status from the kernel. Probably via the /proc filesystem somehow. Been a while since I wrote that sort of code.

    thanks much for the pointers. Hopefully I can report back with what worked.

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    I would suggest you look at using open() and read(), as opposed to the standard fopen/fread approach. You'll have a lot more scope for controlling what is going on, with what is after all rather system specific to begin with.
    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.

  7. #22
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by Salem View Post
    I would suggest you look at using open() and read(), as opposed to the standard fopen/fread approach. You'll have a lot more scope for controlling what is going on, with what is after all rather system specific to begin with.
    I had tried an open() (read-only) and read() and got the same result....

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You also need to put the stream in non-blocking mode. Salem's point is that this is impossible in the first place with fread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by CornedBee View Post
    You also need to put the stream in non-blocking mode. Salem's point is that this is impossible in the first place with fread.

    any idea how I would go about putting it in a non-blocking mode?
    I've been looking around but haven't had much success....

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Passing O_NONBLOCK in mode to open() sounds like an option.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    As already mentioned - http://www.rt.com/man/fcntl.2.html
    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.

  12. #27
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by CornedBee View Post
    Passing O_NONBLOCK in mode to open() sounds like an option.
    was just looking at that, going to try it but been reading that its not recommended...thanks

  13. #28
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    tried with the open()/read() and the open with O_NONBLOCK but no luck.
    wondering if adding O_NDELAY would help.

  14. #29
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You could start another thread to monitor the disks or something? Very OS specific, but so is your problem.

  15. #30
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Quote Originally Posted by Salem View Post
    As already mentioned - http://www.rt.com/man/fcntl.2.html
    tried with fcntl(), I got passed the read() but then hang on the close(fd).

    I'm sure I missed something here. Can someone have a look?
    Not that familiar with fcntl()

    Code:
            int             fd_flags = 0;
    
            fd = open(fullpath, O_RDONLY|O_NONBLOCK|O_NDELAY);
            if (!fd) {
                    fprintf(stderr, "ERROR: Can't open device %s
    \n",
                            fullpath);
                    return 0;
            }
            fd_flags = fcntl(fd, F_GETFL, 0);
            fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
    
            ret = read(fd, buffer, sizeof(buffer));
            close(fd);

Page 2 of 3 FirstFirst 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, 03:34 AM
  2. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 10:42 PM
  3. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 08:26 AM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 07: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, 07:31 AM

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