48
\$\begingroup\$

(Just open 50 tabs in Google Chrome :D (just kidding, no you can't))

Shortest code for infinite disk I/O any language, C# example:

using System.IO;

namespace FileApp {
    static class Program {
        public static void Main() {
            do {
                File.WriteAllText("a", "a");
                File.Delete("a");
            } while (true);
        }
    }
}

You can't just fill the entire disk though, as then it would halt in the end and would be finite.

And you can't do reading only, infinite writing has to happen. (It has to kill my SSD after enough runtime.)

Get cracking! :)

\$\endgroup\$
18
  • 6
    \$\begingroup\$ Does reading files instead of writing them also count as disk I/O? What about writing to /dev/null? (Is yes>/dev/null a valid Bash answer?) \$\endgroup\$
    – Doorknob
    Apr 1, 2016 at 12:33
  • 2
    \$\begingroup\$ Can it take any input ? \$\endgroup\$ Apr 1, 2016 at 14:22
  • 29
    \$\begingroup\$ Dang man...what did your SSD do to you? \$\endgroup\$
    – R. Kap
    Apr 1, 2016 at 23:14
  • 2
    \$\begingroup\$ As I can't hope to compete with 6 byte solutions, would creating the file ./a with the 3 byte contents ./a count for a bonus prize for lateral thinking? AFAIK just executing a file causes some file system writing to take place on many systems, because at the very least 'last access time' gets updated as a byproduct ;-) \$\endgroup\$
    – Stilez
    Apr 2, 2016 at 13:57
  • 3
    \$\begingroup\$ Many of these answers will write the data into the same space over and over. That does not result in an actual disk write even if the data differs. (Extreme case, dos -> windows communications. I wrote 4k of data in dos and read it back in Windows--so long as data was flowing the disk light would stay off.) \$\endgroup\$ Apr 2, 2016 at 23:40

34 Answers 34

1
2
0
\$\begingroup\$

Lua, 39 bytes

while''do io.open('a',"w"):write('')end
\$\endgroup\$
0
\$\begingroup\$

C++, 61 bytes. (or 66 bytes to actually hit the disk on Linux+XFS+lazytime)

Timestamp metadata I/O only:

#include <fstream>
int main(){for(;;){std::ofstream a("a");}}

The disk I/O is due to a's mod time changing, which will be written to disk occasionally. (very occasionally with the commonly-use lazytime mount option. Also see my C answer.)


Write data before closing:

#include <fstream>
int main(){for(;;){std::ofstream a("a");a<<4;}}

Actually hits the disk: lights up my hard drive light. (Linux 4.2.0, XFS, lazytime)


The ofstream constructor opens the file, and the destructor closes it again. There are no resource leaks, so this will actually run forever without running out of memory, file handles, or disk space.

strace output:

open("a", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(3, "4", 1)                        = 1      # not present with first version
close(3)                                = 0
... repeating
\$\endgroup\$
0
\$\begingroup\$

C99, 84 bytes

#include<stdio.h>
int main(){while(1){FILE*f=fopen("a","w");fputc(9,f);fclose(f);};}

If you don't care about buffers, you can do this instead:


C99, 65 bytes

#include<stdio.h>
int main(){while(1){fputc(9,fopen("a","w"));};}

I don't know if it closes automatically (no file handle), so please comment and I'll edit.


Lots of \ts to write (relax, they're not infinite!).

\$\endgroup\$
0
\$\begingroup\$

Tcl, 36 bytes

set f [open a w]
while 1 {puts $f .}

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.