12

I have a C application. Which creates a connection to file at startup and it continuously runs for many days.

I am only connecting one time . and not reconnecting it in code. Only checking if

if(NULL == file)

Is there can be case that my application lose I/O connection of that file handler ?

if yes. Is there a way , How can I create test case for it ?

Vivek Goel
  • 22,942
  • 29
  • 114
  • 186

2 Answers2

10

Yes it's possible. For example if the file is on another machine or on a removable storage. Then you can physically disconnect/unplug the device and the subsequent operations on the handle will fail.

As others mentioned in the comments, you can simulate it in a test case by creating a RAM disk and either unmounting it or killing the driver. Alternatively, if all you care is about unidirectional I/O, maybe you could test your code with a pipe, and then close the other end of the pipe.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • 1
    There are simpler ways to simulate this by intercepting IO calls, see http://stackoverflow.com/questions/1361518/how-can-i-simulate-a-failed-disk-during-testing – Dietrich Epp May 16 '11 at 07:32
  • @ybungalobill If file is same machine , and on system drive. I think in that condition connection will not lost right ? or it can still happen while i/o cycle going above hd limit (I had read that thing somewhere but not sure ) – Vivek Goel May 16 '11 at 07:45
  • 1
    You can have a crashed fuse driver, locally. (transport endpoint not connected error) – sehe May 16 '11 at 07:50
  • 1
    @Vivek: in a controlled environment, assuming no hardware failures, yes, you can assume it is never lost. In general, no. There are plenty of ways to make it happen, an it *does* happen. – Yakov Galka May 16 '11 at 07:51
  • @sehe can you please provide more details ? – Vivek Goel May 16 '11 at 12:07
  • 1
    Yep. On linux, use fuse [to mount an fs, even local (e.g. `rofs`)](http://sourceforge.net/apps/mediawiki/fuse/index.php?title=FileSystems#rofs); then kill/crash the fuse program (_driver_) and access the mountpoint: 'endpoint not connected' – sehe May 16 '11 at 12:09
8

You can loose the connection with the file, but it won't set your file variable to NULL, the symptom will be an IO error. Reading or writing will fail. (For C++ streams, the badbit will be set instead of the failbit which is set for format error;)

AProgrammer
  • 51,233
  • 8
  • 91
  • 143