Skip to main content
Commonmark migration
Source Link

#C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

#somewhat-portable ANSI C, 38/51 bytes (timestamp only), 52/67 bytes (real disk activity)

somewhat-portable ANSI C, 38/51 bytes (timestamp only), 52/67 bytes (real disk activity)

#C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

#somewhat-portable ANSI C, 38/51 bytes (timestamp only), 52/67 bytes (real disk activity)

C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

somewhat-portable ANSI C, 38/51 bytes (timestamp only), 52/67 bytes (real disk activity)

added 1630 characters in body
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35

I see some answers that open("a",1). O_CREAT is required if a doesn't already exist. O_CREAT is defined as octal 0100 (64, 0x40) on Linux.


No resource leaks, so it can run forever. strace output:

On a filesystem with the Linux lazytime mount option enabled, a change that only affects inode timestamps will only cause one write per 24 hours. With that mount option disabled, timestamp updating might be a viable way to wear out your SSD. (However, several other answers only do metadata I/O).


alternatives:

shorter non-working:

main(){for(;;)close(write(open("a",577),"",3));} uses write's return value to pass a 3 arg to close. It saves another byte, but doesn't work with gcc -O0 or -O3 on amd64. The garbage in the 3rd arg to open is different, and doesn't include write permission. a gets created the first time, but future iterations all fail with -EACCESS.

longer, working, with different system calls:

main(c){for(open("a",65);pwrite(3,"",1);)sync();} rewrites a byte in-place and calls sync() to sync all filesystems system-wide. This keeps the drive light lit up.

We don't care which byte, so we don't pass 4th arg to pwrite. Yay for sparse files:

$ ll -s a
300K -rwx-wx--- 1 peter peter 128T May 15 11:43 a

Writing one byte at an offset of ~128TiB led to xfs using 300kiB of space to hold the extent map, I guess. Don't try this on OS X with HFS+: IIRC, HFS+ doesn't support sparse files, so it will fill the disk.

XFS is a proper 64bit filesystem, supporting individual files up to 8 exabytes. i.e. 2^63-1, the maximum value off_t can hold.

strace output:

open("a", O_WRONLY|O_CREAT, 03777711166007270) = 3
pwrite(3, "\0", 1, 139989929353760)     = 1
sync()                                  = 0
pwrite(3, "\0", 1, 139989929380071)     = 1
sync()                                  = 0
...

No resource leaks, so it can run forever. strace output:

On a filesystem with the Linux lazytime mount option enabled, a change that only affects inode timestamps will only cause one write per 24 hours. With that mount option disabled, timestamp updating might be a viable way to wear out your SSD. (However, several other answers only do metadata I/O).

I see some answers that open("a",1). O_CREAT is required if a doesn't already exist. O_CREAT is defined as octal 0100 (64, 0x40) on Linux.


No resource leaks, so it can run forever. strace output:

On a filesystem with the Linux lazytime mount option enabled, a change that only affects inode timestamps will only cause one write per 24 hours. With that mount option disabled, timestamp updating might be a viable way to wear out your SSD. (However, several other answers only do metadata I/O).


alternatives:

shorter non-working:

main(){for(;;)close(write(open("a",577),"",3));} uses write's return value to pass a 3 arg to close. It saves another byte, but doesn't work with gcc -O0 or -O3 on amd64. The garbage in the 3rd arg to open is different, and doesn't include write permission. a gets created the first time, but future iterations all fail with -EACCESS.

longer, working, with different system calls:

main(c){for(open("a",65);pwrite(3,"",1);)sync();} rewrites a byte in-place and calls sync() to sync all filesystems system-wide. This keeps the drive light lit up.

We don't care which byte, so we don't pass 4th arg to pwrite. Yay for sparse files:

$ ll -s a
300K -rwx-wx--- 1 peter peter 128T May 15 11:43 a

Writing one byte at an offset of ~128TiB led to xfs using 300kiB of space to hold the extent map, I guess. Don't try this on OS X with HFS+: IIRC, HFS+ doesn't support sparse files, so it will fill the disk.

XFS is a proper 64bit filesystem, supporting individual files up to 8 exabytes. i.e. 2^63-1, the maximum value off_t can hold.

strace output:

open("a", O_WRONLY|O_CREAT, 03777711166007270) = 3
pwrite(3, "\0", 1, 139989929353760)     = 1
sync()                                  = 0
pwrite(3, "\0", 1, 139989929380071)     = 1
sync()                                  = 0
...
don't need to save the open() return value: it will be 3 eventually
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35

#C on amd64 Linux, 36 bytes (timestamp only), 5252 49 bytes (real disk activity)

main(c){for(;write(c=openopen("a",577),"",1);close(c3));}

We don't need to save the return value from open(). We assume it's 3, because it always will be. Even if we start with fd 3 open, it will be closed after the first iteration. Worst-case, open keeps opening new fds until 3 is the last available file descriptor. So, up to the first 65531 write() calls could fail with EBADF, but will then work normally with every open creating fd = 3.

#C on amd64 Linux, 36 bytes (timestamp only), 52 bytes (real disk activity)

main(c){for(;write(c=open("a",577),"",1);close(c));}

#C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

main(){for(;write(open("a",577),"",1);close(3));}

We don't need to save the return value from open(). We assume it's 3, because it always will be. Even if we start with fd 3 open, it will be closed after the first iteration. Worst-case, open keeps opening new fds until 3 is the last available file descriptor. So, up to the first 65531 write() calls could fail with EBADF, but will then work normally with every open creating fd = 3.

added 96 characters in body
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading
ANSI C version
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading
for-loop trick. (And I think byte-count had been wrong on the timestamp-only version for a while).
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading
actually hit the disk by writing a byte
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading
added 62 characters in body
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading
Source Link
Peter Cordes
  • 5.1k
  • 1
  • 24
  • 35
Loading