Tag Archives: OS

Simulating disk-full scenario

Unix-like operating systems provide a number of device files in order to simulate different behaviors. In this article we will focus on simulating a “disk-full” scenario using a special file named /dev/full. Its a special file which always returns an error when an attempt is made to write to this file. Lets try to simulate this through a program :

/* disk-full.c */

#include 
#include 
#include 

int main()
{
  int fd;
  ssize_t rc;
  char *buf= "hello world!";

  // Open /dev/full for writing.
  fd= open("/dev/full", "w");

  if (fd == -1)
    printf("Error! Couldn't open the file. %d : %s\n",
           errno, strerror(errno));

  /* Now, lets try to write into this file. */
  rc= write(fd, buf, 1);

  if (rc == -1)
    printf("Error! Couldn't write to the file. %d : %s\n",
           errno, strerror(errno));

  close(fd);
  return 0;
}

Output :

./a.out 
Error! Couldn't write to the file. 28 : No space left on device

Besides /dev/full, there are other interesting device files available under /dev, which can help in a multitude of ways.

> hexdump -Cn80 /dev/urandom 
00000000  ee 32 7c 62 97 17 79 33  71 20 d5 2e 82 32 cd 32  |.2|b..y3q ...2.2|
00000010  26 02 b4 75 4e 90 5d 2f  fb 7f 58 d8 9b ba 50 34  |&..uN.]/..X...P4|
00000020  f5 30 2a e0 b1 95 ea a4  0c 92 ef 57 99 0e b1 ec  |.0*........W....|
00000030  a5 78 df 45 39 df 17 02  12 c1 0b dd cc d0 e0 ad  |.x.E9...........|
00000040  fd 65 c8 71 25 e7 1d 71  c9 77 1d 2a 57 78 be 1f  |.e.q%..q.w.*Wx..|